未发现 GridView 中的 ASP.NET 控件存在于后面的代码中 [英] ASP.NET control in GridView not found to exist in code behind
问题描述
我有一个 DropDownList,我想用数据库中的列值填充它.但是,当我尝试在后面的代码中绑定 DropDownList 时,IDE 一直告诉我:
I have a DropDownList that I would like to populate with column values from a DataBase. However, when I try to bind the DropDownList in code behind, the IDE keeps telling me:
当前上下文中不存在名称‘EqpCatDDL’"
"The name 'EqpCatDDL' does not exist in the current context"
我不确定发生了什么,因为我通过其 ID 引用了控件.代码如下:
I am not sure what is going on since I referred to the control by its ID. The following is the code:
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 控件.
The IDE can't find the EqpCatDDL control.
我正在使用以下软件:Visual Studio 2010、Microsoft SQL Server Management Studio 2008
I am using the following: Visual Studio 2010, Microsoft SQL Server Management Studio 2008
我正在使用 Visual Studio 网站
I am working with a Visual Studio website
推荐答案
使用此代码将数据绑定到 dropdown
而不使用 RowDataBound
.
Use this code to bound data to dropdown
without using RowDataBound
.
创建一个函数,将数据绑定到 dropdown
如下,并在 Page_Load
事件中调用它
Create a function that'll bind the Data to dropdown
as follow and call it in Page_Load
event
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,然后你必须绑定你的下拉框
Please note that you have to bind
GridView first, and then you have to bind your dropdown
这篇关于未发现 GridView 中的 ASP.NET 控件存在于后面的代码中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!