Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic ControlParameters bound to Dynamically Generated Controls

Status
Not open for further replies.

DTaylorAtWork

Technical User
Jul 12, 2007
11
CA
I've been tasked with creating pages to allow users to create edit and display their own reports. I have the generation functioning properly, if not efficiently but it's when I go to the running of the report that things get problematic.

Because the reports are dynamic they can have any number of parameters involved so I need to generate controls (for now just text boxes) to allow the users to give the parameters values.

The controls are generated properly and no errors are thrown during execution but if the report has any parameters then no results are shown. The grid view is populated properly if there are no parameters in the select query.

Here is the code for generating the controls.

Code:
    protected override void OnInit(EventArgs e)
    {
        currReport = new Report(Convert.ToInt32(Request.QueryString["ReportID"]));

            ((Label)Master.FindControl("lblMain")).Text = "Running Report: " + currReport.Name;

            int varIndex = 0;
            while (varIndex < currReport.Command.LastIndexOf("@") && currReport.Command.IndexOf("@") > -1)
            {
                varIndex = currReport.Command.IndexOf("@");
                string variable = currReport.Command.Substring(varIndex,
                    currReport.Command.IndexOf(" ", varIndex) - varIndex);

                Form.FindControl("ContentPlaceHolder1").Controls.AddAt(0, new Label());
                Form.FindControl("ContentPlaceHolder1").Controls[0].ID = "lbl" + variable.Substring(1);
                ((Label)Form.FindControl("ContentPlaceHolder1").Controls[0]).Text = variable;

                Form.FindControl("ContentPlaceHolder1").Controls.AddAt(1, new TextBox());
                Form.FindControl("ContentPlaceHolder1").Controls[1].ID = "txt" + variable.Substring(1);
            }

            Form.FindControl("ContentPlaceHolder1").Controls.Add(currReport.BuildDataSource("sqldsResults"));

            GridView gvResults = new GridView();
            Form.FindControl("ContentPlaceHolder1").Controls.Add(gvResults);
            gvResults.ID = "gvResults";
            gvResults.AutoGenerateColumns = true;
            gvResults.DataSourceID = "sqldsResults";
            gvResults.EnableViewState = true;
    }

And the BuildDataSource function
Code:
    public SqlDataSource BuildDataSource(string id)
    {
        sqldsResults = new SqlDataSource(conn.ConnectionString, SelectCommand);
        int varIndex = 0;

        while (Command.IndexOf("@") > -1 && varIndex < Command.LastIndexOf("@"))
        {
            varIndex = Command.IndexOf("@");
            string variable = Command.Substring(varIndex + 1,
                Command.IndexOf(" ", varIndex) - (varIndex + 1));

            ControlParameter param = new ControlParameter(variable, "txt" + variable);
            sqldsResults.SelectParameters.Add(param);
        }

        sqldsResults.ID = id;
        return sqldsResults;
    }

Can anyone give me any hints on what to try next?
 
I just realized that I forgot to set the PropertyName for my parameter and fixed that but it still gives me no results. When I set the default value I can get results but even when I type something different into the TextBox the results don't change, defeating the purpose.

Is it even possible to have Control Parameters bound to dynamically created controls?
 
I would suggest not using the datasource controls and writing stored procedures to handle data access.
 
I'm not certain how I would avoid using a datasource even with stored procedures. I could change the current datasource to execute the procedure rather than run a SELECT command but it would still have all the same parameter requirements.

Thanks for the suggestion. If I'm just not understanding it correctly I'm still open to the idea.

David I Taylor
 
I finally found my problem after thoroughly checking several watches and seeing that the GridView had rows in it but wasn't displaying. I wasn't adding the controls deeply enough into the page.

There was an AJAX update panel where my button to generate the results resided. After changing my controls to be added to
Code:
((UpdatePanel)Form.FindControl("ContentPlaceHolder1").FindControl("UpdatePanel1")).ContentTemplateContainer
things are working fine.

Thanks for the attempts to help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top