How to bind to a group of radio buttons

4 posts, 0 answers
  1. Sy
    Sy avatar
    6 posts
    Member since:
    Nov 2011

    Posted 16 Dec 2011 Link to this post

    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?

  2. Kevin
    Kevin avatar
    360 posts
    Member since:
    Jul 2012

    Posted 19 Dec 2011 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Sy
    Sy avatar
    6 posts
    Member since:
    Nov 2011

    Posted 19 Dec 2011 Link to this post

    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>
  5. Kevin
    Kevin avatar
    360 posts
    Member since:
    Jul 2012

    Posted 20 Dec 2011 Link to this post

    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.
Back to Top