(50分求助)关于GridView中绑定DropDownList的问题
在一个GridView中绑定2个DropDownList,分别是ddl_A,ddl_B,都用模板列方式绑定。B的实际内容需要根据A的当前选定内容来获取,怎样将A和B关联起来呢?ddl_A的SelectedIndexChanged事件写在哪里呢?
------解决方案-------------------- <asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID= "da " runat= "server " AutoPostBack= "true " OnSelectedIndexChanged= "da_SelectedIndexChanged " >
<asp:ListItem> 1 </asp:ListItem>
<asp:ListItem> 2 </asp:ListItem>
<asp:ListItem> 3 </asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
protected void da_SelectedIndexChanged( object sender, EventArgs e )
{
string[] b = { "A ", "B ", "C " };
DropDownList da = (DropDownList)sender;
System.Web.UI.WebControls.GridViewRow dvr = (System.Web.UI.WebControls.GridViewRow)da.NamingContainer;
DropDownList db = (DropDownList)dvr.FindControl( "db ");
for (int i = 0 ; i < b.Length ; i++)
{
db.Items.Add(b[i]);
}
db.SelectedIndex = da.SelectedIndex;
}
------解决方案--------------------完整例子
<%@ Page Language= "C# " EnableViewState= "true " AutoEventWireup= "true " CodeFile= "Default.aspx.cs "
Inherits= "_Test_Default " %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns= "http://www.w3.org/1999/xhtml ">
<head runat= "server ">
<title> 无标题页 </title>
</head>
<body>
<form runat= "server ">
<asp:GridView ID= "GridView1 " runat= "server ">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID= "da " runat= "server " AutoPostBack= "true " OnSelectedIndexChanged= "da_SelectedIndexChanged ">
<asp:ListItem> 1 </asp:ListItem>
<asp:ListItem> 2 </asp:ListItem>
<asp:ListItem> 3 </asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID= "db " runat= "server ">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
</body>
</html>
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
public partial class _Test_Default : System.Web.UI.Page
{
ICollection CreateDataSource( )
{
System.Data.DataTable dt = new System.Data.DataTable();
System.Data.DataRow dr;
dt.Columns.Add(new System.Data.DataColumn( "学生班级 ", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn( "学生姓名 ", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn( "语文 ", typeof(System.Decimal)));