访问 GridView 中的 DropDownList






3.33/5 (12投票s)
访问 GridView 中的 DropDownList 及其事件
引言
本文演示了如何在 GridView
内部绑定 DropDownList
,以及如何在 DropDownList
的 SelectedIndexChanged
事件中绑定 TextBox
。
Using the Code
首先,将 GridView
控件添加到页面,添加任何绑定的列,然后添加两个模板列:一个用于 DropDownList
,一个用于 TextBox
。 将 DropDownList
的 AutoPostBack
属性设置为 "True
",并创建 SelectedIndexChanged
事件。
ASPX 代码如下所示
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="CustomerID" HeaderText="Customer ID" />
<asp:BoundField DataField="CustomerName" HeaderText="Customer Name"/>
<asp:TemplateField HeaderText="Sample Dropdown">
<ItemTemplate>
<asp:DropDownList Width="50" runat="server"
id="ddlTest" AutoPostBack="true"
OnSelectedIndexChanged="ddlTest_SelectedIndexChanged">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Sample Textbox">
<ItemTemplate>
<asp:TextBox ID="txtTest" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
让我们继续进行代码隐藏部分。
首先要做的是将网格绑定到一些示例数据。 为了简单起见,我将示例数据绑定到网格。为此,我创建了 Customer
类的对象,为其属性分配了虚拟数据,并将其添加到列表中。 这是这段代码
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Creating object of the list.
List lst = new List();
//Creating object of the Customer class
Customer cust1 = new Customer();
//Assigning properties with Dummy data
cust1.CustomerID = 1;
cust1.CustomerName = "Customer1";
Customer cust2 = new Customer();
cust2.CustomerID = 2;
cust2.CustomerName = "Customer2";
Customer cust3 = new Customer();
cust3.CustomerID = 3;
cust3.CustomerName = "Customer3";
//Adding Customer objects in the list.
lst.Add(cust1);
lst.Add(cust2);
lst.Add(cust3);
//Assigning the list to the Grid.
GridView1.DataSource = lst;
GridView1.DataBind();
}
}
第二步是将 DropDownList
与数据绑定。 为了实现这一点,我们必须使用 GridView
的 RowDataBound
事件。 当我们将数据绑定到 GridView
时,此事件会触发。 在此事件中,我们需要找到每一行中的 DropDownList
控件并将其与一些数据绑定。 同样,为了简单起见,我通过创建一个名为“DropDownData
”的虚拟类对象并将其添加到列表中来绑定它,最后,将此列表作为数据源分配给 DropDownList
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("ddlTest");
if (ctrl != null)
{
DropDownList dd = ctrl as DropDownList;
//Binding the Dropdown with Dummy data.
List lst = new List();
DropDownData cust1 = new DropDownData(1, "One");
DropDownData cust2 = new DropDownData(2, "Two");
lst.Add(cust1);
lst.Add(cust2);
dd.DataTextField = "Text";
dd.DataValueField = "ID";
dd.DataSource = lst;
dd.DataBind();
}
}
}
第三步也是最重要的一步是处理 DropDownList
的 SelectedIndexChanged
事件。 由于我们需要识别 GridView
中触发 SelectedIndexChanged
事件的确切 Row
,因此我们需要将 DropDownList
的“ClientID
”与 GridView
中所有行中的下拉列表进行比较。 一旦 CliendID
匹配,我们就可以将 TextBox
与该 Row
的一些数据绑定。 为了简单起见,我绑定了 DropDownList
的 SelectedValue
数据。 您也可以触发数据库查询以获取真实数据。 这是代码
protected void ddlTest_SelectedIndexChanged(object sender, EventArgs e))
{
//Casting sender to Dropdown
DropDownList ddl = sender as DropDownList;
//Looping through each Gridview row to find exact Row
//of the Grid from where the SelectedIndex change event is fired.
foreach (GridViewRow row in GridView1.Rows)
{
//Finding Dropdown control
Control ctrl = row.FindControl("ddlTest") as DropDownList;
if (ctrl != null)
{
DropDownList ddl1 = (DropDownList)ctrl;
//Comparing ClientID of the dropdown with sender
if (ddl.ClientID == ddl1.ClientID)
{
//ClientID is match so find the Textbox
//control bind it with some dropdown data.
TextBox txt = row.FindControl("txtTest") as TextBox;
txt.Text = ddl1.SelectedValue;
break;
}
}
}
}
示例类
public class DropDownData
{
public DropDownData(int id, string displaytext)
{
iD = id;
text = displaytext;
}
int iD;
public int ID
{
get { return iD; }
set { iD = value; }
}
string text;
public string Text
{
get { return text; }
set { text = value; }
}
}
public class Customer
{
public int CustomerID
{
get;
set;
}
public string CustomerName
{
get;
set;
}
}
就这样了...