0

Hello,I've a gridview control and inside a cell, i've put 3 radiobuttons under a same group. I'm not able to get which radio button is selected through checked property. As checked property is giving false always, when i check it with breakpoint. How can i get a the selected value from a group of radiobuttons. Thank You

3
Contributors
4
Replies
17
Views
4 Years
Discussion Span
Last Post by johnny-bravo
1

Can you provide the code for the GridView, where you're databinding it, and the code where you're trying to get the radio button values?

0
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using _database;

public partial class takequiz : System.Web.UI.Page
{
    string str;
    string jobtitle;
    string[] choice = new string[200];
    int score=0,index=0,jobid;
    database db = new database();
    SqlDataReader reader;
    protected void Page_Load(object sender, EventArgs e)
    {
        jobtitle = Session["title"].ToString();
        str = "select jobid from job where jobtitle='" + jobtitle + "'";
        reader = db.data_read(str);
        if (reader.Read())
        {
            jobid = int.Parse(reader.GetSqlValue(0).ToString());
        }
        DataSet ds = new DataSet();
        str = "select slno,question,choice1,choice2,choice3 from questions where jobid=" + jobid + "";

        grdquestions.DataSource = db.data_set(str);
        grdquestions.DataBind();
    }

    protected void btnsubmit_Click(object sender, EventArgs e)
    {
        jobtitle = Session["title"].ToString();
        str = "select jobid from job where jobtitle='" + jobtitle + "'";
        reader = db.data_read(str);
        if (reader.Read())
        {
             jobid = int.Parse(reader.GetSqlValue(0).ToString());
        }

        foreach (GridViewRow r in grdquestions.Rows)

        {
            string opt;

            RadioButton radio1;
            RadioButton radio2;
            RadioButton radio3;

            radio1 = (RadioButton)r.FindControl("rdb1");
            radio2 = (RadioButton)r.FindControl("rdb2");
            radio3 = (RadioButton)r.FindControl("rdb3");

            if ( (radio1.Checked == true || radio2.Checked == true) || radio3.Checked == true) 
            {
                if (radio1.Checked == true)
                {
                   // choice[index++] = radio1.FindControl("rdb1").ToString();
                    opt = "choice1";
                    str = "select choice1 from questions where jobid="+jobid+"";
                    reader = db.data_read(str);
                    if (reader.Read())
                    {
                        choice[index++] = reader.GetSqlValue(0).ToString();
                        Label1.Text = reader.GetSqlValue(0).ToString();
                    }


                }
                else if (radio2.Checked == true)
                {
                    //choice[index++] = radio2.FindControl("rdb2").ToString();
                    opt = "choice2";
                    str = "select choice2 from questions where jobid=" + jobid + "";
                    reader = db.data_read(str);
                    if (reader.Read())
                    {
                        choice[index++] = reader.GetSqlValue(0).ToString();
                    }

                }
                else
                {
                    opt = "choice3";
                    str = "select choice3 from questions where jobid=" + jobid + "";
                    reader = db.data_read(str);
                    if (reader.Read())
                    {
                        choice[index++] = reader.GetSqlValue(0).ToString();
                    }
                    choice[index++] = radio2.FindControl("rdb3").ToString();
                }
            }
        }
        index=0;
        str="select answer from questions where jobid="+jobid+"";
        reader = db.data_read(str);
        while (reader.Read())
        {
            if (choice[index++] == reader.GetSqlValue(0).ToString())
                score++;
        }
        str = "insert into score (total) values (" + score + ")";
        db.database_command(str);
        Session["total"] = score;
        Response.Redirect("score.aspx");               
    }
}
1

Be sure that in the page load event you put the databinding of the gridview in a !IsPostBack block. Right now the values in the gridview are getting reset before the button's click event is executed because the page load event occurs before the click event during a postback.

Votes + Comments
Thank You. This helped
0

no i m getting the same error. I have put the binding code in !IsPostBack but when i clicked on the radio button and while debugging it gives OBJECT REFERENCE NOT SET TO AN INSTANCE OF OBJECT.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.