[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 657
  • Last Modified:

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.
0
asp_net2
Asked:
asp_net2
  • 6
  • 3
1 Solution
 
informaniacCommented:
rbList.SelectedItem.Text;
0
 
asp_net2Author Commented:
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();
            }
        }
    }
0
 
informaniacCommented:
Can't you add an if condition

if(Row["psv_id"].ToString() == "2000")
{
         pnl1.Visible = true;
}

Selecting the value from radiobuttonlist is proper.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
asp_net2Author Commented:
Yes, that may work. I did not think of that. Can you help me implement that?
0
 
asp_net2Author Commented:
@informaniac,

Can you still help with this?
0
 
informaniacCommented:
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
0
 
asp_net2Author Commented:
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();
            }
        }
    }
0
 
asp_net2Author Commented:
@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.
0
 
asp_net2Author Commented:
@informaniac

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

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now