原創--gridview中的drowDownList事件,能區分每個dropDownList的事件
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="order_no" HeaderText="order_no" SortExpression="order_no" ReadOnly =True />
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="ddl_province" OnSelectedIndexChanged="ddl_SelectedIndexChanged" runat="server" >
<asp:ListItem>Y</asp:ListItem>
<asp:ListItem>N</asp:ListItem>
</asp:DropDownList>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("order_no") %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
cs內的代碼:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
getlist();
}
}
private void getlist()
{
DataHelper func = new DataHelper();
DataSet ds = new DataSet();
ds = func.ExctuteDataSet("Select top 5 order_no from setBBB");
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = e.Row.FindControl("ddl_province") as DropDownList;
if (ddl != null)
{
ddl.AutoPostBack = true;
ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);
}
}
}
protected void ddl_SelectedIndexChanged(object sender, System.EventArgs e)
{
DropDownList Drp=(DropDownList)sender;
string region = Drp.SelectedItem.Value;
Response.Write(region);
DropDownList ddl = sender as DropDownList;
if (ddl != null)
{
TableCell cell = ddl.Parent as TableCell;
if (cell != null)
{
Label lbl = cell.FindControl("Label1") as Label;
Response.Write("</br>" + lbl.Text);
}
}
}

浙公网安备 33010602011771号