Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 找不到控件';主内容下拉列表1和x27;在控制参数中';公司';_C#_Asp.net_Gridview_Code Behind_Controlparameter - Fatal编程技术网

C# 找不到控件';主内容下拉列表1和x27;在控制参数中';公司';

C# 找不到控件';主内容下拉列表1和x27;在控制参数中';公司';,c#,asp.net,gridview,code-behind,controlparameter,C#,Asp.net,Gridview,Code Behind,Controlparameter,我正在调试,出现了上面列出的奇怪错误。这很奇怪,因为我的Dropdownlist叫做DropDownlist1。当我早些时候将其设置为该值时,调试器告诉我它无法将其转换为字符串 这是我的C#,问题就在这里 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { Gridsource.ConnectionString = "Data Source=CATALOGSERVE

我正在调试,出现了上面列出的奇怪错误。这很奇怪,因为我的Dropdownlist叫做DropDownlist1。当我早些时候将其设置为该值时,调试器告诉我它无法将其转换为字符串

这是我的C#,问题就在这里

 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();
 }
下面是asp.net的一小部分

<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>

如果您需要查看更多代码,请留下评论,我将发布更多。

根据您在问题中更新的代码,我可以看出问题在于DropDownList和SqlDataSource(此ControlParameter所属)不在同一命名容器中。您有两个选择:

  • 将SqlDataSource添加到标记中,而不是仅在代码隐藏中创建,然后在页面生命周期的后期添加。这是最简单的选择
  • 将SqlDataSource添加到页面。在页面生命周期的早期以编程方式控制集合。比如在Init事件期间
  • 像这样:

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

    亚历山大:你为什么要给出cp.ControlID=“DropDownList1”;,DropDownList 1是DropDownList的对象,因此不能将此名称作为字符串传递,并且cp.Type=System.TypeCode.string是字符串类型,因此cp不能包含对象


    如果要将控件添加到cp中,则cp.ControlId=DropDownList1.ClientID

    请发布异常消息。调试时,会引发哪一行异常。您的下拉列表是在页面上还是在模板(例如EditTemplate)中?下拉列表是在aspx页面上。我刚刚在末尾发布了异常消息。这改变了问题,但无法解决问题。新错误是在控制参数“Company”中找不到控件“MainContent\u DropDownList1”。我想你很接近。也许还有别的问题。你需要看更多我的代码吗?@AlexanderRyanBaggett是的,我需要。与“DropDownList1”相关的SqlDataSource“Gridsource”在哪里?Gridsource1位于页面加载之前,类似于SqlDataSource Gridsource=new SqlDataSource();受保护的无效页面加载(对象发送方,EventArgs e){}@alexah,这就是问题所在。它们不在同一个命名容器中(因为它不在页面的控件集合中),所以cp找不到DropDownList 1。在DropDownList之后的标记中不包含SqlDataSource有什么原因吗?有,我有另一个SqlDataSource绑定到它下面的下拉列表。我想我也可以把它放在那里。很抱歉,我刚刚更新了代码。我仍然有同样的问题。jadarnel27早些时候指出了这一点。
    protected void Page_Init(object sender, EventArgs e)
    {
        Page.Controls.Add(Gridsource);
    }