asp.net下无法循环绑定投票的标题和选项的解决方法
程序员文章站
2024-03-07 10:11:32
问题:1,无法循环绑定投票的标题和选项 解决方法: 在repeater绑定中添加itemdatabound事件,选项用radiobuttonlist绑定,附源代码: def...
问题: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" onclick="button1_click" text="提交" />
<asp:button id="button2" runat="server" text="查看结果" onclick="button2_click" />
</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;
解决方法: 在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" onclick="button1_click" text="提交" />
<asp:button id="button2" runat="server" text="查看结果" onclick="button2_click" />
</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;