GridView模版列有个DropDownList,DropDownList绑定数据,DropDownList触发事件如何取它选取的值
GridView模版列有个DropDownList,这个DropDownList绑定数据,然后触发OnSelectedIndexChanged事件如何取它选取的值
<ItemTemplate>
<asp:DropDownList ID= "DropDownList1 " runat= "server " AutoPostBack= "True " DataSourceID= "SqlDataSource2 " DataTextField= "LinkName " DataValueField= "LinkUrl " OnSelectedIndexChanged= "DropDownList1_SelectedIndexChanged ">
</asp:DropDownList>
<SelectParameters>
<asp:ControlParameter ControlID= "Label1 " Name= "LinkClassID " PropertyName= "Text " Type= "Int32 " />
</SelectParameters>
</asp:SqlDataSource>
</ItemTemplate>
在GridView模版列编辑状态时,双击DropDownList1创建事件:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Redirect( "you select : ");//输出取到DropDownList1下拉诓的值
}
但问题时如何取到这个值呢?
我刚开始用这个,GridView在创建行的时候能取到每行的DropDownList1对象,然后把DropDownList1对象的SelectedIndexChanged事件绑定到相应的处理函数,但是这里的 ddl.SelectedValue如何能传到处理函数呢.. :
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
DropDownList ddl = (DropDownList)e.Row.FindControl( "DropDownList1 ");
ddl.SelectedIndexChanged += new EventHandler(this.DropDownList1_SelectedIndexChanged);
}
小弟叩首,望各位达人指点...
------解决方案--------------------如果你在DataGrid的列模板里绑定了DropDownList,实际上是产生了一组DropDownList(有多少行就有多少个),它们的ID是有规律的,可以通过控件ID区分,也可以通过编程方式由你自己去分配ID。两个方法中都可以获得行数(在控件ID尾)。
------解决方案--------------------DropDownList ddl = (DropDownList)sender;
string test = ddl.SelectedValue;
Response.Redirect(sting.Format( "you select :{0} ",test);//输出取到DropDownList1下拉诓的值