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
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.
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
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
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
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
All ok.
Regards, Ida