Solved

RadioButton not firing click event on first postback from inside nested Gridview

Posted on 2014-01-22
18
4,062 Views
Last Modified: 2014-02-10
I have a nested gridview that I have a group of radio buttons on that are linked to answers for a question in the main gridview.  When I first click on the radio button, it does a post back, but the selection does not change.  If I click the button a second time it posts back and the click event fires.
 
How do I get the first click to fire the click event?
0
Comment
Question by:Dukster131
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
18 Comments
 
LVL 41

Expert Comment

by:guru_sami
ID: 39801046
- Make sure you are not rebinding in the PageLoad on postback
- Are you using UpdatePanel?
Can you share your some relevant code.
0
 
LVL 1

Author Comment

by:Dukster131
ID: 39801069
Thanks for your response.  

The nested gridview contains answers to a Question gridview.  So when I get the questions, it fires the Stored Procedure to get the answers for that question.  Since there is only one question per page, it has to fire on each new page.  However, that is not the problem.  As I said, I can retain the information, it is just a matter of making it select the chosen answer on the first click rather than having to click twice.   When reviewing the questions and answers, if the question was answered, it is checked.  However, to choose something else, it will post back and not change the selection or fire the click event for the radio button.  However, after that, it will function correctly for any radio button choice.  So the first time on the page it will post back when clicking the radio button, but will not change anything.  Subsequently, it will make the appropriate change and fire the click event.
0
 
LVL 1

Author Comment

by:Dukster131
ID: 39801080
Here is the source from the aspx page:

<asp:UpdatePanel ID="UPPNLTest" runat="server"><ContentTemplate>
<asp:Panel ID="pnlTest" runat="server">
    <table class="SMCTest">
         <tr>
        <td >
            <asp:Label ID="lblCount" runat="server" />
        </td>
    </tr>
    <tr>
        <td>
    <asp:GridView ID="gridQuestions" runat="server" AutoGenerateColumns="false"
                ShowHeader = "false" EnableViewState = "true"
                AllowPaging="True" onpageindexchanging="gridQuestions_PageIndexChanging" DataKeyNames="QID"
                PageSize="1" onrowdatabound="gridQuestions_RowDataBound" ShowFooter="True"
                FooterStyle-HorizontalAlign="Left"
                EmptyDataText="There is no test available at this time"
                onselectedindexchanged="gridQuestions_SelectedIndexChanged">
         <Columns>
                <asp:TemplateField HeaderText = "QID" SortExpression="QID" Visible = "false">
                    <ItemTemplate>
                        <asp:Label ID="lblQID" Text= '<%# Bind("QID") %>' runat="server"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Question" SortExpression="QText">
               
                <ItemTemplate>
                    <table class="SMCTest">
                        <tr>
                            <td>
                      <asp:CheckBox ID="chkReview" runat="server" />&nbsp; &nbsp;<asp:Label ID="lblReview" runat="server" Text="Check here to come back to this question at the end of the exam."></asp:Label>
                            </td>
                        </tr>
                        <tr>
                            <td>
                    <asp:Label ID="lblQuestion" runat="server" Text=""></asp:Label>
                            </td>
                        </tr>
                        <tr>
                            <td>
                    <asp:Image ID="imgTest" runat="server" />
                   
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <asp:GridView ID="gridAnswers" runat="server" AutoGenerateColumns = "false" ShowHeader="false" OnRowDataBound="gridAnswers_RowDataBound" EnableViewState="true" DataKeyNames="AID">
                                    <Columns>
                                        <asp:TemplateField>
                                            <ItemTemplate>
                                                <asp:UpdatePanel ID="upRadioButton" runat="server"><ContentTemplate>
                                                <asp:RadioButton ID= "rdb"  runat="server" Text='<%# Bind("AText") %>'  OnCheckedChanged="rb_CheckedChanged" EnableViewState="true" ViewStateMode="Enabled"  AutoPostBack="True" Enabled="true" Visible="true"  />
                                               
                                                </ContentTemplate>
                                               
                                                </asp:UpdatePanel>
                                               
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                    </Columns>
                                   
                                </asp:GridView>
                            </td>
                        </tr>
                    </table>
                </ItemTemplate>
                <FooterTemplate>
                   
                    <asp:Button ID="btnFinish" runat="server" Text = "Finish Exam" Visible = "true"
                        onclick="btnFinish_Click" />
                </FooterTemplate>
                <FooterStyle CssClass="RowAlignLeft"></FooterStyle>
            </asp:TemplateField>

           
        </Columns>

        <PagerSettings FirstPageText="" LastPageText="Finish Test"
                                    Mode="NextPrevious" NextPageText="Save and Continue" PreviousPageText="Previous Question" />
        <FooterStyle CssClass="RowAlignLeft" />
    </asp:GridView>
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 41

Expert Comment

by:guru_sami
ID: 39801563
Nothing is clicking after looking at the code...but try this out...how about you remove the updatepanel from your second grid's radiobutton.

Also just wondering why you have RadioButton, did you consider using RadioButtonList instead?
0
 
LVL 2

Expert Comment

by:Aijaz Chauhan
ID: 39802115
I think this issue is due to Update panel.

First try it by removing all the update panels on your page.

otherwise ,There is a property "UpdateMode" for update panel. Set it to "Always".

I hope this will work.
0
 

Expert Comment

by:anwarul
ID: 39802257
update radio button on event by using trigger
0
 
LVL 1

Author Comment

by:Dukster131
ID: 39803111
I took out all the update panels but it still does not hold the check on the first click.  It does a postback but the OnCheckedChanged event does not fire.  The second time I click it, the OnCheckedChanged event does fire.
0
 
LVL 1

Author Comment

by:Dukster131
ID: 39803132
I am doing RowDataBound events for the Questions and answers.  The following code is what is being used.  Not sure if this is affecting the situation although everything populates correctly from the opening of the page.

 protected void gridQuestions_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int intError;
            intError = 0;
            string strImage;
           
            int intQID = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "QID"));
            string strQuestion = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "QText"));
            if (DataBinder.Eval(e.Row.DataItem, "TestImage") != null)
            {
                strImage = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "TestImage"));
            }
            else
            {
                strImage = "";
            }
            var imgID = e.Row.FindControl("imgTest") as Image;
            var chkReview = e.Row.FindControl("chkReview") as CheckBox;
            var lblQuestion = e.Row.FindControl("lblQuestion") as Label;
            //chkReview.ID = intQID.ToString();

            lblQuestion.Text = strQuestion.Replace("\r\n", "<br /><br />");
            if (strImage != "")
            {
                imgID.ImageUrl = strImage;
                imgID.Visible = true;
            }
            else
            {
                imgID.Visible = false;
            }
           
            intQID = Convert.ToInt32(gridQuestions.DataKeys[e.Row.RowIndex].Value);
            GridView gridAnswers = e.Row.FindControl("gridAnswers") as GridView;

            try
            {
                using (SqlConnection conn = new SqlConnection(connectionString))
                {
                    conn.Open();

                    using (SqlDataAdapter comm = new SqlDataAdapter("dbo.GetSMCStudentTestAnswers", conn))
                    {
                        comm.SelectCommand.CommandType = CommandType.StoredProcedure;

                        comm.SelectCommand.Parameters.AddWithValue("@QID", intQID);

                        DataSet ds = new DataSet();

                        comm.Fill(ds);
                        DataTable dt = ds.Tables[0];

                        //gridAnswers.DataSource = dt.DefaultView;

                        gridAnswers.DataSource = ds;
                        //gridAnswers.EditIndex = -1;
                        gridAnswers.DataBind();
                    }
                }
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                intError = ex.Number;
                if (intError > 0)
                {

                }
            }
            //gridAnswers.EditIndex = -1;
        }
    }
    protected void gridAnswers_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //Get the number of documents per department
            int intAID = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "AID"));
            int intQID = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "QID"));
            var rdb = e.Row.FindControl("rdb") as RadioButton;
            var lblID = e.Row.FindControl("lblTaskID") as Label;
            //GridView gv = (GridView)gridQuestions.Rows[0].FindControl("gridAnswers");
           
            if (DataBinder.Eval(e.Row.DataItem, "AID") != DBNull.Value)
            {
                if (rdb != null)
                {
                rdb.ID = intAID.ToString();
                }
               
                //rdb.GroupName = "Answers";
                //this.Controls.Add(this.rdb);
                rdb.Enabled = true;
                //rdb.Focus();
                rdb.EnableViewState = true;
                //rdb.Visible = true;
                rdb.AutoPostBack = true;
                //rdb.GroupName = "Answers";
                gridQuestions.EditIndex =-1;
                //gv.EditIndex = -1;
            }
           
            }
        }
0
 
LVL 1

Author Comment

by:Dukster131
ID: 39821678
Anybody got any ideas?  I would really like to fix this.
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 39830437
My doubt is somewhere there is some rebinding is happening.
After your page first opens, place a break point on this line:  gridAnswers.DataBind();
Then check the radio button and see if you hit that line.

What does your event handlers look like e.g. Page_Load and onpageindexchanging?
0
 
LVL 1

Author Comment

by:Dukster131
ID: 39835561
I placed a break at the gridAnswers.DataBind() and when I clicked the radio button the first time, it did not stop at the break.

Here is my Page_Load

 if (!IsPostBack)
        {
            CheckStudentsAllowed();
            CheckTestActive();
            if ((User.IsInRole("SMCStudent") && intActiveTest == 1 && intAllowStudents ==1) || (!User.IsInRole("SMCStudent") && intActiveTest ==1))
            {
                NoCache();

                CheckAgreement();
                if (intAgree == 0)
                {
                    pnlAgreement.Visible = true;
                    GetAgreement();
                    pnlSummary.Visible = false;
                    btnReview.Visible = false;
                    btnContinue.Visible = true;
                    //pnlScores.Visible = false;
                    pnlTest.Visible = false;

                }
                else
                {
                    btnAgree.Visible = false;
                    btnReview.Visible = false;
                    btnContinue.Visible = false;
                }
                CheckPreviousTest();
                if (intTester == 1 || intTester==2)
                {
                    CheckEssayCount();
                    if (intEssay > 0)
                    {
                        Session["QTable"] = null;
                        Response.Redirect("TestGridViewEssay.aspx");
                    }
                    else
                    {
                        pnlAgreement.Visible = false;
                        pnlTest.Visible = false;
                        pnlSummary.Visible = false;
                        pnlFinish.Visible = true;
                        lblFinish.Text = "You have already taken this test and may not take it again.";
                        lblFinish.CssClass = "Warning";
                    }
                }
                if (intTester == 3 )
                {
                    getPreviousAnswers();
                }

                getQuestions();
                blModify = false;
                GetAnswerList();

                if (intTester == 3)
                {
                    PopulateCheckedValues();
                    PopulateReviewedValues();
                }
                btnReview.Visible = false;
                btnMissing.Visible = false;
                btnAll.Visible = false;
                //rdb.Attributes.Add("onclick", "javascript:return Validate()");
            }

            else
            {
                pnlAgreement.Visible = false;
                pnlTest.Visible = false;
                pnlSummary.Visible = false;
                pnlFinish.Visible = true;
                lblFinish.Visible = true;
                lblFinish.Text = "There are no active tests to take at this time.";
                lblFinish.CssClass = "Warning";
            }
        }
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 39836964
I am not sure what is wrong with the code and so I tried creating a sample page with a nested GV and radiobutton. I see the radiobutton firing upon first click.

Though I still don't understand, why do you use RadioButton instead of a RadioButtonList?

Edit: Ahhh...I see what's different that's causing the issue. It's setting the ID of the RadioButton in the gvAnswers_RowDataBound.
Comment out this line and your RB will start firing:
  rdb.ID = intAID.ToString();

Open in new window

0
 
LVL 1

Author Comment

by:Dukster131
ID: 39837120
You are correct, however if I do that, then I lose the ID for the radio button and I store the answer in an array that is tied to that ID.  So if they go back in the test, they no longer have the answers for those questions checked.

ArrayList arrChoices = (ArrayList)Session["CHECKED_ITEMS"];
        if (arrChoices != null && arrChoices.Count > 0)
        {
            GridView gv = (GridView)gridQuestions.Rows[0].FindControl("gridAnswers");
            foreach (GridViewRow gvrow in gv.Rows)
            {
                int index = (int)gv.DataKeys[gvrow.RowIndex].Value;
                if (arrChoices.Contains(index))
                {
                    RadioButton myCheckBox = (RadioButton)gvrow.FindControl("" + index);
                    myCheckBox.Checked = true;
                }
            }
        }
the myCheckBox.checked is now getting a null reference value.
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 39837280
But since each of your GridAnswers row will have one and only one radio, I am not sure why you need unique id.

From what I understand just this should work.
RadioButton myCheckBox = (RadioButton)gvrow.FindControl("rdb");


You haven't answered my question yet: Why are you using RadioButton over RadioButtonList. RBL would make all this very easy and you can get rid of your second grid gridAnswers.
 Check my sample page that uses RBL in one of it's column.
0
 
LVL 1

Author Comment

by:Dukster131
ID: 39841799
Well I thought about using a RadioButtonList but I thought a radio button that would grow based on the number of answers(which may change and is randomized) would work better.  I guess once I got the buttons working, I never went back to look at a radio button list.  I will check that out.
0
 
LVL 1

Author Comment

by:Dukster131
ID: 39841905
The reason I was keeping the ID was that if the session was lost, all the answers were stored in the database and I could regenate the AnswerID from that.  Without binding the ID on the answers rowdatabound, how do I bind the answerID to the radiobutton?  If I use ID = '<%# Bind("AID") %>'Instead of "rdb" I get the error The ID property of a control can only be set using the ID attribute in the tag and a simple value
0
 
LVL 41

Accepted Solution

by:
guru_sami earned 500 total points
ID: 39842451
RadioButtonList adjusts itself based on the number of answers. So I still don't see any issue using it. With RBL you get Text and Value for the ListItem, and you can set the Value=AID.

Moreover with RBL you get an implicit selection of only one item at a time. With your case, you will have to explicitly uncheck the other radiobuttons if user want's to change the answer.
With single RadioButton, you just get Text and no Value.
0
 
LVL 1

Author Closing Comment

by:Dukster131
ID: 39847721
Thanks for your help.  I went with the Radiobuttonlist and you are correct, it worked like a charm.  I am tweaking it now to update the testing system, but while testing it on my development machine it works like a charm.  Thanks for your patience and assistance.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developer tools in browsers have been around for a while, yet they are still heavily underused by developers. Developers still fix html or CSS then refresh page to see effect, or they put alert or debugger in JavaScript and then try again and again …
Thoughout my experience working on eCommerce web applications I have seen applications succumbing to increased user demand and throughput. With increased loads the response times started to spike, which leads to user frustration and lost sales. I ha…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question