发布于 2016-06-02 07:43:32 | 142 次阅读 | 评论: 1 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

ASP.NET

ASP.NET 是.NET FrameWork的一部分,是一项微软公司的技术,是一种使嵌入网页中的脚本可由因特网服务器执行的服务器端脚本技术,它可以在通过HTTP请求文档时再在Web服务器上动态创建它们。 指 Active Server Pages(动态服务器页面) ,运行于 IIS(Internet Information Server 服务,是Windows开发的Web服务器)之中的程序 。


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" 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;

最新网友评论  共有(1)条评论 发布评论 返回顶部
kcsgkv 发布于2016-07-20 05:22:31
10W个赞,感谢楼主!!
支持(0)  反对(0)  回复

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务