Link to home
Start Free TrialLog in
Avatar of Brian
BrianFlag for United States of America

asked on

How to retrieve RadioButtonList Values

Hello Experts,

I'm creating an application with the following specification below. Please see what I need help with below the specifications of my application.

Specifications:

- User must select a value from a RadioButtonList Control. The values are retrieved from a DB to the RadioButtonList Control.

- If a user select the word ONSITE which has a value of 20000 then it will display a TextBox Control from a Panel Control called panel_Onsite.

- If a user select the word OFFISTE which has a value of 20001 then it will display a TextBox and FileUpload Control from a Panel Control called panel_Offsite.

What I need help with:

- I need to display the selected value from the RadioButtonList Control along with the Controls within the Panel Control from the selected RadioButtonList Control on Page_Load.

I have all steps working fine except for the step mentioned above.
Avatar of Obadiah Christopher
Obadiah Christopher
Flag of India image

rbList.SelectedItem.Text;
Avatar of Brian

ASKER

Hi informaniac,

I was able to use the following below to retrieve the value for the RadioButtonList Control. IS this the correct way? Also, my other problem that I mentioned is that when the Page Loads for the first time I need to put a selection in the RadioButtonList control but also display the Panel from the RadioButtonList control along with that Panels Controls and it's data.

protected void Page_Load(object sender, EventArgs e)
    {
        lblVerificationFormFileName.Visible = false;
        lblFileSize.Visible = false;
        lblInsertError.Visible = false;
        panel_Onsite.Visible = false;
        panel_Offsite.Visible = false;

        string FirstName = Convert.ToString(Session["fname"]);
        string LastName = Convert.ToString(Session["lname"]);
        string EmpID = Convert.ToString(Session["empid"]);

        lblFullNameSession.Text = "Hello, " + FirstName + " " + LastName;
        hf_emp_id.Value = EmpID;

        if (!IsPostBack)
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["WellnessTracker"].ConnectionString);

            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "RetrievePreventiveHealthScreeningsValuesByEMP_ID";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = conn;

            cmd.Parameters.AddWithValue("@emp_id", SqlDbType.Int).Value = EmpID;

            DataTable dtPreventiveScreenings = new DataTable("Modify");

            SqlDataAdapter adp = new SqlDataAdapter();

            try
            {
                conn.Open();

                adp.SelectCommand = cmd;
                adp.Fill(dtPreventiveScreenings);

                if ((dtPreventiveScreenings != null))
                {
                    DataRow data = dtPreventiveScreenings.Rows[0];
                    hf_phs_id.Value = data["phs_id"].ToString();

                    lblVerificationFormFileName.Text = "No file is uploaded";

                    foreach (DataRow Row in dtPreventiveScreenings.Rows)
                    {
                        if (!(Row.IsNull("psv_id")))
                        {
                            rbl_PreventiveHealthScreenings.Items.FindByValue(Row["psv_id"].ToString()).Selected = true;
                        }
                    }

                    txtOnsiteDateCompleted.Text = string.Empty;
                    if (data["phs_date"] != DBNull.Value && !string.IsNullOrEmpty(Convert.ToString(data["phs_date"])))
                        txtOnsiteDateCompleted.Text = Convert.ToDateTime(data["phs_date"]).ToShortDateString();

                    txtOffsiteDateCompleted.Text = string.Empty;
                    if (data["phs_date"] != DBNull.Value && !string.IsNullOrEmpty(Convert.ToString(data["phs_date"])))
                        txtOffsiteDateCompleted.Text = Convert.ToDateTime(data["phs_date"]).ToShortDateString();

                    string phs_pdf_filename = null;
                    if (data["phs_pdf_filename"] != DBNull.Value)
                    {
                        phs_pdf_filename = data["phs_pdf_filename"].ToString();
                    }

                    if (!string.IsNullOrEmpty(phs_pdf_filename))
                    {
                        lblVerificationFormFileName.Visible = true;
                        lblVerificationFormFileName.Text = "Your File has been uploaded: " + phs_pdf_filename;
                    }

                    if (data["phs_section_complete"].ToString() == "1")
                    {
                        btn_PreventiveScreenings.Enabled = false;
                        cb_PreventiveScreenings.Checked = true;
                    }

                    else
                    {
                        cb_PreventiveScreenings.Checked = false;
                    }
                }
            }

            catch (Exception ex)
            {
                ex.Message.ToString();
            }

            finally
            {
                conn.Close();
            }
        }
    }
ASKER CERTIFIED SOLUTION
Avatar of Obadiah Christopher
Obadiah Christopher
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Brian

ASKER

Yes, that may work. I did not think of that. Can you help me implement that?
Avatar of Brian

ASKER

@informaniac,

Can you still help with this?
Sorry mate. It was 1am when I had replied to you. :). So how can I help you with implementing this. What exactly are you not able to get.

You said that you were able to implement the logic, only thing is you were not able to do it in the pageload. Can you show some of that code here
Avatar of Brian

ASKER

Hi informaniac,

No worry, I wasn't sure if you where still able to assist. Thank you for replying back and helping out. I was able to figure it out late last night after I made my last post. But would appreciate it if you could look over the code I implemented to see if I'm doing it correctly and maybe you have a better way that you can show me.

Please notice the following code below this line. This is what I added below. This is how I'm displaying the RadioButtonList value from the DB along with displaying the Panel Control that is related to the selected RadioButtonList value along with the Panel's Controls.

                    if (data["psv_id"].ToString() == "20000")
                    {
                        panel_Onsite.Visible = true;
                        rbl_PreventiveHealthScreenings.Items.FindByValue(data["psv_id"].ToString()).Selected = true;
                    }

                    if (data["psv_id"].ToString() == "20001")
                    {
                        panel_Offsite.Visible = true;
                        rbl_PreventiveHealthScreenings.Items.FindByValue(data["psv_id"].ToString()).Selected = true;
                    }




protected void Page_Load(object sender, EventArgs e)
    {
        lblVerificationFormFileName.Visible = false;
        lblVerificationFormTypeError.Visible = false;
        lblFileSize.Visible = false;
        lblInsertError.Visible = false;
        panel_Onsite.Visible = false;
        panel_Offsite.Visible = false;

        EmployeeLoginInfo();
        SectionsCompleted();
        RetrievePreventiveHealthScreeningValues();

        string FirstName = Convert.ToString(Session["fname"]);
        string LastName = Convert.ToString(Session["lname"]);
        string EmpID = Convert.ToString(Session["empid"]);

        lblFullNameSession.Text = "Hello, " + FirstName + " " + LastName;
        hf_emp_id.Value = EmpID;

        if (!IsPostBack)
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["WellnessTracker"].ConnectionString);

            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "RetrievePreventiveHealthScreeningsValuesByEMP_ID";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = conn;

            // cmd.Parameters.AddWithValue("@emp_id", SqlDbType.Int).Value = emp_id;
            cmd.Parameters.AddWithValue("@emp_id", SqlDbType.Int).Value = EmpID;

            DataTable dtPreventiveScreenings = new DataTable("Modify");

            SqlDataAdapter adp = new SqlDataAdapter();

            try
            {
                conn.Open();

                adp.SelectCommand = cmd;
                adp.Fill(dtPreventiveScreenings);

                if ((dtPreventiveScreenings != null))
                {
                    DataRow data = dtPreventiveScreenings.Rows[0];
                    hf_phs_id.Value = data["phs_id"].ToString();

                    lblVerificationFormFileName.Text = "No file is uploaded";

                    //foreach (DataRow Row in dtPreventiveScreenings.Rows)
                    //{
                    //    if (!(Row.IsNull("psv_id")))
                    //    {
                    //        rbl_PreventiveHealthScreenings.Items.FindByValue(Row["psv_id"].ToString()).Selected = true;
                    //    }
                    //}

                    if (data["psv_id"].ToString() == "20000")
                    {
                        panel_Onsite.Visible = true;
                        rbl_PreventiveHealthScreenings.Items.FindByValue(data["psv_id"].ToString()).Selected = true;
                    }

                    if (data["psv_id"].ToString() == "20001")
                    {
                        panel_Offsite.Visible = true;
                        rbl_PreventiveHealthScreenings.Items.FindByValue(data["psv_id"].ToString()).Selected = true;
                    }

                    txtOnsiteDateCompleted.Text = string.Empty;
                    if (data["phs_date"] != DBNull.Value && !string.IsNullOrEmpty(Convert.ToString(data["phs_date"])))
                        txtOnsiteDateCompleted.Text = Convert.ToDateTime(data["phs_date"]).ToShortDateString();

                    txtOffsiteDateCompleted.Text = string.Empty;
                    if (data["phs_date"] != DBNull.Value && !string.IsNullOrEmpty(Convert.ToString(data["phs_date"])))
                        txtOffsiteDateCompleted.Text = Convert.ToDateTime(data["phs_date"]).ToShortDateString();

                    string phs_pdf_filename = null;
                    if (data["phs_pdf_filename"] != DBNull.Value)
                    {
                        phs_pdf_filename = data["phs_pdf_filename"].ToString();
                    }

                    if (!string.IsNullOrEmpty(phs_pdf_filename))
                    {
                        lblVerificationFormFileName.Visible = true;
                        lblVerificationFormFileName.Text = "Your File has been uploaded: " + phs_pdf_filename;
                    }

                    if (data["phs_section_complete"].ToString() == "1")
                    {
                        btn_PreventiveScreenings.Enabled = false;
                        cb_PreventiveScreenings.Checked = true;
                    }

                    else
                    {
                        cb_PreventiveScreenings.Checked = false;
                    }
                }
            }

            catch (Exception ex)
            {
                ex.Message.ToString();
            }

            finally
            {
                conn.Close();
            }
        }
    }
Avatar of Brian

ASKER

@informaniac,

Hi, can you please over look what I added. I would like to award you points but wanted to ask you that one last question.
Avatar of Brian

ASKER

@informaniac

Can you please answer my question in post ID: 38320691 before I close this post??