Solution to the failure to repeatedly bind voting titles and options in asp.net

Source: Internet
Author: User

Problem: 1. The title and option of the vote cannot be bound cyclically.
Solution: add the ItemDataBound event to the Repeater binding. Use RadioButtonList to bind the event with the source code:
Default Page, Source Page
Copy codeThe Code is as follows:
<Div>
Survey of problems related to construction of affordable housing <br/>
<Asp: Repeater ID = "Repeater1" runat = "server" OnItemDataBound = "repeaterincluitemdatabound">
<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 = "button#click" Text = ""/>
<Asp: Button ID = "Button2" runat = "server" Text = "view results" OnClick = "Button2_Click"/>
</Div>


Corresponding cs page:
Copy codeThe Code is as follows:
Protected void repeaterincluitemdatabound (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: the user's choice cannot be obtained cyclically.
Solution: loop through the Item of the Repeater control to obtain the RadioButtonList control and check whether it is selected. If so, splice it into a string,
Combine the question number and add it cyclically with the source code:

Default cs page:
Copy codeThe Code is as follows:
Protected void button#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 choosing ') </script> ");
}
Else
{
This. ClientScript. RegisterClientScriptBlock (typeof (string), "OK", "<script> alert ('Please complete select') </script> ");
}
}

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

Skills:
JavaScript jump:
This. ClientScript. RegisterClientScriptBlock (typeof (string), "OK", "<script> alert ('vote successful! Thank you for choosing ') </script> ");
Two methods to obtain controls:
Literal Literal1 = (Literal) e. Item. FindControl ("Literal1 ");
Literal Literal1 = e. Item. FindControl ("Literal1") as Literal;

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.