ASP: repeater nesting

Source: Internet
Author: User

An online examination system was developed. During the examination, repeater was used to repeatedly read the questions displayed on the page. During the multiple-choice question processing, the database was designed to be a little troublesome. I stored the question stem and options in two separate tables, so that only one repeater could not be used for reading when the page is displayed, all are nested.CodeEntry:

<Div id = "  Divsubjectofsingleselectionlist  " Style = "  Text-align: left;  " > <Div style = "  Color: red; text-align: left  " > Tip: case-insensitive letters </Div> <asp: repeater id = "  Rptcategories  " Runat = "  Server  " Onitemdatabound = "  Rptcategories_itemdatabound1  " > <Headertemplate> <Table width = "  100%  " Border = " 0  " Cellspacing = "  0  " Cellpadding = "  0  " > </Headertemplate> <itemtemplate> <! -- Stem --> <tr> <TD> <B> (<% # container. itemindex + 1 %>) <% # Databinder. eval (container. dataitem, " Topic  " ) %> (< ASP: textbox ID = "  Txtsingleselectionanswer  " Runat = "  Server  " Borderwidth = "  0  " Width = "  20px  " Maxlength = "  1  " TEXT = '  <% # Databinder. eval (container. dataitem, "rowone") %>  ' > </ASP: textbox> ) </B> <asp: hiddenfield id = "  Hidsingleselectionid  " Runat = "  Server  " Value = '  <% # Eval ("ID") %>  ' > </ASP: hiddenfield> </TD> </tr> <! -- Option --> <asp: repeater id = "  Rptproduct  " Runat = "  Server  " > <Itemtemplate> <tr> <TD> & nbsp ;& Nbsp; <% # Databinder. eval (container. dataitem, "  Selectabcde  " ) %> & Nbsp ;& Nbsp; <% # Databinder. eval (container. dataitem, "  Option  " ) %> </TD> </tr> </itemtemplate> </ASP: repeater> </itemtemplate> <footertemplate> </table> </footertemplate> </ASP: repeater> </div>

Everyone knows: <Itemtemplate> here is the content to be displayed cyclically </itemtemplate>

<Footertemplate> the end part </footertemplate>

In itemtemplate, I can only insert the option under each question in a loop, so here I put an ASP: repeater, query the question stem Based on the ID of the question stem stored in the hidden domain hiddenfield under each question stem, and rebind the question stem. In addition, the outer ASP: repeater adds an onitemdatabound = "rptcategories_itemdatabound1" attribute to trigger the option binding. The background code is as follows:

  Protected   Void Page_load ( Object  Sender, eventargs e ){  If (! Ispostback) {bindtestinfo ();  // Bind basic exam information
Bindrepeater (); // Bind question stem
}} Private Void Bindrepeater () {testlistbll = New Testlistbll (); String Id = request. querystring [ " ID " ]; // Exam ID Datatable = Testlistbll. getinformations (ID); lblsingleselectioncount. Text = Datatable. Rows. Count. tostring (); rptcategories. datasource =Datatable; rptcategories. databind (); // bind a single-choice question
Datatable dtmore = Testlistbll. getinformationsmore (ID); label2.text = Dtmore. Rows. Count. tostring (); rptsubjectofmultiselectionlist. datasource = Dtmore; rptsubjectofmultiselectionlist. databind (); // bind multiple choice questions
Allcount. Text = (Datatable. Rows. Count + Dtmore. Rows. Count). tostring ();} // Bind options under question stem Protected Void Rptcategories_itemdatabound1 (Object Sender, repeateritemeventargs e) {testlistbll = New Testlistbll (); If (E. Item. itemtype = listitemtype. Item | E. Item. itemtype = Listitemtype. alternatingitem) {repeater rep = E. Item. findcontrol ( " Rptproduct " ) As Repeater; // Find the repeater object in the layer Datarowview rowv = (datarowview) E. Item. dataitem; // Locate the data item associated with the category Repeater String Typeid = rowv [ " ID " ]. Tostring (); // Obtains the ID of the fill subclass. Datatable dt = Testlistbll. getoptions (typeid); rep. datasource = DT; rep. databind ();}}

If (E. Item. itemtype = listitemtype. Item | E. Item. itemtype = listitemtype. alternatingitem) {} this judgment is indispensable. Remember that if it is not added, it cannot be bound.

Effect:


 

 

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.