怎样在GridView中的DropDownList选项改变的时候获取GridView中的DataKeys.Value的二种方法:...

【1】
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="ID" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true"
onselectedindexchanged="DropDownList1_SelectedIndexChanged" >
<asp:ListItem Text="选项1" Value="1001" Selected="False" ></asp:ListItem>
<asp:ListItem Text="选项2" Value="1002" Selected="False"></asp:ListItem>
<asp:ListItem Text="选项3" Value="1003" Selected="False"></asp:ListItem>
<asp:ListItem Text="选项4" Value="0" Selected="False"></asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField ShowHeader="true" DataField="ID" HeaderText="ID" />
<asp:BoundField ShowHeader="true" DataField="name" HeaderText="姓名" />
</Columns>
</asp:GridView>


protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList DropDownList1 = sender as DropDownList;
int index = (DropDownList1.NamingContainer as GridViewRow).RowIndex;
string id = GridView1.DataKeys[index].Value.ToString();
/
4   DropDownList drp = sender as DropDownList; // 触发事件的 DropDownList
5    GridViewRow row = drp.NamingContainer as GridViewRow; // GridView 当前行 即时在dropdownlist所在容器里 就是行的信息    
6    row.Style.Add(HtmlTextWriterStyle.BackgroundColor, drp.SelectedValue);
7    Response.Write(row.RowIndex+1);//获取dropdownlist中选定行的行号.
8        //Response.Write(String.Format("选中第 {0} 行", row.RowIndex + 1));
9     DropDownList ddlClass = (DropDownList)sender;
10     string ClassID = ddlClass.SelectedValue.ToString();//获取Dropdownlist中选定值

}

【2】

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" ABCD='<%# Eval("primary_field") %>'>

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList dr = sender as DropDownList;
string keyValue=dr.Attributes["ABCD"];
//根据keyValue进行处理。
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值