Solution to asp.net the title and options of a ballot that cannot be circularly bound

Source: Internet
Author: User
Tags add object bool eval net string split
Question: 1, unable to loop the title and options of the voting
Workaround: Add the ItemDataBound event in the repeater binding, with the option RadioButtonList binding, with source code:
Default page, source page
The code is as follows:
<div>
The vast number of netizens on the protection of housing construction related issues investigation <br/>
<asp:repeater id= "Repeater1" runat= "Server" onitemdatabound= "Repeater1_itemdatabound" >
<ItemTemplate>
<table>
<tr>
&LT;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= "Submit"/>
<asp:button id= "Button2" runat= "Server" text= "View results"/>
</div>


Corresponding CS page:
The code is as follows:
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 ();
}

Issue 2: Unable to cycle to get user's choice
Workaround: First loop the item of the Repeater control to get the RadioButtonList control, loop to detect whether it is selected, and if so, splice it into a string.
And then the title of the number of stitching up, loop Add, with source code:

CS page for Default:
The code is as follows:
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 (' vote successful! Thank you for participation ') </script>");
}
Else
{
This. Clientscript.registerclientscriptblock (typeof (String), "OK", "<script>alert (' please complete selection ') </script>");
}
}

Dal page:
The code is as follows:
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;
}

The skills that are mastered:
JavaScript Jump:
This. Clientscript.registerclientscriptblock (typeof (String), "OK", "<script>alert (' vote successful! Thank you for participation ') </script>");
Two ways to get controls:
Literal Literal1 = (Literal) e.item.findcontrol ("Literal1");
Literal Literal1 = E.item.findcontrol ("Literal1") as Literal;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.