asp.net下無法循環(huán)綁定投票的標(biāo)題和選項(xiàng)的解決方法
更新時(shí)間:2010年12月06日 20:45:25 作者:
asp.net下無法循環(huán)綁定投票的標(biāo)題和選項(xiàng)與無法循環(huán)獲得用戶的選擇的解決方法。
問題:1,無法循環(huán)綁定投票的標(biāo)題和選項(xiàng)
解決方法: 在Repeater綁定中添加ItemDataBound事件,選項(xiàng)用RadioButtonList綁定,附源代碼:
Default頁(yè),源頁(yè)面
<div>
廣大網(wǎng)友對(duì)保障房建設(shè)相關(guān)問題調(diào)查<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="查看結(jié)果" OnClick="Button2_Click" />
</div>
對(duì)應(yīng)的cs頁(yè):
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: 無法循環(huán)獲得用戶的選擇
解決方法: 先循環(huán)Repeater控件的Item獲得RadioButtonList控件,循環(huán)檢測(cè)是否為選中狀態(tài),,如果是則拼接到一個(gè)字符串中,
再把題目的編號(hào)獲得拼接起來,循環(huán)添加,附源代碼:
Default的cs頁(yè):
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('請(qǐng)完成選擇')</script>");
}
}
DAL頁(yè):
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跳轉(zhuǎn):
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事件,選項(xiàng)用RadioButtonList綁定,附源代碼:
Default頁(yè),源頁(yè)面
復(fù)制代碼 代碼如下:
<div>
廣大網(wǎng)友對(duì)保障房建設(shè)相關(guān)問題調(diào)查<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="查看結(jié)果" OnClick="Button2_Click" />
</div>
對(duì)應(yīng)的cs頁(yè):
復(fù)制代碼 代碼如下:
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: 無法循環(huán)獲得用戶的選擇
解決方法: 先循環(huán)Repeater控件的Item獲得RadioButtonList控件,循環(huán)檢測(cè)是否為選中狀態(tài),,如果是則拼接到一個(gè)字符串中,
再把題目的編號(hào)獲得拼接起來,循環(huán)添加,附源代碼:
Default的cs頁(yè):
復(fù)制代碼 代碼如下:
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('請(qǐng)完成選擇')</script>");
}
}
DAL頁(yè):
復(fù)制代碼 代碼如下:
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跳轉(zhuǎn):
this.ClientScript.RegisterClientScriptBlock(typeof(string), "ok", "<script>alert('投票成功!謝謝參與')</script>");
兩種獲得控件的方法:
Literal Literal1 = (Literal)e.Item.FindControl("Literal1");
Literal Literal1 = e.Item.FindControl("Literal1") as Literal;
相關(guān)文章
.NET 6開發(fā)TodoList應(yīng)用之實(shí)現(xiàn)API版本控制
API接口版本管理,對(duì)于一些規(guī)模稍大的企業(yè)應(yīng)用來說,是經(jīng)常需要關(guān)注的一大需求。本文將介紹在.NET 6開發(fā)中如何實(shí)現(xiàn)API版本控制,感興趣的可以了解一下2022-01-01asp.net開發(fā)sql server轉(zhuǎn)換成oracle的方法詳解
這篇文章主要給大家介紹了關(guān)于asp.net開發(fā)中sql server轉(zhuǎn)換成oracle的相關(guān)資料,文中通過示例代碼和圖文將實(shí)現(xiàn)的步驟一步步介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧。2017-08-08阿里云上從ASP.NET線程角度對(duì)“黑色30秒”問題的全新分析
在這篇博文中,我們拋開對(duì)阿里云的懷疑,完全從ASP.NET的角度進(jìn)行分析,看能不能找到針對(duì)問題現(xiàn)象的更合理的解釋2015-09-09asp.net中url地址傳送中文參數(shù)時(shí)的兩種解決方案
前天遇到一個(gè)地址傳遞中文參數(shù)變?yōu)閬y碼的問題,同樣的兩個(gè)web Project,一個(gè)是vs2003,一個(gè)是vs2005,前者可以,后者就是不可以。2009-11-11