• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1134
  • Last Modified:

ASP.NET C# return RadioButtonList selectedvalue

Hi,

This is the code I am using to populate the radiobuttonlist object but I have no idea how to return the selected value in a variable after submitform          

DataSet ds = DB.GetDS(sqlRadioButton, false);
//BIND TO RB
rb.DataSource = ds;
rb.DataValueField = "IndicatorID";
rb.DataTextField = "IndicatorName";
rb.DataBind();
ds.Dispose();

I have seen a few solutions in VB.NET but not in C#
Thanks
0
JoeUS
Asked:
JoeUS
  • 3
  • 3
1 Solution
 
raterusCommented:
The SelectedValue of the RadioButtonLis is what you'll want to use, look at the VB.net examples again, only the syntax is different, the objects/properties you access are all the same.

string rbvalue = rb.SelectedValue();
0
 
JoeUSAuthor Commented:
Here is what i am trying to run

.ASPX
<asp:RadioButtonList id="rb" runat="server" OnSelectedIndexChanged="Index_Changed" AutoPostBack="true"/>
       
----------------------------------
C#    
protected void Page_Load(object sender, EventArgs e)
{
               
sqlRadioButton = ("Select Type, Name,Indicator from LType where Name = '" + strName + "'");
                DataSet ds = DB.GetDS(sqlRadioButton, false);
                //BIND TO RB
                rb.DataSource = ds;
                rb.DataValueField = "Indicator ";
                rb.DataTextField = "Name";
                rb.DataBind();
                ds.Dispose();
}

protected void Index_Changed(Object sender, EventArgs e)
{
            string rbValue;

            foreach (DataRow row in ds.Tables[0].Rows)
            {
                 if (rb.SelectedValue)
                 {
                     rbValue = DB.RowField(row, "Indicator");
                 }
            }
            Label1.Text = "You selected " + rbValue ;
}
0
 
raterusCommented:
For starters, you'll want to do this, else rebinding the radiobutton on each page_load will result in you losing your SelectedValue

protected void Page_Load(object sender, EventArgs e)
{
              if(!Page.IsPostBack()){
sqlRadioButton = ("Select Type, Name,Indicator from LType where Name = '" + strName + "'");
                DataSet ds = DB.GetDS(sqlRadioButton, false);
                //BIND TO RB
                rb.DataSource = ds;
                rb.DataValueField = "Indicator ";
                rb.DataTextField = "Name";
                rb.DataBind();
                ds.Dispose();
               }
}

this may fix your problem altogether, as far as your Index_Changed event, you should already have the Indicator field as the SelectedValue of the radiobutton, no need to go back to your DataSet to retrieve it, I'd just do this...

protected void Index_Changed(Object sender, EventArgs e)
{
            string rbValue = rb.SelectedValue();
            Label1.Text = "You selected " + rbValue;
}
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
JoeUSAuthor Commented:
You were absolutelly right!
It was the (!Page.IsPostBack)

I did notice all together that my SelectedValue kept disappearing but  I just couldn't think of the reason "WHY"

thanks again and points well earned

Have a blessfull Easter!
0
 
JoeUSAuthor Commented:
blissfull
0
 
raterusCommented:
:-) Thanks, only a few can truly have a blissful Easter, the rest think it's just about the easter bunny and eggs.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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