首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

GridView 行单选 RadioButton 如何赋值与取值?(明天上午结贴)

2012-01-31 
GridView 行单选 RadioButton 怎么赋值与取值?(明天上午结贴)GridView 行单选 RadioButton 怎么赋值与取值

GridView 行单选 RadioButton 怎么赋值与取值?(明天上午结贴)
GridView 行单选 RadioButton 怎么赋值与取值?(明天上午结贴)

数据表结构
ID主键 标题 结果
1 XXXX 赞成
2 XXX 反对  


我要生成如下格式的GridView 

标题 反对/赞成
XXXX RadioButton <---- 一行为一组RadioButton,当数据表读出来为赞成时,RadioButton 就默认选中赞成
XXX RadioButton 



还有我怎么取到RadioButton 的值。

[解决办法]
想想不难。gridview里面用模板
<>标题<> <>反对/赞成<> 这里放个隐藏域给附上值<%#Eval("xxx")%>
<>xxx<> <>[反对][赞成]<>
在gridview的RowDataBound里面
HiddenField HFcontent = e.Row.Cells[2].FindControl("HFcontent") as HiddenField;
然后在找到RadioButton rb= e.Row.Cells[2].FindControl("xxx") as RadioButton ;
然后根据HFcontent得到的值来进行给RadioButton赋值选中
 rbSelectedIndex = rb.Items.IndexOf(rb.Items.FindByValue(HFcontent.value.tostring()));
只做参考。大体思路如此
[解决办法]
楼主别忘了把答案贴上来啊,让我们也学习学习,谢谢
[解决办法]
<ItemTemplate>
<asp:HiddenField ID="hditem" runat="server" Value='<%# Eval("item") %>' />
<asp:RadioButtonList ID="rblidea" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow">
<asp:ListItem Value="1">同意</asp:ListItem>
<asp:ListItem Value="0">不同意</asp:ListItem>
</asp:RadioButtonList>
</ItemTemplate>
cs:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
int strid = e.Row.RowIndex;
RadioButtonList rbliea = (RadioButtonList)e.Row.FindControl("rblidea");

Button btnSubVise = (Button)e.Row.FindControl("btnSubVise");
HtmlInputButton hReSubVise = (HtmlInputButton)e.Row.FindControl("ReSubVise");

if (rbliea != null)
{
if (((HiddenField)e.Row.FindControl("hditem")).Value == "3")
{
//rbliea.Items.Insert(2, "需客户核准");
rbliea.Items.Insert(2, new ListItem("需客户核准", "2"));
btnSubVise.Visible = false;
hReSubVise.Visible = false;
}
}

}
[解决办法]
第一种方法:在GridView的模版列里加服务器端控件RadioButton,使用js控制单选

p>使用模版列里加RadioButton</p>
<script type="text/javascript">
function setRadio(nowRadio)
...{
var myForm,objRadio;
myForm=document.forms[0];
/**////alert(myForm);
for(var i=0;i<myForm.length;i++)
...{
if(myForm.elements[i].type=="radio")
...{
objRadio=myForm.elements[i];
/**////alert(objRadio.name);
if(objRadio!=nowRadio && objRadio.name.indexOf("GridView1")>-1 && objRadio.name.indexOf("RadioButton1")>-1)
...{
alert(objRadio.name);
if(objRadio.checked)
...{
objRadio.checked=false;


}
}
}
}
}
</script>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" ShowHeader="False" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:RadioButton ID="RadioButton1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="Button1" runat="server" Text="取选项" OnClick="Button1_Click" />
<asp:Label ID="Label1" runat="server"></asp:Label>

蓝色那段就是控制单选的js,在这里,我使用了遍历页面上所有控件的方法,加入了条件,就是红色那个判断,只控制GridView1里id是RadioButton1生成的单选按钮

这种办法需要绑定客户端事件

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//给每个RadioButton1绑定setRadio事件
try
{
((RadioButton)e.Row.FindControl("RadioButton1")).Attributes.Add("onclick", "setRadio(this)");
}
catch (Exception)
{ }
}

取值的方法就是遍历GridView的每一行,取选中的控件

protected void Button1_Click(object sender, EventArgs e)
{
//使用模版列里加RadioButton
Label1.Text = "";
foreach (GridViewRow gvr in GridView1.Rows)
{
try
{
if (((RadioButton)gvr.FindControl("RadioButton1")).Checked)
{
Label1.Text = "当前选中第" + Convert.ToString(gvr.RowIndex + 1) + "个";
break;
}
}
catch (Exception)
{ }
}
if (Label1.Text.Length == 0)
{
Label1.Text = "没有选中项";
}
}

这种方法,在客户端和服务器端都使用了遍历

[解决办法]
楼上已经说完了 - -

热点排行