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:Radio ButtonList >
aspx.cs:
SqlCommand myCommand = new SqlCommand("select id, name from TaskTypes where TaskCategoryID = "+Convert.ToInt16(TaskCate goriesID)+ " 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"].ToStri ng(), rs["ID"].ToString()));
}
dbconn.Close();
This works, but in postback, I cant get the value: rbTaskTypesID.Text
Can somebody help please?
I have a dynamic crated radiobuttonlist:
aspx:
<asp:RadioButtonList ID="rbTaskTypesID" runat="Server"></asp:Radio
aspx.cs:
SqlCommand myCommand = new SqlCommand("select id, name from TaskTypes where TaskCategoryID = "+Convert.ToInt16(TaskCate
dbconn.Open();
SqlDataReader rs = myCommand.ExecuteReader();
int i = 0;
while(rs.Read())
{
rbTaskTypesID.Items.Insert
}
dbconn.Close();
This works, but in postback, I cant get the value: rbTaskTypesID.Text
Can somebody help please?
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.
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.
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?
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.
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(TaskCate goriesID)+ " 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"].ToStri ng(), rs["ID"].ToString()));
}
dbconn.Close();
}
if(!ispostback)
{
SqlCommand myCommand = new SqlCommand("select id, name from TaskTypes where TaskCategoryID = "+Convert.ToInt16(TaskCate
dbconn.Open();
SqlDataReader rs = myCommand.ExecuteReader();
int i = 0;
while(rs.Read())
{
rbTaskTypesID.Items.Insert
}
dbconn.Close();
}
Then by using rbTaskTypesID.SelectedValu e
ASKER
vinodch: The problem is, that I need to bind it in postback, because the values is set because of the users chose...
ASKER
Maybe the problem is that I´m using rbTaskTypesID.Items.Clear( ); before binding the list..? How can I solve this?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Once it is a list then you have to access the selected item not the list itself...