asp.net下无法循环绑定投票的标题和选项的解决方法

问题:1,无法循环绑定投票的标题和选项
解决方法: 在Repeater绑定中添加ItemDataBound事件,选项用RadioButtonList绑定,附源代码:
Default页,源页面
复制代码 代码如下:

<div>
广大网友对保障房建设相关问题调查<br />
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
<ItemTemplate>
<table>
<tr>
<td colspan="3">
<b>
<%# Eval("t_timu")%>
<asp:Literal ID="Literal1" Text='<%# Eval("t_id")%>' runat="server"></asp:Literal>

</b>
</td>
</tr>
<tr>
<asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal">
</asp:RadioButtonList>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
<br />
<asp:Button ID="Button1" runat="server" Text="提交" />
<asp:Button ID="Button2" runat="server" Text="查看结果" />
</div>

对应的cs页:
复制代码 代码如下:

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
Literal Literal1 = (Literal)e.Item.FindControl("Literal1");
RadioButtonList RadioButtonList1 = (RadioButtonList)e.Item.FindControl("RadioButtonList1");
RadioButtonList1.DataSource = dcw_toupiao_M.dcw_toupiao_getxuanxian(Convert.ToInt32(Literal1.Text));
RadioButtonList1.DataTextField = "x_name";
RadioButtonList1.DataValueField = "x_id";
RadioButtonList1.DataBind();
}

问题2: 无法循环获得用户的选择
解决方法: 先循环Repeater控件的Item获得RadioButtonList控件,循环检测是否为选中状态,,如果是则拼接到一个字符串中,
再把题目的编号获得拼接起来,循环添加,附源代码:

Default的cs页:
复制代码 代码如下:

protected void Button1_Click(object sender, EventArgs e)
{
string zifu = "";
string Pid = "";
int tiaoshu = 5;
foreach (RepeaterItem iemt in Repeater1.Items)
{
RadioButtonList rbtn = iemt.FindControl("RadioButtonList1") as RadioButtonList;
try
{
if (rbtn.SelectedItem.Selected)
{
zifu += rbtn.SelectedItem.Value + ",";
}
Literal Literal1 = (Literal)iemt.FindControl("Literal1"); //e.Item.FindControl("");
if (Literal1.Text != "")
{
Pid += Literal1.Text + ",";
}
}
catch (Exception ex)
{
}
}
string[] xid = null;
xid = zifu.TrimEnd(',').Split(',');
string[] pid = null;
pid = Pid.TrimEnd(',').Split(',');
if (dcw_toupiao_M.dcw_toupiao_Insert(xid, pid, tiaoshu))
{
this.ClientScript.RegisterClientScriptBlock(typeof(string), "ok", "<script>alert('投票成功!谢谢参与')</script>");
}
else
{
this.ClientScript.RegisterClientScriptBlock(typeof(string), "ok", "<script>alert('请完成选择')</script>");
}
}

DAL页:
复制代码 代码如下:

public static bool dcw_toupiao_Insert(string[] xid, string[] pid, int tiaoshu)
{
bool flag = false;
for (int i = 0; i < pid.Length; i++)
{
SqlParameter[] prm = new SqlParameter[2];
prm[0] = new SqlParameter("@xid", Int32.Parse(xid[i]));
prm[1] = new SqlParameter("@pid", Int32.Parse(pid[i]));
if (dcw_toupiao_M.dcw_toupiao_gettcount(Convert.ToInt32(xid[i]), Convert.ToInt32(pid[i])))
{
flag = _dc_toupiao_DB.SqlHelper.ExeucteNonQuery("sm_dcw_toupiao_Insert", CommandType.StoredProcedure, prm) > 0;
}
}

return flag;
}

所掌握的技巧:
JavaScript跳转:
this.ClientScript.RegisterClientScriptBlock(typeof(string), "ok", "<script>alert('投票成功!谢谢参与')</script>");
两种获得控件的方法:
Literal Literal1 = (Literal)e.Item.FindControl("Literal1");
Literal Literal1 = e.Item.FindControl("Literal1") as Literal;

时间: 2024-08-03 19:37:01

asp.net下无法循环绑定投票的标题和选项的解决方法的相关文章

asp.net下无法循环绑定投票的标题和选项的解决方法_实用技巧

问题:1,无法循环绑定投票的标题和选项 解决方法: 在Repeater绑定中添加ItemDataBound事件,选项用RadioButtonList绑定,附源代码: Default页,源页面 复制代码 代码如下: <div> 广大网友对保障房建设相关问题调查<br /> <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemData

IE 下Enter提交表单存在重复提交问题的解决方法

  这篇文章主要介绍了IE 下Enter提交表单存在重复提交问题的解决方法,需要的朋友可以参考下 在submit()后加个return false就可以了.如: 代码如下: document.formname.submit(); return false; 以后要注意了 后面尝试了另一种方法:   代码如下: if ($("#formid").validationEngine("validate")){ document.getElementById("f

win7系统下浏览器主页被金山毒霸导航劫持的两种解决方法

  一般情况下,win7操作系统自带有IE浏览器,用户可直接打开浏览器观看视频或搜索网页内容等等.有的时候会发生浏览器主页被劫持的现象,一位用户说win7 32位系统明明打开浏览器主页却变成毒霸导航的网址,为什么会出现这样的情况呢?主要原因就是你安装了金山毒霸,而毒霸的"新保护功能"把你的主页劫持了.接下去,本教程小编介绍两种解决方法,大家可以往下学习. 解决方法: 方法一.卸载金山毒霸,这个不用教了吧. 方法二. 1.在毒霸设置页面,安全保护设置-上网保护; 2.点击上网保护,取消安

Linux 下shell显示-bash-4.1$不显示用户名路径的解决方法

  Linux CentOS下shell显示-bash-4.1$不显示用户名路径的解决方法   问题描述:   CentOS下新增一个用户,登录进去之后shell脚本的信息如下:     而不是我们经常看到的username@hostname的组合,看起来很不舒服.   解决方案:   问题的原因是因为没有配置.bash_profile的问题,可以通过配置这个文件来解决问题.   1.在Terminal输入: vi ~/.bash_profile     2.如果没有.bash_profile可

ios-MAC 终端下怎么设置代理 (折腾一天了,找到解决方法)

问题描述 MAC 终端下怎么设置代理 (折腾一天了,找到解决方法) 在MAC的终端下,执行curl获取ruby 包 curl -L https://get.rvm.io | bash -s stable % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0 cur

ASP.NET下根据QueryString决定使用哪块&amp;#106avascript的两种方法 :)

asp.net 在asp下,有时候会使用下面的代码: <%if request("m") = "1" then%><script language="javascript"><!--m_name[0] = "<财经>杂志 ";//m_period[0] = 6;init(1, 1, 1);//--></script><%else%><script l

php下foreach提示Warning:Invalid argument supplied for foreach()的解决方法_php技巧

本文实例讲述了php下foreach()错误提示Warning: Invalid argument supplied for foreach() 的解决方法.分享给大家供大家参考.具体实现方法如下: 一.问题: php下foreach()错误提示Warning: Invalid argument supplied for foreach() 错误提示:Warning: Invalid argument supplied for foreach() in E:wampwwwmyshopcart.p

在Windows2003中ASP出现(错误“不允许的父路径 ”)问题的解决方法

window|windows2003|错误|解决|问题 配置好IIS6.0,然后也成功导入SQL2000的数据库,输入地址访问Index.asp页面之后出现如下出错信息: Active Server Pages 错误 'ASP 0131' 不允许的父路径 /xxx/xxx/index.asp,行 1 解决方法如下: 1.运行Internet信息服务管理器 2.选择你当前所在站点 3.从站点"属性"中选择"虚拟目录",再进入"配置",最后进入&qu

win8下XAMPP中Apache模块无效(apache无法打开)的解决方法

win8下系统默认占用80端口,导致apache无法打开,下面是具体的解决方法:首先以管理员权限运行c:windowssystem32cmd.exe.....感兴趣的朋友可以参考下哈,希望对大家有所帮助   win8下系统默认占用80端口,导致apache无法打开. 以管理员权限运行c:windowssystem32cmd.exe C:WINDOWSsystem32>net stop http HTTP Service 服务已成功停止. C:WINDOWSsystem32>netstat -a