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
Copy Code code 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>
<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= "Submit"/>
<asp:button id= "Button2" runat= "Server" text= "View Results" onclick= "button2_click"/>
</div>
Corresponding CS page:
Copy Code code 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:
Copy Code code 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:
Copy Code code 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;