Datalist and RadioButton

R

Rob Risner

Is there a way to use a RadioButton web control in a datalist template
column without the button getting a unique name? It doesn't allow a single
radio button to be clicked.

Thanks,
Rob
 
A

Alvin Bruney [MVP]

Radiobuttonlists implement inamingcontainer which forces unique id's. What
are you trying to do? Maybe there is a workaround
 
R

Rob Risner

I am trying to display a list of images and allow the user to click on a
radio button to select one. I thought about the Radiobuttonlist but that
seems to want a Datatextfield to show one item from the database.

Thanks for the quick reply,
Rob

Alvin Bruney said:
Radiobuttonlists implement inamingcontainer which forces unique id's. What
are you trying to do? Maybe there is a workaround

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/27cok
Rob Risner said:
Is there a way to use a RadioButton web control in a datalist template
column without the button getting a unique name? It doesn't allow a
single radio button to be clicked.

Thanks,
Rob
 
J

Jeffrey Tan[MSFT]

Hi Rob,

Based on my understanding, you nested RadioButton in the DataList control,
and you want to handle the CheckedChanged event for different RadioButton.

Suppose you are doing like this:
<asp:DataList id="DataList1" runat="server">
<ItemTemplate>
<asp:RadioButton Runat="server" ID="rb"></asp:RadioButton>
</ItemTemplate>
</asp:DataList>

To handle the RadioButton's CheckedChanged event for each row, you should
hook into DataList.ItemCreated event, and add the event handler for it. Do
like this:

private void DataList1_ItemCreated(object sender,
System.Web.UI.WebControls.DataListItemEventArgs e)
{
if(e.Item.ItemType==ListItemType.Item)
{
RadioButton rb=e.Item.FindControl("rb") as RadioButton;
rb.CheckedChanged+=new EventHandler(rb_CheckedChanged);
}
}

private void rb_CheckedChanged(object sender, EventArgs e)
{
}

Then, in the rb_CheckedChanged, you can do your program logic.

===============================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi Rob,

Does my reply make sense to you? Do you still have any concern on this
issue?

Please feel free to post. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
R

Rob Risner

I tried your method and could not get it to work. I've stepped through and
it's adding a new event handler to the rb.CheckedChanged but rb_CheckChanged
is not being called when the radio button is being click during a post back.
This also does not solve the problem with the user being able to click
multiple radio buttons and their name uniquness. I've worked around this by
simply using a radio button list instead.

Thanks,
Rob
 
J

Jeffrey Tan[MSFT]

Hi Rob,

Thanks very much for your feedback.

For your RadioButton event does not fire problem, it is a common issue.
Normally, it is because you did not determine the postback, and re-bind
your datagrid every postback time. Like this:

private void Page_Load(object sender, System.EventArgs e)
{
DataSet ds=new DataSet ();
SqlDataAdapter adapter=new SqlDataAdapter("select * from
jobs","server=localhost;database=pubs;uid=sa;pwd=");
adapter.Fill(ds);
DataGrid1.DataSource=ds;
DataGrid1.DataBind();
}

Through this way, your datagrid's child controls(Such as RadioButton) will
be re-created every postback, its status will be re-freshed every time, so
its event will not fire. Instead, you should only bind the DataGrid at the
first time, like this:

private void Page_Load(object sender, System.EventArgs e)
{
if(!this.IsPostBack)
{
DataSet ds=new DataSet ();
SqlDataAdapter adapter=new SqlDataAdapter("select * from
jobs","server=localhost;database=pubs;uid=sa;pwd=");
adapter.Fill(ds);
DataGrid1.DataSource=ds;
DataGrid1.DataBind();
}
}

Through this way, the event should fire for you.

If this is not your problem, please pasted some code snippet to reproduce
your problem, I will figure out the problem for you.
========================================

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
R

Rob Risner

There is still the problem with unique RadioButtons.

The datalist changes the names to
DataList1__ctl0_rb
DataList1__ctl1_rb
Etc...

This allows for mulitple RadioButtons to be checked.
 
J

Jeffrey Tan[MSFT]

Hi Rob,

Thanks very much for your feedback.

Oh, yes, in Asp.net Repeater controls, you can not make them Mutually
Exclusive, this is a known issue, please refer to:
"BUG: Radio Buttons Are Not Mutually Exclusive When Used in a Repeater
Server Control"
http://support.microsoft.com/default.aspx?scid=kb;en-us;316495

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi Rob,

Does my reply make sense to you? Do you still have any concern on this
issue?

Please feel free to post. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
S

Sascha Bötzel

Hi everyone,
I'm currently encountering the same problem. Microsoft states, it's a
bug, but there seems to be no bug fix for this.
Does anybody know a workaround ?


Thanx in advance

Sascha
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top