Solved

How to get value from RadioButtonList created from code behind

Posted on 2010-09-07
9
1,037 Views
Last Modified: 2012-08-13
Hi guys.

I have a dynamic crated radiobuttonlist:

aspx:
<asp:RadioButtonList ID="rbTaskTypesID" runat="Server"></asp:RadioButtonList>

aspx.cs:
SqlCommand myCommand = new SqlCommand("select id, name from TaskTypes where TaskCategoryID = "+Convert.ToInt16(TaskCategoriesID)+" order by ID desc", dbconn);
dbconn.Open();
SqlDataReader rs = myCommand.ExecuteReader();
int i = 0;
while(rs.Read())
{
rbTaskTypesID.Items.Insert(i, new ListItem(rs["Name"].ToString(), rs["ID"].ToString()));
}
dbconn.Close();


This works, but in postback, I cant get the value: rbTaskTypesID.Text

Can somebody help please?
0
Comment
Question by:bongii
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 35

Expert Comment

by:mrichmon
ID: 33620548
You want rbTaskTypesID.selectedItem.Text

Once it is a list then you have to access the selected item not the list itself...
0
 
LVL 2

Expert Comment

by:bureshd
ID: 33620604
You will need to access RBL.SelectedItem.Text

If that doesn't work, you may need to verify you are creating the control at the correct time in the correct place. Dynamic controls can be a little confusing if you are not familiar with the page life cycle.
0
 

Author Comment

by:bongii
ID: 33620632
Well rbTaskTypesID.SelectedItem.Text dosen´t work.
Before I created it dynamic from codebehind, rbTaskTypesID.Text worked perfect.
But after I´ve creted the list dynamic, it dosen´t work anymore?
0
 
LVL 2

Expert Comment

by:bureshd
ID: 33620654
Are you creating the control on some event? If you were to create a control on a button on-click event, you would need to create it in that button's on-click function and then recreate the control on the page's pageload. You essentially have to create it twice for it to work.

This link will help:
http://www.aspnettutorials.com/tutorials/controls/control-dym-aspnet2-csharp.aspx

Basically, you need to create the control and store it in some data object like an array, then create it on the next page load.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Expert Comment

by:vinodch
ID: 33620833
I think you have to bind Radiobuttonlist in ispostback condition

if(!ispostback)
{
SqlCommand myCommand = new SqlCommand("select id, name from TaskTypes where TaskCategoryID = "+Convert.ToInt16(TaskCategoriesID)+" order by ID desc", dbconn);
dbconn.Open();
SqlDataReader rs = myCommand.ExecuteReader();
int i = 0;
while(rs.Read())
{
rbTaskTypesID.Items.Insert(i, new ListItem(rs["Name"].ToString(), rs["ID"].ToString()));
}
dbconn.Close();

}

0
 
LVL 4

Expert Comment

by:vinodch
ID: 33620847
Then by using rbTaskTypesID.SelectedValue
0
 

Author Comment

by:bongii
ID: 33620930
vinodch: The problem is, that I need to bind it in postback, because the values is set because of the users chose...
0
 

Author Comment

by:bongii
ID: 33621213
Maybe the problem is that I´m using rbTaskTypesID.Items.Clear(); before binding the list..? How can I solve this?
0
 
LVL 2

Accepted Solution

by:
bureshd earned 500 total points
ID: 33621331
You should only populate the radio button list once per page life cycle. Once you do it on the initial pageload, you shouldn't bind it again on any postback unless you are updating the contents of the control. IF you are just updating what is selected, you shouldn't have to bind the data again. I think if you clarify when you bind all the values and when you make the selection, I could help more. You should never be selecting an item without binding the control's items first.

Pageload()

{

   if (!isPostback)

   {

       BindData()

   }

  

   Update user selection here.

}

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now