locked
RadioButtonList Keeping State after change items while binding to database RRS feed

Answers

  • User614698185 posted

    Hi TamerHemaida,

    Based on your requirement, I modify your code like below:

    On .aspx page:

    <asp:RadioButtonList OnDataBound="RadioButtonList1_DataBound" ID="RadioButtonList1" AutoPostBack="true" runat="server" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged">
    </asp:RadioButtonList>

    On .aspx.cs page:

        public partial class KeepChoice : System.Web.UI.Page
        {
            string sql, SEL;
            SqlConnection con;
            SqlDataAdapter da;
            DataTable dt;
            string cons = ConfigurationManager.ConnectionStrings["Constr"].ConnectionString;
            string currentSelected = "";
            public class getanswer
            {
                public string quename { get; set; }
                public string ans { get; set; }
            }
            public static class inc
            {
                public static ArrayList al;
                public static int index;
            }
    
            protected void Page_Load(object sender, EventArgs e)
            {
                //GET DATA..
                sql = "SELECT [QID], [Ques], [Op1], [Op2], [Op3], [Op4],[Rop] FROM [Questions]";
                con = new SqlConnection(cons);
                da = new SqlDataAdapter(sql, con);
                dt = new DataTable();
                da.Fill(dt);
                if (!IsPostBack)
                {
                    inc.index = 0;
                    Label1.Text = dt.Rows[inc.index]["Ques"].ToString();
                    currentSelected = dt.Rows[inc.index]["Rop"].ToString();//Get selected item
                    string[] items = { dt.Rows[inc.index]["Op1"].ToString(), dt.Rows[inc.index]["Op2"].ToString(), dt.Rows[inc.index]["Op3"].ToString(), dt.Rows[inc.index]["Op4"].ToString() };
                    RadioButtonList1.DataSource = items;
                    RadioButtonList1.DataBind();
    
                    ViewState[inc.index.ToString()] = -1;
                }
            }
    
            protected void First_Click(object sender, EventArgs e)
            {
                if (RadioButtonList1.SelectedIndex > -1)
                {
                    //SAVE ANSWERS
                    save_answer();
                }
                //MOVE TO FIRST RECORD
                inc.index = 0;
                Label1.Text = dt.Rows[inc.index]["Ques"].ToString();
                currentSelected = dt.Rows[inc.index]["Rop"].ToString();//Get selected item
                string[] items = { dt.Rows[inc.index]["Op1"].ToString(), dt.Rows[inc.index]["Op2"].ToString(), dt.Rows[inc.index]["Op3"].ToString(), dt.Rows[inc.index]["Op4"].ToString() };
                RadioButtonList1.DataSource = items;
                RadioButtonList1.DataBind();
    
    
            }
    
            protected void Next_Click(object sender, EventArgs e)
            {
                if (RadioButtonList1.SelectedIndex > -1)
                {
                    //SAVE ANSWERS
                    save_answer();
                }
                //MOVE TO NEXT RECORD
                if (inc.index < dt.Rows.Count - 1)
                {
                    inc.index++;
                }
                Label1.Text = dt.Rows[inc.index]["Ques"].ToString();
                currentSelected = dt.Rows[inc.index]["Rop"].ToString();//Get selected item
                string[] items = { dt.Rows[inc.index]["Op1"].ToString(), dt.Rows[inc.index]["Op2"].ToString(), dt.Rows[inc.index]["Op3"].ToString(), dt.Rows[inc.index]["Op4"].ToString() };
                RadioButtonList1.DataSource = items;
                RadioButtonList1.DataBind();
    
            }
    
            protected void Previous_Click(object sender, EventArgs e)
            {
                if (RadioButtonList1.SelectedIndex > -1)
                {
                    //SAVE ANSWERS
                    save_answer();
                }
                //MOVE TO PREVIOUS RECORD
                if (inc.index > 0)
                {
                    inc.index--;
                }
                Label1.Text = dt.Rows[inc.index]["Ques"].ToString();
                currentSelected = dt.Rows[inc.index]["Rop"].ToString();//Get selected item
                string[] items = { dt.Rows[inc.index]["Op1"].ToString(), dt.Rows[inc.index]["Op2"].ToString(), dt.Rows[inc.index]["Op3"].ToString(), dt.Rows[inc.index]["Op4"].ToString() };
                RadioButtonList1.DataSource = items;
                RadioButtonList1.DataBind();
    
            }
    
            protected void Last_Click(object sender, EventArgs e)
            {
                if (RadioButtonList1.SelectedIndex > -1)
                {
                    //SAVE ANSWERS
                    save_answer();
                }
                //MOVE TO LAST RECORD
                inc.index = dt.Rows.Count - 1;
                Label1.Text = dt.Rows[inc.index]["Ques"].ToString();
                currentSelected = dt.Rows[inc.index]["Rop"].ToString();//Get selected item
                string[] items = { dt.Rows[inc.index]["Op1"].ToString(), dt.Rows[inc.index]["Op2"].ToString(), dt.Rows[inc.index]["Op3"].ToString(), dt.Rows[inc.index]["Op4"].ToString() };
                RadioButtonList1.DataSource = items;
                RadioButtonList1.DataBind();
    
            }
    
            private void save_answer()
            {
                SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Constr"].ConnectionString);
                conn.Open();
                string Rop;
                Rop = RadioButtonList1.SelectedItem.Value;
                SqlCommand cmd = new SqlCommand("Update Questions set Rop=@Rop where QID=@QID", conn);
                cmd.Parameters.Add("@Rop", SqlDbType.VarChar);
                cmd.Parameters.Add("@QID", SqlDbType.Int);
                cmd.Parameters["@Rop"].Value = Rop;
                cmd.Parameters["@QID"].Value = inc.index + 1;
                cmd.ExecuteNonQuery();
                conn.Close();
    
            }
            protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
            {
                ViewState[inc.index.ToString()] = RadioButtonList1.SelectedIndex;
            }
            protected void Button1_Click(object sender, EventArgs e)
            {
                RadioButtonList1.SelectedIndex = 3;
            }
    
            protected void RadioButtonList1_DataBound(object sender, EventArgs e)
            {
                var item = RadioButtonList1.Items.FindByValue(currentSelected);
                if (item != null)
                {
                    item.Selected = true;
                }
    
            }
        }

    Best Regards,

    Candice Zhou

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 30, 2015 9:26 AM

All replies

  • User-1506965535 posted

    How to keep my choices from RadioButtonList While Binding to database

    Hi TamerHemaida,

    The image is not fully clarifying your issue.

    Can you make it more clear as what is the issue and what is not exactly working.

    Also it would be great if you post some code related to this.

    The below link also helps in knowing how to use the radiobuttonlist

    http://code.runnable.com/UjnnaN9UjN8SAAAV/asp-net-how-to-use-radiobutton-list

    Wednesday, September 30, 2015 1:51 AM
  • User-699776897 posted

    Thanks Mr. 

    nadeem157   for reply

    ===============================================

    This Quiz For Student ...

    I need to keep RadioButtonList Selected Option by  student , after go to next ques and back to previous ques

    ===============================================

    this is my Code  for default.aspx:

    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>--%>
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    <hr />
    <asp:RadioButtonList ID="RadioButtonList1" AutoPostBack="true" runat="server" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged"></asp:RadioButtonList>
    <%--<asp:SqlDataSource runat="server" ID="SqlDataSource1" ConnectionString='<%$ ConnectionStrings:learningConnectionString %>' SelectCommand="SELECT [QID], [Ques], [Op1], [Op2], [Op3], [Op4], [Rop] FROM [Questions]"></asp:SqlDataSource>--%>
    <hr />
    <asp:Button ID="First" runat="server" Text="|<" OnClick="First_Click" ToolTip="أول سؤال" />
    <asp:Button ID="Next" runat="server" Text="<" OnClick="Next_Click" ToolTip="التالي" />
    &nbsp;&nbsp;&nbsp;
    <asp:Button ID="Previous" runat="server" Text=">" OnClick="Previous_Click" ToolTip="السابق" />
    <asp:Button ID="Last" runat="server" Text=">|" OnClick="Last_Click" ToolTip="آخر سؤال" />
    </ContentTemplate>
    </asp:UpdatePanel>

    and this code behind

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data.SqlClient;
    using System.Data;
    using System.Collections;
    using System.Configuration;
    public partial class _Default : Page
    {
    string sql,SEL;
    SqlConnection con;
    SqlDataAdapter da;
    DataTable dt;
    string cons = ConfigurationManager.ConnectionStrings["learningConnectionString"].ConnectionString;
    public class getanswer
    {
    public string quename { get; set; }
    public string ans { get; set; }
    }
    public static class inc
    {
    public static ArrayList al;
    public static int index;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
    //GET DATA..
    sql = "SELECT [QID], [Ques], [Op1], [Op2], [Op3], [Op4], [Rop] FROM [Questions]";
    con = new SqlConnection(cons);
    da = new SqlDataAdapter(sql, con);
    dt = new DataTable();
    da.Fill(dt);
    if (!IsPostBack)
    {
    //FILL FIRST RECORD
    List<getanswer> answ = new List<getanswer>();
    Session.Add("answ", answ);
    inc.index = 0;
    Label1.Text = dt.Rows[inc.index]["Ques"].ToString();
    string[] items = { dt.Rows[inc.index]["Op1"].ToString(), dt.Rows[inc.index]["Op2"].ToString(), dt.Rows[inc.index]["Op3"].ToString(), dt.Rows[inc.index]["Op4"].ToString() };
    RadioButtonList1.DataSource = items;
    RadioButtonList1.DataBind();
    ViewState[inc.index.ToString()]=-1;
    }
    }
    protected void First_Click(object sender, EventArgs e)
    {
    if (RadioButtonList1.SelectedIndex > -1)
    {
    //SAVE ANSWERS
    save_answer();
    }
    //MOVE TO FIRST RECORD
    inc.index = 0;
    Label1.Text = dt.Rows[inc.index]["Ques"].ToString();
    string[] items = { dt.Rows[inc.index]["Op1"].ToString(), dt.Rows[inc.index]["Op2"].ToString(), dt.Rows[inc.index]["Op3"].ToString(), dt.Rows[inc.index]["Op4"].ToString() };
    RadioButtonList1.DataSource = items;
    RadioButtonList1.DataBind();

    CheckR();
    }

    protected void Next_Click(object sender, EventArgs e)
    {
    if (RadioButtonList1.SelectedIndex > -1)
    {
    //SAVE ANSWERS
    save_answer();
    }
    //MOVE TO NEXT RECORD
    if (inc.index < dt.Rows.Count - 1)
    {
    inc.index++;
    }
    Label1.Text = dt.Rows[inc.index]["Ques"].ToString();
    string[] items = { dt.Rows[inc.index]["Op1"].ToString(), dt.Rows[inc.index]["Op2"].ToString(), dt.Rows[inc.index]["Op3"].ToString(), dt.Rows[inc.index]["Op4"].ToString() };
    RadioButtonList1.DataSource = items;
    RadioButtonList1.DataBind();
    CheckR();
    }
    protected void Previous_Click(object sender, EventArgs e)
    {

    if (RadioButtonList1.SelectedIndex > -1)
    {
    //SAVE ANSWERS
    save_answer();
    }
    //MOVE TO PREVIOUS RECORD
    if (inc.index > 0)
    {
    inc.index--;
    }
    Label1.Text = dt.Rows[inc.index]["Ques"].ToString();
    string[] items = { dt.Rows[inc.index]["Op1"].ToString(), dt.Rows[inc.index]["Op2"].ToString(), dt.Rows[inc.index]["Op3"].ToString(), dt.Rows[inc.index]["Op4"].ToString() };
    RadioButtonList1.DataSource = items;
    RadioButtonList1.DataBind();
    CheckR();
    }
    protected void Last_Click(object sender, EventArgs e)
    {
    if (RadioButtonList1.SelectedIndex > -1)
    {
    //SAVE ANSWERS
    save_answer();
    }
    //MOVE TO LAST RECORD
    inc.index = dt.Rows.Count - 1;
    Label1.Text = dt.Rows[inc.index]["Ques"].ToString();
    string[] items = { dt.Rows[inc.index]["Op1"].ToString(), dt.Rows[inc.index]["Op2"].ToString(), dt.Rows[inc.index]["Op3"].ToString(), dt.Rows[inc.index]["Op4"].ToString() };
    RadioButtonList1.DataSource = items;
    RadioButtonList1.DataBind();
    CheckR();
    }
    private void save_answer()
    {
    getanswer ans = new getanswer();
    ans.quename = Label1.Text;
    ans.ans = RadioButtonList1.SelectedValue.ToString();
    List<getanswer> answ = new List<getanswer>();
    foreach (getanswer ch in answ)
    {
    if (ch.quename.Equals(ans.quename))
    {
    answ.Remove(ch);
    answ.Add(ans);
    }
    else
    {
    answ.Add(ans);
    }
    }
    answ.Add(ans);
    Session.Add("answ", answ);
    }
    protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
    {
    ViewState[inc.index.ToString()] = RadioButtonList1.SelectedIndex;
    //SEL = ViewState[inc.index.ToString()].ToString();
    //Button1.Text = SEL;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
    RadioButtonList1.SelectedIndex = 3;
    }
    private void CheckR()
    {
    ViewState[inc.index.ToString()] = -1;
    SEL = ViewState[inc.index.ToString()].ToString();
    RadioButtonList1.SelectedIndex = int.Parse(SEL);
    }
    }

    Wednesday, September 30, 2015 2:08 AM
  • User-1506965535 posted

    I need to keep RadioButtonList Selected Option by  student

    If you need to set Student selected, you need to do something like this on Page_load.

    radioButtonList1.Items.FindByText("Your Text").Selected = true;

    Wednesday, September 30, 2015 2:15 AM
  • User-699776897 posted

    My Dear

    I want to keep the student choices during the performance of the test and during the sailing during the test
    Note that there are buttons to move from question to another
    After the end of the test and are grouped those answers

    Wednesday, September 30, 2015 2:54 AM
  • User614698185 posted

    Hi TamerHemaida,

    Based on your requirement, I created the sample for you, code shown below:

    if (lblCity.Text == "Paris") 
    {
        RadioButtonList1.Items.FindByValue("1").Selected = true;  
    }
    else if(lblCity.Text=="Monaco") 
    {
        RadioButtonList1.Items.FindByValue("2").Selected = true;
    }
    else if(lblCity.Text=="Lyon") 
    {
        RadioButtonList1.Items.FindByValue("3").Selected = true;
    }
    else
    {
        RadioButtonList1.Items.FindByValue("4").Selected = true;
    }

    Best Regards,

    Candice Zhou

    Wednesday, September 30, 2015 3:49 AM
  • User-699776897 posted

    Dear: Candice Zhou

    your code is not answer to my question ..

    Imagine that you are in a quiz and navigate to variant question according to your request

     the problem is : how keep your choices after change questions every time

    Wednesday, September 30, 2015 4:08 AM
  • User614698185 posted

    Hi TamerHemaida,

    Based on your requirement, I modify your code like below:

    On .aspx page:

    <asp:RadioButtonList OnDataBound="RadioButtonList1_DataBound" ID="RadioButtonList1" AutoPostBack="true" runat="server" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged">
    </asp:RadioButtonList>

    On .aspx.cs page:

        public partial class KeepChoice : System.Web.UI.Page
        {
            string sql, SEL;
            SqlConnection con;
            SqlDataAdapter da;
            DataTable dt;
            string cons = ConfigurationManager.ConnectionStrings["Constr"].ConnectionString;
            string currentSelected = "";
            public class getanswer
            {
                public string quename { get; set; }
                public string ans { get; set; }
            }
            public static class inc
            {
                public static ArrayList al;
                public static int index;
            }
    
            protected void Page_Load(object sender, EventArgs e)
            {
                //GET DATA..
                sql = "SELECT [QID], [Ques], [Op1], [Op2], [Op3], [Op4],[Rop] FROM [Questions]";
                con = new SqlConnection(cons);
                da = new SqlDataAdapter(sql, con);
                dt = new DataTable();
                da.Fill(dt);
                if (!IsPostBack)
                {
                    inc.index = 0;
                    Label1.Text = dt.Rows[inc.index]["Ques"].ToString();
                    currentSelected = dt.Rows[inc.index]["Rop"].ToString();//Get selected item
                    string[] items = { dt.Rows[inc.index]["Op1"].ToString(), dt.Rows[inc.index]["Op2"].ToString(), dt.Rows[inc.index]["Op3"].ToString(), dt.Rows[inc.index]["Op4"].ToString() };
                    RadioButtonList1.DataSource = items;
                    RadioButtonList1.DataBind();
    
                    ViewState[inc.index.ToString()] = -1;
                }
            }
    
            protected void First_Click(object sender, EventArgs e)
            {
                if (RadioButtonList1.SelectedIndex > -1)
                {
                    //SAVE ANSWERS
                    save_answer();
                }
                //MOVE TO FIRST RECORD
                inc.index = 0;
                Label1.Text = dt.Rows[inc.index]["Ques"].ToString();
                currentSelected = dt.Rows[inc.index]["Rop"].ToString();//Get selected item
                string[] items = { dt.Rows[inc.index]["Op1"].ToString(), dt.Rows[inc.index]["Op2"].ToString(), dt.Rows[inc.index]["Op3"].ToString(), dt.Rows[inc.index]["Op4"].ToString() };
                RadioButtonList1.DataSource = items;
                RadioButtonList1.DataBind();
    
    
            }
    
            protected void Next_Click(object sender, EventArgs e)
            {
                if (RadioButtonList1.SelectedIndex > -1)
                {
                    //SAVE ANSWERS
                    save_answer();
                }
                //MOVE TO NEXT RECORD
                if (inc.index < dt.Rows.Count - 1)
                {
                    inc.index++;
                }
                Label1.Text = dt.Rows[inc.index]["Ques"].ToString();
                currentSelected = dt.Rows[inc.index]["Rop"].ToString();//Get selected item
                string[] items = { dt.Rows[inc.index]["Op1"].ToString(), dt.Rows[inc.index]["Op2"].ToString(), dt.Rows[inc.index]["Op3"].ToString(), dt.Rows[inc.index]["Op4"].ToString() };
                RadioButtonList1.DataSource = items;
                RadioButtonList1.DataBind();
    
            }
    
            protected void Previous_Click(object sender, EventArgs e)
            {
                if (RadioButtonList1.SelectedIndex > -1)
                {
                    //SAVE ANSWERS
                    save_answer();
                }
                //MOVE TO PREVIOUS RECORD
                if (inc.index > 0)
                {
                    inc.index--;
                }
                Label1.Text = dt.Rows[inc.index]["Ques"].ToString();
                currentSelected = dt.Rows[inc.index]["Rop"].ToString();//Get selected item
                string[] items = { dt.Rows[inc.index]["Op1"].ToString(), dt.Rows[inc.index]["Op2"].ToString(), dt.Rows[inc.index]["Op3"].ToString(), dt.Rows[inc.index]["Op4"].ToString() };
                RadioButtonList1.DataSource = items;
                RadioButtonList1.DataBind();
    
            }
    
            protected void Last_Click(object sender, EventArgs e)
            {
                if (RadioButtonList1.SelectedIndex > -1)
                {
                    //SAVE ANSWERS
                    save_answer();
                }
                //MOVE TO LAST RECORD
                inc.index = dt.Rows.Count - 1;
                Label1.Text = dt.Rows[inc.index]["Ques"].ToString();
                currentSelected = dt.Rows[inc.index]["Rop"].ToString();//Get selected item
                string[] items = { dt.Rows[inc.index]["Op1"].ToString(), dt.Rows[inc.index]["Op2"].ToString(), dt.Rows[inc.index]["Op3"].ToString(), dt.Rows[inc.index]["Op4"].ToString() };
                RadioButtonList1.DataSource = items;
                RadioButtonList1.DataBind();
    
            }
    
            private void save_answer()
            {
                SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Constr"].ConnectionString);
                conn.Open();
                string Rop;
                Rop = RadioButtonList1.SelectedItem.Value;
                SqlCommand cmd = new SqlCommand("Update Questions set Rop=@Rop where QID=@QID", conn);
                cmd.Parameters.Add("@Rop", SqlDbType.VarChar);
                cmd.Parameters.Add("@QID", SqlDbType.Int);
                cmd.Parameters["@Rop"].Value = Rop;
                cmd.Parameters["@QID"].Value = inc.index + 1;
                cmd.ExecuteNonQuery();
                conn.Close();
    
            }
            protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
            {
                ViewState[inc.index.ToString()] = RadioButtonList1.SelectedIndex;
            }
            protected void Button1_Click(object sender, EventArgs e)
            {
                RadioButtonList1.SelectedIndex = 3;
            }
    
            protected void RadioButtonList1_DataBound(object sender, EventArgs e)
            {
                var item = RadioButtonList1.Items.FindByValue(currentSelected);
                if (item != null)
                {
                    item.Selected = true;
                }
    
            }
        }

    Best Regards,

    Candice Zhou

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 30, 2015 9:26 AM