关于DataGrid1_ItemDataBound事件 DataGrid1里面有二个模板列,放二个DropDownList,分别是DropDownList1和DropDownList2我想选择DropDownList1中的一个值,同时能在DropDownList2中出现DropDownList1对应的值 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 DropDownList ddl1=(DropDownList)e.Items.FindControl("ddl1");DropDownList ddl2=(DropDownList)e.Items.FindControl("ddl2");写ddl1的SelectIndexChange事件让ddl2.SelectIndex等于ddl1.SelectIndex 在ItemdataBound事件里面写上:(DropDownList)e.Item.FindControl(DropDownList2).SelectValue = (DropDownList)e.Item.FindControl(DropDownList1).SelectValue;具体的你调试一下 DropDownList模板列没有事件可以触发, if(e.Item.ItemType==System.Web.UI.WebControls.ListItemType.EditItem) { System.Data.SqlClient.SqlDataAdapter mycmd =new System.Data.SqlClient.SqlDataAdapter("select ssx from am_ssx_tab order by ssx",myconn); DataSet ds =new DataSet(); mycmd.Fill(ds,"ssx"); ((DropDownList)e.Item.FindControl("temp_ssx")).DataTextField="ssx"; ((DropDownList)e.Item.FindControl("temp_ssx")).DataValueField="ssx"; ((DropDownList)e.Item.FindControl("temp_ssx")).DataSource=ds.Tables["ssx"]; ((DropDownList)e.Item.FindControl("temp_ssx")).DataBind();//这是第一个下拉框 System.Data.SqlClient.SqlDataAdapter mycmd1 =new System.Data.SqlClient.SqlDataAdapter("select xian from am_ssx_tab where ssx='"+((DropDownList)e.Item.FindControl("temp_ssx")).SelectedItem.Text+"' order by ssx",myconn); DataSet ds1 =new DataSet(); mycmd1.Fill(ds1,"xian"); ((DropDownList)e.Item.FindControl("temp_xian")).DataTextField="xian"; ((DropDownList)e.Item.FindControl("temp_xian")).DataValueField="xian"; ((DropDownList)e.Item.FindControl("temp_xian")).DataSource=ds1.Tables["xian"]; ((DropDownList)e.Item.FindControl("temp_xian")).DataBind(); }//这是第二个下拉框,内容是当选择第一个框中内容时,第二个中的内容要跟着变化,我这样写只是页面起来时可以出来,也只是第一个框的对应的内容,当选择第一个框中内容时,第二个不会变了。 动态给DropDownList1绑定事件DropDownList DropDownList1=(DropDownList)e.Items.FindControl("ddl1");DropDownList1.AutoPostBack = true;DropDownList1.SelectedIndexChanged += new System.EventHandler(SelectedIndexChanged);SelectedIndexChanged为根据DropDownList1选项绑定DropDownList2值的函数;类似于private void SelectedIndexChanged(object sender, System.EventArgs e){ ...} erwanfan(teddyxiong) :你好,我按你的意思操作了,可还是不行,当改变temp_ssx中内容时,不出运行那个selechange(),是不是temp_ssx和DropDownList1是两个不同的对象?================== private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) { if(e.Item.ItemType==System.Web.UI.WebControls.ListItemType.EditItem) { System.Data.SqlClient.SqlDataAdapter mycmd =new System.Data.SqlClient.SqlDataAdapter("select ssx from am_ssx_tab order by ssx",myconn); DataSet ds =new DataSet(); mycmd.Fill(ds,"ssx"); ((DropDownList)e.Item.FindControl("temp_ssx")).DataTextField="ssx"; ((DropDownList)e.Item.FindControl("temp_ssx")).DataValueField="ssx"; ((DropDownList)e.Item.FindControl("temp_ssx")).DataSource=ds.Tables["ssx"]; ((DropDownList)e.Item.FindControl("temp_ssx")).DataBind(); DropDownList DropDownList1=(DropDownList)e.Item.FindControl("temp_ssx"); DropDownList1.AutoPostBack = true; DropDownList1.SelectedIndexChanged += new System.EventHandler(selechanged); } } private void selechanged(object sender, System.EventArgs e) { Response.Write("<script language='javascript'>alert('aaa!')</script>"); } DropDownList2绑定DropDownList1为数据源就可以了! 绑定DropDownList1的SelectedIndexChanged以后,改变选项时激发了postback了么?如果激发了,则有可能你的DataGrid的数据邦定放在 Not IsPostBack 判定块之外,导致每次点击执行Page_Load的时候,重新邦定了DataGrid,这样会丢的checkbox的选中信息。如果没有激发,你调试一下,看看是否绑定了DropDownList1的动作。 erwanfan(teddyxiong) :private void Page_Load(object sender, System.EventArgs e){if (!Page.IsPostBack) { }}DropDownList1的postbace激发了,我在页面开始时用的上面的代码,是否可以? 不难! 你还可以用 javascrpit 来实现 javascrpit 不清楚怎么弄,能不能给出些具体实现的代码么,我都整了两天了,还没结果。 选择DropDownList1的下拉内容,就是不能触发他绑定的事件, 我测试了,这样子写肯定是可以触发selechanged的,你试试<asp:TemplateColumn HeaderText="dd1"> <ItemTemplate> <asp:DropDownList ID="dd1" Runat="server" AutoPostBack="True" OnSelectedIndexChanged="selechanged"> <asp:ListItem>1</asp:ListItem> <asp:ListItem>2</asp:ListItem> <asp:ListItem>3</asp:ListItem> </asp:DropDownList> </ItemTemplate></asp:TemplateColumn> 【请教】aps.net中如何操作EXCEL合并单元格? 如何实现百万条数据的分页? IE7浏览器多窗口与session问题 二问:二进制文件的显示问题 新建站点部署treeview的问题,救命啊 .net安装问题 怎么做到这一点,让CLICK在LOAD前执行 【【【【【【 DataList里的DataGrid的绑定问题!!!】】】】】】 上传更改的页面到空间就会空间访问不了。空间商向我要啥.net插件。 有人知道下载时候,如何正确设置提示信息,关于Response.AppendHeader()!!! ASP.NET 定时器的问题 很多页面如何共用一个头
DropDownList ddl2=(DropDownList)e.Items.FindControl("ddl2");
写ddl1的SelectIndexChange事件让ddl2.SelectIndex等于ddl1.SelectIndex
(DropDownList)e.Item.FindControl(DropDownList2).SelectValue = (DropDownList)e.Item.FindControl(DropDownList1).SelectValue;具体的你调试一下
{ System.Data.SqlClient.SqlDataAdapter mycmd =new System.Data.SqlClient.SqlDataAdapter("select ssx from am_ssx_tab order by ssx",myconn);
DataSet ds =new DataSet();
mycmd.Fill(ds,"ssx");
((DropDownList)e.Item.FindControl("temp_ssx")).DataTextField="ssx";
((DropDownList)e.Item.FindControl("temp_ssx")).DataValueField="ssx";
((DropDownList)e.Item.FindControl("temp_ssx")).DataSource=ds.Tables["ssx"];
((DropDownList)e.Item.FindControl("temp_ssx")).DataBind();
//这是第一个下拉框
System.Data.SqlClient.SqlDataAdapter mycmd1 =new System.Data.SqlClient.SqlDataAdapter("select xian from am_ssx_tab where ssx='"+((DropDownList)e.Item.FindControl("temp_ssx")).SelectedItem.Text+"' order by ssx",myconn);
DataSet ds1 =new DataSet();
mycmd1.Fill(ds1,"xian");
((DropDownList)e.Item.FindControl("temp_xian")).DataTextField="xian";
((DropDownList)e.Item.FindControl("temp_xian")).DataValueField="xian";
((DropDownList)e.Item.FindControl("temp_xian")).DataSource=ds1.Tables["xian"];
((DropDownList)e.Item.FindControl("temp_xian")).DataBind();
}
//这是第二个下拉框,内容是当选择第一个框中内容时,第二个中的内容要跟着变化,
我这样写只是页面起来时可以出来,也只是第一个框的对应的内容,当选择第一个框中内容时,第二个不会变了。
DropDownList DropDownList1=(DropDownList)e.Items.FindControl("ddl1");
DropDownList1.AutoPostBack = true;
DropDownList1.SelectedIndexChanged += new System.EventHandler(SelectedIndexChanged);
SelectedIndexChanged为根据DropDownList1选项绑定DropDownList2值的函数;类似于
private void SelectedIndexChanged(object sender, System.EventArgs e)
{
...
}
==================
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{ if(e.Item.ItemType==System.Web.UI.WebControls.ListItemType.EditItem)
{ System.Data.SqlClient.SqlDataAdapter mycmd =new System.Data.SqlClient.SqlDataAdapter("select ssx from am_ssx_tab order by ssx",myconn);
DataSet ds =new DataSet();
mycmd.Fill(ds,"ssx");
((DropDownList)e.Item.FindControl("temp_ssx")).DataTextField="ssx";
((DropDownList)e.Item.FindControl("temp_ssx")).DataValueField="ssx";
((DropDownList)e.Item.FindControl("temp_ssx")).DataSource=ds.Tables["ssx"];
((DropDownList)e.Item.FindControl("temp_ssx")).DataBind();
DropDownList DropDownList1=(DropDownList)e.Item.FindControl("temp_ssx");
DropDownList1.AutoPostBack = true; DropDownList1.SelectedIndexChanged += new System.EventHandler(selechanged); }
}
private void selechanged(object sender, System.EventArgs e)
{
Response.Write("<script language='javascript'>alert('aaa!')</script>"); }
如果没有激发,你调试一下,看看是否绑定了DropDownList1的动作。
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
}
}
DropDownList1的postbace激发了,我在页面开始时用的上面的代码,是否可以?
能不能给出些具体实现的代码么,
我都整了两天了,还没结果。
<asp:TemplateColumn HeaderText="dd1">
<ItemTemplate>
<asp:DropDownList ID="dd1" Runat="server" AutoPostBack="True" OnSelectedIndexChanged="selechanged">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>