如何在后台通过调用控件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: }先谢谢了~~~~~~
前台代码,如下:
<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: }先谢谢了~~~~~~
解决方案 »
- 想做个支付系统 能把大部分的支付平台放进去
- gridview 动态增加数据行 的问题
- 各位高手这段代码为啥不能显示图片老出错了
- 非常急!关于“codebehind"
- 请问大虾:asp.net程序怎么能转换成asp程序?
- 上传文件问题,很急,在线等
- 初学asp.net,请求帮助
- ajax & page_load & Response.Write & Sys.WebForms.PageRequestManagerParserErrorException
- 各位老大,思归,班兄来帮帮忙啊,已经有两位斑竹看过了,还是没有解决!!解决了200分相送!解决不了我就不能回家过年了!!
- checkboxlist 中每项分别与数据表中每字段数据绑定问题!!
- JS判断RequiredFieldValidator控件的问题
- wap C#开发中如何把汉字转成 unicode
上面代码有以下问题: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;
}
rdl = (RadioButtonList)Page.FindControl("RadioButtonLis" + i + "");
应该是RadioButtonList”+i+“”
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: }
那个rbl ,我改过来了,不是那个错误!
rbl没有定义:RadioButtonList rdl = (RadioButtonList)Form.FindControl("RadioButtonList" + i + "");