【问题标题】:ASP.NET control in GridView not found to exist in code behindGridView 中的 ASP.NET 控件在后面的代码中不存在
【发布时间】:2016-09-13 18:07:24
【问题描述】:

我有一个 DropDownList,我想用数据库中的列值填充它。但是,当我尝试在后面的代码中绑定 DropDownList 时,IDE 一直告诉我:

“当前上下文中不存在名称‘EqpCatDDL’”

我不确定发生了什么,因为我通过它的 ID 引用了控件。以下是代码:

aspx:

<asp:GridView ID="Gridview1" runat="server" ShowFooter="true" 
                AutoGenerateColumns="false" 
                >
                <Columns>
                    <asp:BoundField DataField="S/N" HeaderText="S/N" />
                    <asp:TemplateField HeaderText="Item Name">
                        <ItemTemplate>
                            <asp:DropDownList ID="EqpCatDDL" runat="server"></asp:DropDownList>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Description">
                        <ItemTemplate>
                            <asp:DropDownList ID="DescripDDL" runat="server">
                            </asp:DropDownList>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Quantity">
                        <ItemTemplate>
                            <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Remarks">
                        <ItemTemplate>
                            <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
                        </ItemTemplate>
                        <FooterStyle HorizontalAlign="Right" />
                        <FooterTemplate>
                            <asp:Button ID="ButtonAdd" onclick="ButtonAdd_Click" runat="server" Text="Add New Row" />
                        </FooterTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>

c#:

    public void Populate1()
{
    string connString = ConfigurationManager.ConnectionStrings["MyDbConn"].ConnectionString;
    SqlConnection connection = new SqlConnection(connString);

    SqlCommand cmd = new SqlCommand("SELECT EqpCateID, EqpCat FROM EqpCategory", connection);
    cmd.Connection.Open();

    SqlDataReader ddlValues;
    ddlValues = cmd.ExecuteReader();

    EqpCatDDL.DataSource = ddlValues;
    EqpCatDDL.DataValueField = "EqpCateID";
    EqpCatDDL.DataTextField = "EqpCat";
    EqpCatDDL.DataBind();

    cmd.Connection.Close();
    cmd.Connection.Dispose();
}
protected void Page_Load(object sender, EventArgs e)
{
    Populate1(); 
}

IDE 找不到 EqpCatDDL 控件。

我正在使用以下产品:Visual Studio 2010、Microsoft SQL Server Management Studio 2008

我正在使用 Visual Studio 网站

【问题讨论】:

  • 你不能像上面那样在GridView中绑定一个DropDownList。为此,您需要一个循环来迭代并获取它的索引值。

标签: c# asp.net visual-studio-2010


【解决方案1】:

使用此代码将数据绑定到dropdown,而不使用RowDataBound

创建一个将数据绑定到dropdown 的函数,如下所示,并在Page_Load 事件中调用它

Public void fill_gridView_dropDown()
{
    // your connection and query to retrieve dropdown data will go here 
    // this loop will go through all row in GridView
    foreach(GridViewRow row in your_gridView_Name.Rows) {
        DropDownList  dropDown = (DropDownList)row.FindControl("dropDownList_id");
        dropDown.DataSource = dataSource;
        dropDown.DataValueField = "ValueField";
        dropDown.DataTextField = "TextField";
        dropDown.DataBind();
    }
}

请注意,你必须先bindGridView,然后你必须绑定你的下拉菜单

【讨论】:

    【解决方案2】:

    您的下拉菜单位于 gridview 中,因此您可以尝试使用此代码

    protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        var ddl = (DropDownList)e.Row.FindControl("EqpCatDDL'");
        SqlCommand cmd = new SqlCommand("SELECT EqpCateID, EqpCat FROM EqpCategory", connection);
    
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
    
        EqpCatDDL.DataSource = ds;
        EqpCatDDL.DataValueField = "EqpCateID";
        EqpCatDDL.DataTextField = "EqpCat";
        EqpCatDDL.DataBind();
    
    }
    }
    

    【讨论】:

    • 我会将数据库访问移至 Page_load 并使用全局变量来存储数据集。这样数据库只被调用一次,而不是每个数据行一次。
    • 您可以为包含您的结果的数据集创建全局方法,方法返回类型为 DataSet。因此您可以申请为 DDL.DataSource = 您的方法名称;
    • 问题在于方法,因此每个行都访问数据库。如果有 1000 行,即 999 调用数据库太多。
    • aspforums.net/Threads/196731/… 只需转到此链接。它对您有用
    • 另一种选择是在 DataBound 而不是 RowDataBound 中进行。您只能访问数据库一次,然后遍历行以填充每一行中的 DropDownList。
    【解决方案3】:

    你不能像这样直接填充GridView'sdropdownlist。您需要先设置GridView的数据源,即

    GridView1.DataSource = DataSource
    

    如果您想访问此网格视图的dropdownlist,可以使用GridViewRowDataBound 事件处理程序,即

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //Checking whether the Row is Data Row
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //Finding the Dropdown control.
            Control ctrl = e.Row.FindControl("EqpCatDDL");
            if (ctrl != null)
            {
                DropDownList dd = ctrl as DropDownList;
                List lst = new List();
                dd.DataSource = lst;
                dd.DataBind();
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-02
      • 2011-09-05
      • 1970-01-01
      • 2021-01-17
      • 2013-12-14
      相关资源
      最近更新 更多