For the system I am creating there are various reports, some of which require input parameters from the user. I have my system working for reports thay require no parameters but can't seem to figure this out. One member of the team has written stored procedures, one being report lookup which I use to see if the report has any input parameters. I've saved these results in a datatable but I need to place text boxes, drop down lists etc on the screen for the parameters, the datatable returns the parameter, label to use beside input fields, and a data type. What I need help with is how can I dynamically build a form based on the report selected. Thanks in advance.

Hi

It is possible to create your controls dynamically and add them to your page. For example, consider the following:

ASPX Designer

<body>
    <form id="form1" runat="server">
        <div>
            <asp:PlaceHolder ID="reportParametersPlaceHolder" runat="server"></asp:PlaceHolder>
        </div>
        <input type="submit" value="Submit" />
    </form>
</body>

The place holder will be where we add the dynamic controls as follows:

Code behind

if (!Page.IsPostBack)
{
    Label myLabel = new Label();
    myLabel.Text = "Parameter Label";
    myLabel.ID = "ParameterLabel";

    reportParametersPlaceHolder.Controls.Add(myLabel);

    TextBox myTextBox = new TextBox();
    myTextBox.ID = "ParameterTextBox";

    reportParametersPlaceHolder.Controls.Add(myTextBox);

    DropDownList myDropDownList = new DropDownList();
    myDropDownList.ID = "ParameterDropDownList";
    myDropDownList.Items.AddRange(new[] { new ListItem("Item 1", "Value 1"), new ListItem("Item 2", "Value 2"), new ListItem("Item 3", "Value 3") });

    reportParametersPlaceHolder.Controls.Add(myDropDownList);
}
else
{
    Response.Write(String.Format("Value of text box: {0}<br />", Request.Form["ParameterTextBox"]));
    Response.Write(String.Format("Selected value from Drop Down List: {0}", Request.Form["ParameterDropDownList"]));
}

The above creates the controls and adds them to the Controls collection of the PlaceHolder control (if the page is not a postback). When the button is clicked and the PostBack occurs, the code makes use of the Request.Form["ID"] to get the actual data (text from the text box or selected value from the Drop Down List).

Obviously, you will need to enumerate your DataTable that contains the parameter information and based on what type of data type is required you will then determine what type of control to create and if necessary, what data is populated into the control (e.g. DropDownList).

You could also make use of a controls Attributes collection to add further information to the control when you are creating it (such as the parameter it maps back to in the stored procedure) and then you can read this data when the form is submitted.

Hopefully the above gives you a start. If you need further info could you provide an example of what data you get back from your stored procedure.

HTH

Thank you for the quick response :) i'll give it a go and see how I get on. The stored procedure is returning data such as:

Parameter : date
data_type : datetime
label : Date

and then for some there is another table which contains the information for any controls that need to be a drop down.

Parameter : ID
dropdown : dropdown_SP
label: ID

the dropdown field contains the name of a stored procedure that will be used to populate the dropdown list.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.