国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

asp.net下無法循環綁定投票的標題和選項的解決方法

2024-07-10 13:25:12
字體:
來源:轉載
供稿:網友
問題:1,無法循環綁定投票的標題和選項
解決方法: 在Repeater綁定中添加ItemDataBound事件,選項用RadioButtonList綁定,附源代碼:
Default頁,源頁面

復制代碼 代碼如下:


<div>
廣大網友對保障房建設相關問題調查<br />
<asp:Repeater runat="server" OnItemDataBound="Repeater1_ItemDataBound">
<ItemTemplate>
<table>
<tr>
<td colspan="3">
<b>
<%# Eval("t_timu")%>
<asp:Literal Text='<%# Eval("t_id")%>' runat="server"></asp:Literal>

</b>
</td>
</tr>
<tr>
<asp:RadioButtonList runat="server" RepeatDirection="Horizontal">
</asp:RadioButtonList>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
<br />
<asp:Button runat="server" Text="提交" /> 
<asp:Button 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;
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 藁城市| 彰武县| 江口县| 岐山县| 炉霍县| 洪雅县| 广饶县| 吉木萨尔县| 新宁县| 汨罗市| 泰宁县| 新昌县| 澄迈县| 依安县| 黄山市| 北票市| 道真| 通渭县| 丰原市| 罗定市| 静安区| 左贡县| 绥化市| 牟定县| 彰化市| 萝北县| 富裕县| 秦皇岛市| 恩施市| 遂平县| 凤山县| 施秉县| 江川县| 内乡县| 女性| 巴塘县| 鄂温| 呼和浩特市| 辽中县| 荥阳市| 嘉鱼县|