locked
Making radio button keep selection on gridview page index changing RRS feed

  • Question

  • User1406973109 posted

    Good morning guys,

    Please i need help with some code, i am saving database questionnaires using radio buttons to database but i want to keep the selection on page index changing, such that when the user changes to the next page the other pages still have their selections, this is my code:

     protected void grdVQuestions_PageIndexChanging(object sender, GridViewPageEventArgs e)
            {
                if (grdVQuestions.PageIndex > 0)
                    btnPrevious.Enabled = true;
                
                if (grdVQuestions.PageCount - 1 == grdVQuestions.PageIndex)
                    btnNext.Enabled = false;
                
                SaveState();
                GetState();
            }
    
    
    private void SaveState()
            {
                using(var db = new ExamDbContext())
                {
                    string opt = "0";
                    foreach (GridViewRow grows in grdVQuestions.Rows)
                    {
                        RadioButton rdbOption1 = (RadioButton)grows.FindControl("rdbOption1");
                        RadioButton rdbOption2 = (RadioButton)grows.FindControl("rdbOption2");
                        RadioButton rdbOption3 = (RadioButton)grows.FindControl("rdbOption3");
                        RadioButton rdbOption4 = (RadioButton)grows.FindControl("rdbOption4");
    
                        Label lblQuest = (Label)grows.FindControl("lblQuestion");
    
                        if (rdbOption1.Checked == true)
                        {
                            opt = "1";
                        }
                        else if (rdbOption2.Checked == true)
                        {
                            opt = "2";
                        }
                        else if (rdbOption3.Checked == true)
                        {
                            opt = "3";
                        }
                        else if (rdbOption4.Checked == true)
                            opt = "4";
    
                        if (CheckExistence() != true)
                        {
                            var examDetails = new ExamAudit()
                            {
                                BeginDate = DateTime.Today,
                                EndDate = DateTime.Today.AddDays(1),
                                Question = lblQuest.Text,
                                InstituteCode = strInstituteCode,
                                Subject = strSubjectId,
                                UserDetails = strUser,
                                Selection = opt,
                                PageIndex = grdVQuestions.PageIndex
                            };
                            db.ExamAudits.Add(examDetails);
                            db.SaveChanges();
                        }
                    }
               }
            }
    
            private void GetState()
            {
                using (var db = new ExamDbContext())
                {
                    string opt;
                    foreach (GridViewRow grows in grdVQuestions.Rows)
                    {
                        RadioButton rdbOption1 = (RadioButton)grows.FindControl("rdbOption1");
                        RadioButton rdbOption2 = (RadioButton)grows.FindControl("rdbOption2");
                        RadioButton rdbOption3 = (RadioButton)grows.FindControl("rdbOption3");
                        RadioButton rdbOption4 = (RadioButton)grows.FindControl("rdbOption4");
    
                        Label lblQuest = (Label)grows.FindControl("lblQuestion");
    
                        var studAnswers = from c in db.ExamAudits
                                          where c.Subject == strSubjectId && c.BeginDate == DateTime.Today && c.InstituteCode == strInstituteCode && c.UserDetails == strUser && c.Question == lblQuest.Text && c.PageIndex == grdVQuestions.PageIndex 
                                          select c;
                        foreach (var item in studAnswers)
                        {
                            opt = item.Selection;
                            //grdVQuestions.PageIndex = item.PageIndex;
                            if (opt == "1")
                                rdbOption1.Checked = true;
                            else if (opt == "2")
                                rdbOption2.Checked = true;
                            else if (opt == "3")
                                rdbOption3.Checked = true;
                            else if (opt == "4")
                                rdbOption4.Checked = true;
                        }
                    }
                }
            }
    

    Saturday, May 14, 2016 8:27 AM

Answers

  • User-271186128 posted

    Hi Tim,

    According to your code, since you save the selected option into database. After page index change, you need to rebind the GridView. So, I suggest you could re-select the radio button in the GridView RowDataBound event.

    In the GridView, you can use a label to bind the Selection field. Then, in the GridView RowDataBound event, you can use FindControl method to find this label and the radio buttons. Then, you can according the label text value to select the radio button. Code like this:

    protected void grdVQuestions_PageIndexChanging(object sender, GridViewPageEventArgs e)
            {
                if (grdVQuestions.PageIndex > 0)
                    btnPrevious.Enabled = true;
                
                if (grdVQuestions.PageCount - 1 == grdVQuestions.PageIndex)
                    btnNext.Enabled = false;
                
                SaveState();
                //query the ExamAudits table
                //rebind GridView
            }
    
      protected void grdVQuestions_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    //using FindControl method to find the label and radio button
                    //e.Row.FindControl("labelID")
    
                    //according to the label text value to change radio button checked property.
                }
            }

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 16, 2016 7:37 AM

All replies

  • User-271186128 posted

    Hi timotech,

    I suggest you could refer to the following articles, you can use ViewState or Session to store the selected checkboxes, when page index change, you need to recheck the checkbox

    http://www.aspsnippets.com/Articles/Preserving-state-of-Checkboxes-while-paging-in-ASP.Net-GridView-Control.aspx

    http://www.codeproject.com/Articles/31849/Maintaining-States-of-Selected-CheckBoxes-in-Diffe

    Best regards,

    Dillion

    Saturday, May 14, 2016 10:38 AM
  • User1406973109 posted
    Hi Dillon,
    Thanks so much for your reply, sorry i've not replied since, i've been stuck with trying to implement the links you recommended.
    The place i'm stuck is that i do not understand how to implement the samples for 4 radio buttons, if you look at my initial code posted, where will i use the viewstate array to
    store the selected option, and how will i query it back for the selected option

    Thanks

    Tim
    Saturday, May 14, 2016 9:00 PM
  • User-271186128 posted

    Hi Tim,

    According to your code, since you save the selected option into database. After page index change, you need to rebind the GridView. So, I suggest you could re-select the radio button in the GridView RowDataBound event.

    In the GridView, you can use a label to bind the Selection field. Then, in the GridView RowDataBound event, you can use FindControl method to find this label and the radio buttons. Then, you can according the label text value to select the radio button. Code like this:

    protected void grdVQuestions_PageIndexChanging(object sender, GridViewPageEventArgs e)
            {
                if (grdVQuestions.PageIndex > 0)
                    btnPrevious.Enabled = true;
                
                if (grdVQuestions.PageCount - 1 == grdVQuestions.PageIndex)
                    btnNext.Enabled = false;
                
                SaveState();
                //query the ExamAudits table
                //rebind GridView
            }
    
      protected void grdVQuestions_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    //using FindControl method to find the label and radio button
                    //e.Row.FindControl("labelID")
    
                    //according to the label text value to change radio button checked property.
                }
            }

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 16, 2016 7:37 AM