Link to home
Start Free TrialLog in
Avatar of Jesper Christensen
Jesper Christensen

asked on

How to get value from RadioButtonList created from code behind

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?
Avatar of mrichmon
mrichmon

You want rbTaskTypesID.selectedItem.Text

Once it is a list then you have to access the selected item not the list itself...
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.
Avatar of Jesper Christensen

ASKER

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?
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.
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();

}

Then by using rbTaskTypesID.SelectedValue
vinodch: The problem is, that I need to bind it in postback, because the values is set because of the users chose...
Maybe the problem is that I´m using rbTaskTypesID.Items.Clear(); before binding the list..? How can I solve this?
ASKER CERTIFIED SOLUTION
Avatar of bureshd
bureshd

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial