gridview中使用RadioButtonList的问题 5
GridView里面含有RadioButtonList,当我点击GridView编辑时,会读取RadioButtonList所选择的项,怎么做出来?下面是GridView...
GridView 里面含有RadioButtonList,当我点击GridView编辑时,会读取RadioButtonList所选择的项,怎么做出来?
下面是GridView 里面含有RadioButtonList的片段代码。
<asp:TemplateField HeaderText="性别" SortExpression="Sex">
<EditItemTemplate>
<asp:RadioButtonList ID="rblSex" runat="server" RepeatDirection="Horizontal" >
<asp:ListItem Value="True" >男</asp:ListItem>
<asp:ListItem Value="False">女</asp:ListItem>
</asp:RadioButtonList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("Sex") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField> 展开
下面是GridView 里面含有RadioButtonList的片段代码。
<asp:TemplateField HeaderText="性别" SortExpression="Sex">
<EditItemTemplate>
<asp:RadioButtonList ID="rblSex" runat="server" RepeatDirection="Horizontal" >
<asp:ListItem Value="True" >男</asp:ListItem>
<asp:ListItem Value="False">女</asp:ListItem>
</asp:RadioButtonList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("Sex") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField> 展开
展开全部
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
string sex = (GridView1.Rows[e.NewEditIndex].FindControl("Label3") as Label).Text.Trim();
GridView1.EditIndex = e.NewEditIndex;
//为GridView1重新绑定数据;
RadioButtonList rbl = GridView1.Rows[e.NewEditIndex].FindControl("rblSex") as RadioButtonList;
rbl.Items[0].Selected = sex == "男" ? true : false;
rbl.Items[1].Selected = sex == "女" ? true : false;
}
{
string sex = (GridView1.Rows[e.NewEditIndex].FindControl("Label3") as Label).Text.Trim();
GridView1.EditIndex = e.NewEditIndex;
//为GridView1重新绑定数据;
RadioButtonList rbl = GridView1.Rows[e.NewEditIndex].FindControl("rblSex") as RadioButtonList;
rbl.Items[0].Selected = sex == "男" ? true : false;
rbl.Items[1].Selected = sex == "女" ? true : false;
}
追问
谢谢你,在你写的代码里,为啥我在点编辑时 rlb 为null的?
追答
//为GridView1重新绑定数据;
这个你要替换你给GridView绑定数据的方法
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询