locked
Repeater RRS feed

  • Question

  • User-158363518 posted

    hi every body

    How can I resolve the error code ?

    I want the text of options to be read from the database .

                <asp:Repeater ID="Repeater1" runat="server">
                    <ItemTemplate>
    
                                    <p><%# DataBinder.Eval(Container.DataItem, "DQuestions")%></p>
    
    
                                    <asp:RadioButtonList ID="RadioButtonList1" runat="server" CssClass="Spaces" RepeatDirection="Vertical">
                                        <asp:ListItem Value="1" Text='<%# DataBinder.Eval(Container.DataItem, "num1")%>'></asp:ListItem>
                                        <asp:ListItem Value="2" Text='<%# DataBinder.Eval(Container.DataItem, "num2")%>'></asp:ListItem>
                                        <asp:ListItem Value="3" Text='<%# DataBinder.Eval(Container.DataItem, "num3")%>'></asp:ListItem>
                                        <asp:ListItem Value="4" Text='<%# DataBinder.Eval(Container.DataItem, "num4")%>'></asp:ListItem>
                                    </asp:RadioButtonList>
    
                    </ItemTemplate>
                </asp:Repeater>

    this code Not Worked

    <asp:ListItem Value="1" Text='<%# DataBinder.Eval(Container.DataItem, "num1")%>'></asp:ListItem>
    <asp:ListItem Value="2" Text='<%# DataBinder.Eval(Container.DataItem, "num2")%>'></asp:ListItem>
    <asp:ListItem Value="3" Text='<%# DataBinder.Eval(Container.DataItem, "num3")%>'></asp:ListItem>
    <asp:ListItem Value="4" Text='<%# DataBinder.Eval(Container.DataItem, "num4")%>'></asp:ListItem>

    after that , I need a code For storage of the selected options.

    Thanks a Lot . CSajad

    Friday, June 3, 2016 2:21 PM

Answers

  • User61956409 posted

    Hi csajad,

    In a word, you could retrieve records (options) from database based on the Question id field or other field, then you could dynamically create and append items to RadioButtonList control on Repeater.ItemDataBound event.

    For saving selected RadioButton values as answer, you could loop through Repeater control items to find selected options and insert records into database.

    for (int i = 0; i <  Repeater1.Items.Count; i++)
    {
        int selectedindex = ((RadioButtonList)Repeater1.Items[i].FindControl("RadioButtonList1")).SelectedIndex;
    
        //insert records into AnswerTable 
    }   
    

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 7, 2016 8:24 AM

All replies

  • User61956409 posted

    Hi csajad,

    You could try to find RadioButtonList control “RadioButtonList1” on Repeater.ItemDataBound event and populate RadioButtonList control with your data source. The following sample code is for your reference.

    <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
        <ItemTemplate>
            <p><%# DataBinder.Eval(Container.DataItem, "DQuestions")%></p>
            <asp:RadioButtonList ID="RadioButtonList1" runat="server" CssClass="Spaces" RepeatDirection="Vertical">
            </asp:RadioButtonList>
        </ItemTemplate>
    </asp:Repeater>
    
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        RadioButtonList rads = (RadioButtonList)e.Item.FindControl("RadioButtonList1");
    
        //here, you could fetch records from database and populate RadioButtonList control
    
        DataTable dt = new DataTable();
        dt.Columns.Add("num");
    
        dt.Rows.Add("num11");
        dt.Rows.Add("num12");
        dt.Rows.Add("num13");
        dt.Rows.Add("num14");
    
        rads.DataTextField = "num";
        rads.DataValueField = "num";
    
        rads.DataSource = dt;
        rads.DataBind();
    }
    

    Best Regards,

    Fei Han



    Monday, June 6, 2016 5:08 AM
  • User-158363518 posted

    hi Fei Han

    I am thankful with your guidance .

    I want to show in my test questions .


    /*      Table_Questions      */
    
    idQuestion int , DQuestions nvarchar(2000), Option1 nvarchar(2000), Option2 nvarchar(2000), Option3 nvarchar(2000), Option4 nvarchar(2000),
    Answer int , score int

    /*        Table_ExamQuestions            */
    
    idExam  int ,
    idQuestions  int
    

    These are my tables.

    I want to show my test questions.

    Now what do you recommend ?

    Monday, June 6, 2016 8:07 AM
  • User61956409 posted

    Hi csajad,

    According to your table definition, we could find that all items are stored in [Option*] fields (Option1, Option2, Option3 and Option4). So you could try to dynamically create and append items to RadioButtonList control.

    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        RadioButtonList rads = (RadioButtonList)e.Item.FindControl("RadioButtonList1");
    
        //here, you could fetch records from database and populate RadioButtonList control
    
        DataTable dt = new DataTable();
        dt.Columns.Add("idQuestion");
        dt.Columns.Add("DQuestions");
        dt.Columns.Add("Option1");
        dt.Columns.Add("Option2");
        dt.Columns.Add("Option3");
        dt.Columns.Add("Option4");
    
        dt.Rows.Add("1","Question1","num11","num12","num13","num14");
    
        ListItem item1 = new ListItem(dt.Rows[0]["Option1"].ToString());
        rads.Items.Add(item1);
    
        ListItem item2 = new ListItem(dt.Rows[0]["Option2"].ToString());
        rads.Items.Add(item2);
    
        ListItem item3 = new ListItem(dt.Rows[0]["Option3"].ToString());
        rads.Items.Add(item3);
    
        ListItem item4 = new ListItem(dt.Rows[0]["Option4"].ToString());
        rads.Items.Add(item4);
    
    }
    

    Best Regards,

    Fei Han

    Monday, June 6, 2016 8:53 AM
  • User-158363518 posted

    hi Fei Han

    I am thankful with your guidance .

    I'm a little confused.Can you explain a little more ?

        protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            RadioButtonList rads = (RadioButtonList)e.Item.FindControl("RadioButtonList1");
            Label lblQuestion = (Label)e.Item.FindControl("Label1");
    
    
            SqlConnection con = new SqlConnection(connectionString);
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "OnlineExamQuestionsBind";
            cmd.Connection = con;
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
    
            DataTable dt = new DataTable();
            
            dt.Columns.Add("idQuestion");
            dt.Columns.Add("DQuestions");
            dt.Columns.Add("Option1");
            dt.Columns.Add("Option2");
            dt.Columns.Add("Option3");
            dt.Columns.Add("Option4");
            sda.Fill(dt);
    
            dt.Rows.Add("1", "DQuestions", "Option1", "Option2", "Option3", "Option4");
    
            ListItem item1 = new ListItem(dt.Rows[0]["Option1"].ToString());
            rads.Items.Add(item1);
    
            ListItem item2 = new ListItem(dt.Rows[0]["Option2"].ToString());
            rads.Items.Add(item2);
    
            ListItem item3 = new ListItem(dt.Rows[0]["Option3"].ToString());
            rads.Items.Add(item3);
    
            ListItem item4 = new ListItem(dt.Rows[0]["Option4"].ToString());
            rads.Items.Add(item4);
            lblQuestion.Text = dt.Rows[0]["DQuestions"].ToString();
        }

     

    I Want Show Quetion Title , Options in My page .

    Radio Button must :

    Option1 : value 1 , Text : OptionTitle

    Option2 : value 2 , Text : OptionTitle

    Option3 : value 3 , Text : OptionTitle

    Option4 : value 4 , Text : OptionTitle

    Option Title  Bind From DataBase ["Option1"]

    after that I need All Selected RadioButton Value For  Save In AnswerTable .

        Table Responses
          [idStudent] int ,
          [idQuestions] int,
          [Answer]  int

    What do you recommend?

    Thank you very much for your help

    Monday, June 6, 2016 10:22 AM
  • User61956409 posted

    Hi csajad,

    In a word, you could retrieve records (options) from database based on the Question id field or other field, then you could dynamically create and append items to RadioButtonList control on Repeater.ItemDataBound event.

    For saving selected RadioButton values as answer, you could loop through Repeater control items to find selected options and insert records into database.

    for (int i = 0; i <  Repeater1.Items.Count; i++)
    {
        int selectedindex = ((RadioButtonList)Repeater1.Items[i].FindControl("RadioButtonList1")).SelectedIndex;
    
        //insert records into AnswerTable 
    }   
    

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 7, 2016 8:24 AM