asp.net 控件查找

sliven2008 2009-12-07 12:02:22
如何在后台通过调用控件ID ,获取所对应的值:
前台代码,如下:
<ul class="ul_1">
<li>
<asp:RadioButtonList runat="server" ID="RadioButtonLis1" RepeatColumns="5" RepeatLayout="Table">
<asp:ListItem Text="5分" Value="5"></asp:ListItem>
<asp:ListItem Text="4分" Value="4"></asp:ListItem>
<asp:ListItem Text="3分" Value="3"></asp:ListItem>
<asp:ListItem Text="2分" Value="2"></asp:ListItem>
<asp:ListItem Text="1分" Value="1"></asp:ListItem>
</asp:RadioButtonList></li>
<li>
<asp:RadioButtonList runat="server" ID="RadioButtonList1" RepeatColumns="5" RepeatLayout="Table">
<asp:ListItem Text="5分" Value="5"></asp:ListItem>
<asp:ListItem Text="4分" Value="4"></asp:ListItem>
<asp:ListItem Text="3分" Value="3"></asp:ListItem>
<asp:ListItem Text="2分" Value="2"></asp:ListItem>
<asp:ListItem Text="1分" Value="1"></asp:ListItem>
</asp:RadioButtonList></li>
<li>
<asp:RadioButtonList runat="server" ID="RadioButtonList2" RepeatColumns="5" RepeatLayout="Table">
<asp:ListItem Text="5分" Value="5"></asp:ListItem>
<asp:ListItem Text="4分" Value="4"></asp:ListItem>
<asp:ListItem Text="3分" Value="3"></asp:ListItem>
<asp:ListItem Text="2分" Value="2"></asp:ListItem>
<asp:ListItem Text="1分" Value="1"></asp:ListItem>
</asp:RadioButtonList></li>
<li>
<asp:RadioButtonList runat="server" ID="RadioButtonList3" RepeatColumns="5" RepeatLayout="Table">
<asp:ListItem Text="5分" Value="5"></asp:ListItem>
<asp:ListItem Text="4分" Value="4"></asp:ListItem>
<asp:ListItem Text="3分" Value="3"></asp:ListItem>
<asp:ListItem Text="2分" Value="2"></asp:ListItem>
<asp:ListItem Text="1分" Value="1"></asp:ListItem>
</asp:RadioButtonList></li>
<li>
<asp:RadioButtonList runat="server" ID="RadioButtonList4" RepeatColumns="5" RepeatLayout="Table">
<asp:ListItem Text="5分" Value="5"></asp:ListItem>
<asp:ListItem Text="4分" Value="4"></asp:ListItem>
<asp:ListItem Text="3分" Value="3"></asp:ListItem>
<asp:ListItem Text="2分" Value="2"></asp:ListItem>
<asp:ListItem Text="1分" Value="1"></asp:ListItem>
</asp:RadioButtonList></li>
</ul>

后台代码如下:
public void button_click(object sender, EventArgs e)
{

string[] rdl1 = new string[4];
for (int i = 1; i < 5; i++)
{
RadioButtonList rdl = new RadioButtonList();
rdl = (RadioButtonList)Page.FindControl("RadioButtonLis" + i + "");
rdl1[i] = rdl.SelectedValue;
}
}

错误信息如下:

行 45: rdl.ID = "RadioButtonList_"+i;
行 46: rdl = (RadioButtonList)Page.FindControl("RadioButtonLis" + i + "");
行 47: rdl1[i] = rdl.SelectedValue;
行 48: }

先谢谢了~~~~~~
...全文
151 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
mngzilin 2009-12-07
  • 打赏
  • 举报
回复

上面代码有以下问题:

1.前台代码中空间id重复了RadioButtonList1,应该是从RadioButtonLis1到RadioButtonLis5
2.这句rdl = (RadioButtonList)Page.FindControl("RadioButtonLis" + i + ""); 中
RadioButtonLis少了个t,应该是“RadioButtonList”+i+“”
3.for循环中i要从0到到5,
4.数组rdl1的大小是5,即string[] rdl1 = new string[5];

下面是修改后的部分代码:
string[] rdl1 = new string[5];
for (int i = 0; i < 5; i++)
{
RadioButtonList rdl = new RadioButtonList();
rdl = (RadioButtonList)Page.FindControl("RadioButtonList" + i + "");
rdl1[i] = rdl.SelectedValue;
}
红街咖啡 2009-12-07
  • 打赏
  • 举报
回复
rdl = (RadioButtonList)Page.FindControl("RadioButtonList" + i + "");
PandaIT 2009-12-07
  • 打赏
  • 举报
回复
rdl = (RadioButtonList)Page.FindControl("RadioButtonLis" + i + "");
c_sharp_Rookie 2009-12-07
  • 打赏
  • 举报
回复
两个RadioButtonLis1 不会报错嘛?
rdl = (RadioButtonList)Page.FindControl("RadioButtonLis" + i + "");
应该是RadioButtonList”+i+“”
mngzilin 2009-12-07
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 sliven2008 的回复:]
接上说明下,
  那个rbl ,我改过来了,不是那个错误!
[/Quote]

rbl没有定义:

RadioButtonList rdl = (RadioButtonList)Form.FindControl("RadioButtonList" + i + "");
wuyq11 2009-12-07
  • 打赏
  • 举报
回复
rdl_1[i] = rbl.SelectedValue;
sliven2008 2009-12-07
  • 打赏
  • 举报
回复
接上说明下,
那个rbl ,我改过来了,不是那个错误!
sliven2008 2009-12-07
  • 打赏
  • 举报
回复
前台那个控件ID已改,没有重复了,后台更改后的代码如下:
public void button_click(object sender, EventArgs e)
{
RadioButtonList rbl = new RadioButtonList();
string[] rdl_1 = new string[5];
for (int i = 0; i < 5; i++)
{
rbl = (RadioButtonList) form1.FindControl("RadioButtonList" + i+"");
rdl_1[i] = rdl.SelectedValue;
}
}

调试后错误信息如下:
编译器错误信息: CS0103: 当前上下文中不存在名称“rdl”

源错误:



行 42: {
行 43: rbl = (RadioButtonList) form1.FindControl("RadioButtonList" + i);
行 44: rdl_1[i] = rdl.SelectedValue;
行 45: }
行 46: }

tkscascor 2009-12-07
  • 打赏
  • 举报
回复
RadioButtonList少了个t!!!
n109214114 2009-12-07
  • 打赏
  • 举报
回复
up.........
sliven2008 2009-12-07
  • 打赏
  • 举报
回复
先谢谢各位了,我改下! 再试试 呵呵
amandag 2009-12-07
  • 打赏
  • 举报
回复
同三楼

请将Page.FindControl改为Form.FindControl
zhengweitao 2009-12-07
  • 打赏
  • 举报
回复
不会 UP

62,254

社区成员

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

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

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

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