ASP.NET GridView模板控件问题

最最不静 2011-02-23 01:43:55
//这个是一个按钮控件的click事件代码
protected void BtnSumit_Click(object sender, EventArgs e)
{

foreach (GridViewRow row in Gridview1.Rows)
{

if (row.RowType == DataControlRowType.DataRow)
{

string Grade = ((RadioButtonList)row.FindControl("RadList1")).SelectedValue;

}
}
}

在我的gridview中自定义了radioButtonList控件:
<ItemTemplate>
<asp:RadioButtonList ID="RadList1" runat="server"
RepeatDirection="Horizontal" AppendDataBoundItems="True">
<asp:ListItem Value="A">A</asp:ListItem>
<asp:ListItem Value="B">B</asp:ListItem>
<asp:ListItem Value="C">C</asp:ListItem>
<asp:ListItem Value="D">D</asp:ListItem>
</asp:RadioButtonList>
</ItemTemplate>


出现的问题:在网页Gridview1中点击RadioButtonList控件,然后点击提交按钮,响应按钮事件时,使用FindControl是可以找得到RadioButtonList控件的,但是string Grade = ((RadioButtonList)row.FindControl("RadList1")).SelectedValue;
这句代码Grade的值却为空。求各路大侠指点!!!!!!
...全文
94 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
ycmail 2011-02-23
  • 打赏
  • 举报
回复
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in this.GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
RadioButtonList button = ((RadioButtonList)row.FindControl("RadioButtonList"));
ListItem item = button.SelectedItem;
string value = item.Value;
Response.Write("<script>alert('" + value + "');</script>");
}
}
}
最最不静 2011-02-23
  • 打赏
  • 举报
回复
问题解决了,是页面回发的问题。在页面回发时在Page_Load事件中Gridview又一次绑定数据,所以RadioButtonList会被重置,值就相当于没有了。

谢谢大家了!
zuzuk 2011-02-23
  • 打赏
  • 举报
回复
是页面回发次数过多了吧?
笑道江湖情 2011-02-23
  • 打赏
  • 举报
回复
 protected void Button1_Click(object sender, EventArgs e)
{
string selectValue = string.Empty;
foreach (GridViewRow item in this.gvList.Rows)
{
RadioButtonList rList = (RadioButtonList)item.FindControl("RadList1");
if (rList.SelectedValue != string.Empty)
{
selectValue += rList.SelectedValue + ";";
}
}
//输出每个RadioButtonList选择的值
Response.Write(selectValue);
}
子夜__ 2011-02-23
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 fchzzwcsr201 的回复:]

引用 2 楼 huminghua 的回复:
为什么要这样写呢?this.RadList1.selectedItem[0]这样不行吗?

我要的是获取我在RadList1上选择的内容,这样只是获取了RadList1的第一项
[/Quote]

写了个 你改成这样看看

protected void BtnSumit_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in Gridview1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
RadioButtonList rbs = ((RadioButtonList)row.FindControl("RadList1"));
for (int i = 0; i < rbs.Items.Count; i++)
{
if (rbs.Items[i].Selected == true)
{
Response.Write(rbs.Items[i].Text)//or Value
}
}
}
}
}
最最不静 2011-02-23
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 huminghua 的回复:]
为什么要这样写呢?this.RadList1.selectedItem[0]这样不行吗?
[/Quote]
我要的是获取我在RadList1上选择的内容,这样只是获取了RadList1的第一项
huminghua 2011-02-23
  • 打赏
  • 举报
回复
为什么要这样写呢?this.RadList1.selectedItem[0]这样不行吗?

62,025

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧