GridView中的ASP.NET控件未发现存在于代码后面

时间:2016-09-07 03:09:04

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

我有一个DropDownList,我想用DataBase中的列值填充。但是,当我尝试在后面的代码中绑定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网站

3 个答案:

答案 0 :(得分:2)

使用此代码将数据绑定到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,然后必须绑定您的下拉列表

答案 1 :(得分:1)

您的下拉列表是在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();

}
}

答案 2 :(得分:0)

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

GridView1.DataSource = DataSource

如果您想访问此gridview的dropdownlist,可以使用RowDataBound的{​​{1}}事件处理程序,即

GridView