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

Two-Way databinding radgrid / radiobutton

6 Answers 189 Views
Grid
This is a migrated thread and some comments may be shown as answers.
martin
Top achievements
Rank 1
martin asked on 12 Sep 2008, 02:46 PM
Hi,

I'm trying to use asp:radiobuttons in a radgridview, but I'm experiencing some problems. On each row two radiobuttons for male/female. When not in Editmode they have to be disabled, when in editmode they have to be enabled.
I use the code-behind function OnItemDataBound to "translate" to databasevalue M or F to check the right radiobutton. So far so good.
in my .aspx file:

<telerik

:GridTemplateColumn HeaderText="Male" UniqueName="Gender"> <ItemTemplate>
 <asp:RadioButton ID="RadioButtonMaleItem" runat="server" GroupName="" Enabled="false"/>
</ItemTemplate>
<EditItemTemplate>
<asp:RadioButton ID="RadioButtonMaleEditItem" runat="server" GroupName="MF" />
</
EditItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn HeaderText="Female" UniqueName="Female">
<
ItemTemplate>
<asp:RadioButton ID="RadioButtonFemaleItem" runat="server" GroupName="MF" Enabled="false" />
</ItemTemplate>
<EditItemTemplate>
<asp:RadioButton ID="RadioButtonFemaleItem" runat="server" GroupName="MF" />
</EditItemTemplate>
</telerik:GridTemplateColumn>

When I switch to EditMode the radiobuttons on the editrow are both unchecked how come? And how can I read the new checked value and update the database (similar to OnItemDataBound, but then the other way around)

regards martin


6 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 15 Sep 2008, 09:46 AM
Hi Martin,

To populate a RadioButton in the EditItemTemplate you need to bind it using its Checked property.
 <EditItemTemplate>  
     <asp:RadioButton ID="RadioButton1" Checked='<%#Bind("Male")%>' runat="server"/>  
 </EditItemTemplate>  

You can get the new checked value of the RadioButton in the ItemCommand as shown below.
protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e) 
    { 
        if (e.CommandName == RadGrid.UpdateCommandName) 
        { 
            GridEditableItem editedItem = (GridEditableItem)e.Item; 
            RadioButton rdbtn = (RadioButton)editedItem["Male"].FindControl("RadioButton1"); 
            string strtxt = rdbtn.Checked.ToString(); 
 
        } 
    } 

Thanks
Princy.

0
Ida
Top achievements
Rank 1
answered on 15 Oct 2013, 11:22 AM
Hi Princy,

Could you state how to do the radiobutton binding in VB.NET please?

I have something similar - though instead of Male/Female, I need a radio button list of East/West options.

I need to have the correct radio button selected, based on what whether East/West stored in the database for a row.

Thanks, Ida
0
Princy
Top achievements
Rank 2
answered on 15 Oct 2013, 11:42 AM
Hi Ida,

Please try the following code snippet.

ASPX:
<telerik:GridTemplateColumn HeaderText="Direction">
    <ItemTemplate>
        <%# Eval("Direction")%>
    </ItemTemplate>
    <EditItemTemplate>
        <asp:RadioButtonList ID="RadioButtonList1" runat="server">
            <asp:ListItem Text="East" Value="East"></asp:ListItem>
            <asp:ListItem Text="West" Value="West"></asp:ListItem>
        </asp:RadioButtonList>
    </EditItemTemplate>
</telerik:GridTemplateColumn>

VB:
Protected Sub RadGrid1_ItemDataBound(sender As Object, e As GridItemEventArgs)
    If TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode Then
        Dim edit As GridEditableItem = DirectCast(e.Item, GridEditableItem)
        Dim list As RadioButtonList = DirectCast(edit.FindControl("RadioButtonList1"), RadioButtonList)
    'Default value to be checked
        list.SelectedValue = DataBinder.Eval(edit.DataItem, "Direction").ToString()    
        list.DataBind()
    End If
End Sub

Thanks,
Princy
0
Ida
Top achievements
Rank 1
answered on 15 Oct 2013, 01:23 PM
Thanks for the VB.NET code Princy.

I set up a radio button list as such:

<

 

 

telerik:GridTemplateColumn HeaderText="Shot East/West" Visible="false">

 

 

 

<ItemTemplate>

 

<%

 

# Eval("ShotEW")%>

 

 

 

</ItemTemplate>

 

 

 

<EditItemTemplate>

 

 

 

<asp:RadioButtonList ID="rdShotEastWest" runat="server" >

 

 

 

<asp:ListItem Text="East" Value="1"></asp:ListItem>

 

 

 

<asp:ListItem Text="West" Value="2"></asp:ListItem>

 

 

 

</asp:RadioButtonList>

 

 

 

</EditItemTemplate>

 

 

 

</telerik:GridTemplateColumn>

which should be databinding to the following code-behind:

 

 

 

Protected Sub RDHauls_ItemDataBound(sender As Object, e As GridItemEventArgs) Handles RDHauls.ItemDataBound

 

 

 

' Shot East or West radio button value

 

 

 

 

 

 

 

If TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode Then

 

 

 

 

 

 

 

Dim editShotEW As GridEditableItem = DirectCast(e.Item, GridEditableItem)

 

 

 

Dim listShotEW As RadioButtonList = DirectCast(editShotEW.FindControl("rdShotEastWest"), RadioButtonList)

 

 

 

'Default radio button value to be checked based on ShotEW value from the database

 

 

 

 

 

listShotEW.SelectedValue =

 

DataBinder.Eval(editShotEW.DataItem, "ShotEW").ToString()

 

listShotEW.DataBind()

 

 

End If

 

 

 

 

 

 

 

 

 

 

 

End Sub

Though when I run the code, the IF statement always goes to the End IF and doesnt execute.

Can you advise please?

Thank you, Ida

 

0
Princy
Top achievements
Rank 2
answered on 16 Oct 2013, 05:36 AM
Hi Ida,

You have put the RadioButtonList in the EditItemTemplate of the GridTemplateColumn,hence it will be accessible only in the EditMode.The loop "If TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode Then" is true, when the Grid is in EditMode.Make sure you are trying to access the RadioButtonList ,when the Grid is Editable and not in ViewMode.
Please let me know if any concern.

Thanks,
Princy
0
Ida
Top achievements
Rank 1
answered on 16 Oct 2013, 04:11 PM
Many thanks for your help Princy.

All ok.

Regards, Ida
Tags
Grid
Asked by
martin
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Ida
Top achievements
Rank 1
Share this question
or