asp.net cannot be looped to bind the vote's title and options

  • 2020-05-12 02:26:23
  • OfStack

Problem :1. Unable to loop bind the title and options of the vote
Solution: add ItemDataBound event in Repeater binding, option to bind with RadioButtonList, attached source code:
Default page, source page
 
<div> 
 The majority of Internet users on the construction of affordable housing related issues <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 the results " OnClick="Button2_Click" /> 
</div> 


Corresponding cs page:
 
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(); 
} 

Problem 2: you cannot loop to get the user's choice
Solution: first, loop Item of Repeater control to obtain RadioButtonList control, and then check whether it is in the selected state. If it is, then concatenate it into a string.
Then get the title number splicing together, loop add, attached to the source code:

cs page:
 
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 success ! Thank you for your participation ')</script>"); 
} 
else 
{ 
this.ClientScript.RegisterClientScriptBlock(typeof(string), "ok", "<script>alert(' Please complete the selection ')</script>"); 
} 
} 

DAL page:
 
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; 
} 

Skills:
JavaScript jump:
this.ClientScript.RegisterClientScriptBlock(typeof(string), "ok", " < script > alert(' vote successful! Thank you for your participation ') < /script > ");
There are two ways to get a control:
Literal Literal1 = (Literal)e.Item.FindControl("Literal1");
Literal Literal1 = e.Item.FindControl("Literal1") as Literal;

Related articles: