有时候需要在gridviews里面嵌套一个dropdownlist,当dropdownlist改变时,把对应改的值更新到数据库,比如性别的选择,评审时的通过还是没有通过都需要用到这个,
处理办法:在dropdownlist设置属性keys,通过keys来绑定关键字,
前台代码:
<asp:GridView ID="GridView1" AutoGenerateColumns="False" Width="100%"
runat="server" AllowPaging="True"
onpageindexchanging="GridView1_PageIndexChanging"
style="margin-bottom: 0px" >
<Columns>
<asp:BoundField ItemStyle-Width="20%" DataField="C_NewsName" HeaderText="新闻标题" >
<ItemStyle Width="20%"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="C_Publisher" HeaderText="发布者" />
<asp:BoundField DataField="C_PublishTime" HeaderText="发布时间" />
<asp:BoundField DataField="S_Name" HeaderText="所属部门" />
<asp:TemplateField>
<ItemTemplate >
<asp:HyperLink ID="HyperLink1" NavigateUrl='<%# "../NewsCOntent.aspx?CID="+Eval("C_ID") %>' runat="server">查看</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="审核">
<ItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server" keys='<%# Eval("C_ID") %>'
AutoPostBack="true"
onselectedindexchanged="DropDownList2_SelectedIndexChanged1">
<asp:ListItem Value="-1">=请选择=</asp:ListItem>
<asp:ListItem Value="1">通过</asp:ListItem>
<asp:ListItem Value="2">未通过</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
后台代码:
protected void DropDownList2_SelectedIndexChanged1(object sender, EventArgs e)
{
DropDownList dr = sender as DropDownList;
int CID = Convert.ToInt32(dr.Attributes["keys"]);
int state = Convert.ToInt32(dr.SelectedValue);
if (disgnose.SetNewsState(CID, state))
{
Common.Dialog.OpenDialog(Response, "审核成功!", "NewsDignose.aspx");
}
else
{
Common.Dialog.OpenDialog(Response, "审核失败!", "NewsDignose.aspx");
}
}