首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在代码隐藏中找不到GridView中的ASP.NET控件

问在代码隐藏中找不到GridView中的ASP.NET控件
EN

Stack Overflow用户
提问于 2016-09-07 11:09:04
回答 3查看 1.4K关注 0票数 0

我有一个要用DataBase中的列值填充的DropDownList。然而,当我试图在后台代码中绑定DropDownList时,集成开发环境一直告诉我:

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

我不知道是怎么回事,因为我是通过它的ID来引用控件的。下面是代码:

aspx:

代码语言:javascript
复制
<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#:

代码语言:javascript
复制
    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(); 
}

集成开发环境找不到EqpCatDDL控件。

我使用的是: Visual Studio 2010、Microsoft SQL Server Management Studio 2008

我正在使用Visual Studio网站

EN

回答 3

Stack Overflow用户

发布于 2016-09-07 20:49:33

使用此代码可以在不使用RowDataBound的情况下将数据绑定到dropdown。

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

代码语言:javascript
复制
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,然后再绑定您的dropdown

票数 2
EN

Stack Overflow用户

发布于 2016-09-07 11:52:25

您的下拉列表位于网格视图中,因此您可以尝试使用以下代码

代码语言:javascript
复制
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();

}
}
票数 1
EN

Stack Overflow用户

发布于 2016-09-07 12:00:28

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

代码语言:javascript
复制
GridView1.DataSource = DataSource

如果你想访问这个网格视图的dropdownlist,你可以使用GridView的RowDataBound事件处理程序。

代码语言:javascript
复制
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();
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39360652

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档