Solved

How to get value from RadioButtonList created from code behind

Posted on 2010-09-07
9
1,041 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

828 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