ASP.GridView中的.NET控件在后面的代码中没有找到

本文关键字:代码 中的 GridView NET 控件 在后面 ASP | 更新日期: 2023-09-27 18:06:09

我有一个下拉列表,我想从数据库的列值填充。然而,当我试图在代码中绑定下拉列表时,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网站

ASP.GridView中的.NET控件在后面的代码中没有找到

合作

使用此代码将数据绑定到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();
    }
}

请注意,你必须首先bind GridView,然后你必须绑定下拉菜单

你的下拉菜单在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();
}
}

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

GridView1.DataSource = DataSource

如果你想访问这个gridview的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();
        }
    }
}