Solved

How to get value from RadioButtonList created from code behind

Posted on 2010-09-07
9
1,039 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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
 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Output Caching in IIS 2 28
ASP.net Show "please wait " message as page loads 2 41
ck editor is taking too much time to be loaded in asp 1 17
ASP.NET MVC identity 6 26
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…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

776 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