Solved

How to get value from RadioButtonList created from code behind

Posted on 2010-09-07
9
1,036 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

758 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

21 Experts available now in Live!

Get 1:1 Help Now