问题描述
datagrid中的编辑模板列中有个dropdownlist控件,并且已经绑定了数据,我想选择下拉框中某项数据时,能够触发SelectedIndexChanged事件,请问前台和后台的代码怎么写?谢谢!
解决方案
解决方案二:
在GridView的DataBound事件中初始化DropDownList的SelectedIndexChanged事件
解决方案三:
这个很麻烦的.UP
解决方案四:
引用1楼showlie的回复:
在GridView的DataBound事件中初始化DropDownList的SelectedIndexChanged事件
你说的是itemdatabound事件吗具体怎么初始化?谢谢了
解决方案五:
<asp:TemplateColumnHeaderText="发布周期"><HeaderStyleWrap="False"></HeaderStyle><ItemTemplate><asp:Labelid=Label2runat="server"Text='<%#DataBinder.Eval(Container,"DataItem.发布周期")%>'></asp:Label></ItemTemplate><EditItemTemplate><asp:DropDownListid="zqddl"runat="server"AutoPostBack="True"OnSelectedIndexChanged="zqddl_SelectedIndexChanged"></asp:DropDownList></EditItemTemplate></asp:TemplateColumn>publicvoidzqddl_SelectedIndexChanged(objectsender,System.EventArgse){DropDownListggddl=(DropDownList)this.datagrid.Items[datagrid.EditItemIndex].FindControl("zqddl");//........}
解决方案六:
如果SelectedIndexChanged只是做一个简单的查询建议用ajax这样避免datagrid的再次绑定和页面的刷新~
解决方案七:
用到FindControl方法,同时要用到事件发生源(DropDownlist)sender
解决方案八:
简单的一个事例,数据库引用northwind,主要功能是在gridview_category表中有dropdownlist模板列,其中表示产品的分类。第二个表是dropdownlist选中的分类的产品清单。具体代码如下,已通过测试,可安全使用,无毒无副作用:<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default"%><!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><headrunat="server"><title>无标题页</title></head><body><formid="form1"runat="server"><div><asp:GridViewID="GridView_category"AutoGenerateColumns="false"Width="200"runat="server"OnRowDataBound="GridView_category_RowDataBound"><Columns><asp:TemplateField><ItemTemplate><asp:DropDownListID="DropDownList_category"Width="150"AutoPostBack="true"OnSelectedIndexChanged="DropDownList_category_SelectedIndexChanged"runat="server"></asp:DropDownList></ItemTemplate></asp:TemplateField></Columns></asp:GridView><asp:GridViewID="GridView_product"AutoGenerateColumns="false"Width="200"runat="server"><Columns><asp:BoundFieldHeaderText="产品编号"DataField="ProductId"/><asp:BoundFieldHeaderText="产品名称"DataField="ProductName"/></Columns></asp:GridView> </div></form></body></html>
usingSystem;usingSystem.Data;usingSystem.Configuration;usingSystem.Web;usingSystem.Web.Security;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.WebControls.WebParts;usingSystem.Web.UI.HtmlControls;usingSystem.Data;usingSystem.Data.SqlClient;publicpartialclass_Default:System.Web.UI.Page{protectedvoidPage_Load(objectsender,EventArgse){if(!Page.IsPostBack){this.GridView_category.DataSource=GetCategory();this.GridView_category.DataBind();}}protectedvoidGridView_category_RowDataBound(objectsender,GridViewRowEventArgse){if(e.Row.RowType==DataControlRowType.DataRow){DropDownListDropDownList_category=e.Row.Cells[0].FindControl("DropDownList_category")asDropDownList;if(DropDownList_category!=null){DropDownList_category.DataValueField="CategoryId";DropDownList_category.DataTextField="CategoryName";DropDownList_category.DataSource=GetCategory();DropDownList_category.DataBind();}}}protectedvoidDropDownList_category_SelectedIndexChanged(objectsender,EventArgse){DropDownListDropDownList_category=senderasDropDownList;stringcategoryId=DropDownList_category.SelectedValue;if(!string.IsNullOrEmpty(categoryId)){this.GridView_product.DataSource=GetProduct(categoryId);this.GridView_product.DataBind();}}privateDataTableGetCategory(){stringstrConnection="DataSource=(local);InitialCatalog=Northwind;PersistSecurityInfo=True;UserID=sa";stringstrSelect="selectCategoryId,CategoryNamefromCategories";SqlDataAdapterda=newSqlDataAdapter(strSelect,strConnection);DataTabletable=newDataTable();try{da.Fill(table);}catch(Exceptione){thrownewException(e.Message);}returntable;}privateDataTableGetProduct(stringcategoryId){stringstrConnection="DataSource=(local);InitialCatalog=Northwind;PersistSecurityInfo=True;UserID=sa";stringstrSelect="selectProductId,ProductNamefromProductswhereCategoryId="+categoryId;SqlDataAdapterda=newSqlDataAdapter(strSelect,strConnection);DataTabletable=newDataTable();try{da.Fill(table);}catch(Exceptione){thrownewException(e.Message);}returntable;}}
解决方案九:
顶4楼