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
JoeUSAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.