Search code examples
c#asp.netgridviewcode-behindcontrolparameter

Could not find control 'MainContent_DropDownList1' in ControlParameter 'Company'


I was debugging and I got the strange error listed above. It was weird because my Dropdownlist is called DropDownlist1. When I set it equal to that earlier, the debugger told me it could not cast it to a string.

So here is my C#, where the problem is.

 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
       Gridsource.ConnectionString = "Data Source=CATALOGSERVER;Initial Catalog=UACOrders;Persist Security Info=True;User ID=Catalog;Password=pass";
        Gridsource.SelectCommandType = SqlDataSourceCommandType.StoredProcedure;
        Gridsource.SelectCommand = "GetOrderHistory";
        Gridsource.DataSourceMode = SqlDataSourceMode.DataSet;
        GridView1.DataSource = Gridsource;

        ControlParameter cp = new ControlParameter();
        cp.ControlID = DropDownList1.ClientID;
        cp.Name = "Company";
        cp.PropertyName = "SelectedValue";
        cp.Type = System.TypeCode.String;
        Gridsource.SelectParameters.Add(cp);
        Gridsource.Page = this;
        this.Controls.Add(Gridsource);
        GridView1.DataBind();
        updatepanel1.Update();
 }

And here is the small bit of asp.net involved

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<div id="contentarea">     
    <asp:ScriptManager runat="server"></asp:ScriptManager>
    <asp:UpdatePanel runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional" ID="updatepanel1">
        <ContentTemplate>

            <div>
                <asp:Label Text="Company: " runat="server"></asp:Label>
                <asp:DropDownList ID="DropDownList1" runat="server"
                     DataSourceID="company" DataTextField="COMPANY" DataValueField="COMPANY" 
                    OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
                     AutoPostBack="true" >
                </asp:DropDownList>
                <asp:SqlDataSource ID="company"
                     runat="server" ConnectionString="<%$ ConnectionStrings:UACOrdersConnectionString %>"
                     SelectCommand="SELECT [Customer] AS [COMPANY] FROM [Accounts] ORDER BY [Customer]">
                </asp:SqlDataSource>
                <asp:SqlDataSource ID="Gridsource" runat="server"></asp:SqlDataSource>

So oddly enough I can get this sort of thing working in the asp.net code normally. But I can't seem to get it to work in the C# codebehind which this particular webpage requires. So again, I need to find out what cp.ControlID shoudld really be equal to.

The specific Exception is "InvalidOperationException was unhandled by user code." "Could not find control 'DropDownList1' in ControlParameter 'Company'."

VS putting the Exception at line 39 which is

         GridView1.DataBind();

If you need to see more code just leave a comment and I will post more.


Solution

  • Based on your updated code in your question, I can see that the problem is that DropDownList and the SqlDataSource (to which this ControlParameter belongs) are not in the same Naming Container. You have a couple of options:

    1. Add the SqlDataSource to your markup, instead of creating only in code-behind, and then adding it late in the Page lifecylce. This is the simplest option.
    2. Add the SqlDataSource to the Page.Controls collection programatically early in the Page lifecycle. Like during the Init event.

    Like this:

    protected void Page_Init(object sender, EventArgs e)
    {
        Page.Controls.Add(Gridsource);
    }