繁体   English   中英

GridView中的ASP.NET控件在后面的代码中不存在

[英]ASP.NET control in GridView not found to exist in code behind

我有一个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网站

使用此代码将数据绑定到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 ,则可以使用GridView RowDataBound事件处理程序,即

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();
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM