模板列里两DropDownList dd1,dd2
dd1的selectchanged事件 假设为fff
pub void fff(object sender, System.EventArgs e)
{
DropDownList ddl = (DropDownList)sender;//找到自身
TableCell cell = (TableCell)ddl.Parent;
DataGridItem item = (DataGridItem)cell.Parent;//找到DataGridItem
DropDownList DD_s=(DropDownList)item.FindControl("dd2");//寻找模板列另一个DropDownList
//对其绑定...........
}
dd1的selectchanged事件 假设为fff
pub void fff(object sender, System.EventArgs e)
{
DropDownList ddl = (DropDownList)sender;//找到自身
TableCell cell = (TableCell)ddl.Parent;
DataGridItem item = (DataGridItem)cell.Parent;//找到DataGridItem
DropDownList DD_s=(DropDownList)item.FindControl("dd2");//寻找模板列另一个DropDownList
//对其绑定...........
}
{
DropDownList ddl = (DropDownList)sender;//找到自身
TableCell cell = (TableCell)ddl.Parent;
DataGridItem item = (DataGridItem)cell.Parent;//找到DataGridItem
DropDownList DD_s=(DropDownList)item.FindControl("dd2");//寻找模板列另一个DropDownList
//对其绑定...........
}
1.给模板中的第一个DropDownList添加事件
在DataGrid的ItemCreat或者ItemDataBound事件中
DropDownList ddl1=(DropDownList)e.Item.Cells[n].FindControl("DropDownList1");
ddl1.SelectedIndexChanged+=new EventHandler(ddl_SelectedIndexChanged);2.所有的第一列的DropDownList都调用下面的方法
private void ddl_SelectedIndexChanged(object sender,EventArgs e)
{
DropDownList d=(DropDownList)sender;
//添加代码由d的UniqueID 解出该控件所在的DataGrid的行为i
DropDownList d2=(DropDownList)DataGrid1.Items[i].Cells[n].FindControl("DropDownList2的ID");
//下面进行关联操作。
}
下面是我的代码
<?xml version="1.0" encoding="utf-8" ?>
<china>
<state id="1" name="南宁">
<area>新城</area>
<area>永新 </area>
<area>城北</area>
<area>江南</area>
<area>市效区</area>
<area>武鸣</area>
</state>
<state id="2" name="桂林">
<area>秀峰区</area>
<area>叠彩区</area>
<area>象山区</area>
<area>七星区</area>
<area>西城区</area>
</state>
<state id="3" name="合肥">
<area>中市</area>
<area>东市</area>
<area>西市</area>
<area>长丰</area>
<area>肥东</area>
</state>
</china>private void Form1_Load(object sender, System.EventArgs e)
{
XmlNodeList book;
XmlDocument doc=new XmlDocument();
doc.Load(filename);
XmlElement root = doc.DocumentElement;
book=root.SelectNodes("//@name");
string[] str=new string[book.Count];
for(int i=0;i<book.Count;i++)
{
str[i]=book.Item(i).InnerText.ToString();
}
DropDownList1.DataSource=str;
DropDownList1.DataBind();
}private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
XmlNodeList book;
XmlDocument doc=new XmlDocument();
doc.Load(filename);
string y=DropDownList1.SelectedItem.ToString();
XmlElement root = doc.DocumentElement;
book=root.SelectNodes("//state[@name='"+y+"']/descendant::area");
string[] str=new string[book.Count];
for(int i=0;i<book.Count;i++)
{
str[i]=book.Item(i).InnerText.ToString();
}
DropDownList2.DataSource=str;
DropDownList2.DataBind();
}