This is a migrated thread and some comments may be shown as answers.

How to bind to a group of radio buttons

3 Answers 415 Views
Button
This is a migrated thread and some comments may be shown as answers.
Sy
Top achievements
Rank 1
Sy asked on 17 Dec 2011, 05:54 AM
Let's say that I have two radio buttons that I'd like to bind to a database. Now, both fields are bound, but only one of them is selected based on the data in the database.  Would the following code work?

<telerik:RadButton ID="ID1" runat="server" ToggleType="Radio" GroupName="MyGroup" 
 ButtonType="ToggleButton" AutoPostBack="false" Text="Choice 1" Checked='<%# Bind("datasourcefield")=="Choice 1" %>'>    
</telerik:RadButton><br />
<telerik:RadButton ID="ID2" runat="server" ToggleType="Radio" GroupName="MyGroup" 
 ButtonType="ToggleButton" AutoPostBack="false" Text="Choice 2" Checked='<%# Bind("datasourcefield")=="Choice 2" %>'>    
</telerik:RadButton><br />



If this code would not work, what would?

3 Answers, 1 is accepted

Sort by
0
Kevin
Top achievements
Rank 2
answered on 19 Dec 2011, 03:02 PM
Hello Sy,

You won't be able to use two-way binding, in the way you're implementing it, since the control will not be able to sync the value with the database column. I would suggest changing Bind to Eval and then writing code to update that column accordingly.

I hope that helps.
0
Sy
Top achievements
Rank 1
answered on 20 Dec 2011, 02:30 AM
Would this work?  Again, the idea is this: I have multiple radio buttons, only one of which can appear selected when the user reaches the form. If the appropriate column, "DataSourceColumn," has the value "Text1," then the first radio button should be checked. I would hope that there is a way to accomplish what I need without having to use code behind and deal with this problem programmatically. Thanks.

<telerik:RadButton ID="ID1" runat="server" ToggleType="Radio" GroupName="Group1"
 ButtonType="ToggleButton" AutoPostBack="false" Text="Text1"  
Checked='<%# Eval("DataSourceColumn")=="Text1" ? true : false %>' >     </telerik:RadButton><br /> <telerik:RadButton ID="ID2" runat="server" ToggleType="Radio" GroupName="Group1"   ButtonType="ToggleButton" AutoPostBack="false" Text="Text2" 
Checked='<%# Eval("DataSourceColumn")=="Text2" ? true : false %>' >     </telerik:RadButton>
0
Kevin
Top achievements
Rank 2
answered on 20 Dec 2011, 03:12 PM
Hello Sy,

The following approach would work if you're only using it to display what is checked. What I was trying to say about the code-behind, is that if the user decides to check off the other button, it will not automatically persist the change to the database without the use of custom code.
Tags
Button
Asked by
Sy
Top achievements
Rank 1
Answers by
Kevin
Top achievements
Rank 2
Sy
Top achievements
Rank 1
Share this question
or