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
Try binding the RadioButtons using their Checked properties as shown in the code below.
aspx:
<telerik:GridTemplateColumn UniqueName="Male" HeaderText="Male"> |
<ItemTemplate> |
<asp:RadioButton ID="RadioButton1" Enabled="false" runat="server" /> |
</ItemTemplate> |
<EditItemTemplate> |
<asp:RadioButton ID="RadioButton2" GroupName="MF" Checked='<%#Bind("Male")%>' runat="server" /> |
</EditItemTemplate> |
</telerik:GridTemplateColumn> |
<telerik:GridTemplateColumn UniqueName="Female" HeaderText="Female"> |
<ItemTemplate> |
<asp:RadioButton ID="RadioButton3" Enabled="false" runat="server" /> |
</ItemTemplate> |
<EditItemTemplate> |
<asp:RadioButton ID="RadioButton4" GroupName="MF" Checked='<%#Bind("Female")%>' runat="server" /> |
</EditItemTemplate> |
</telerik:GridTemplateColumn> |
You can get the new values in the UpdateCommand/ItemCommand of the grid as shown below.
cs:
protected void RadGrid1_UpdateCommand(object source, GridCommandEventArgs e) |
{ |
if (e.Item is GridEditableItem) |
{ |
GridEditableItem dataItem = (GridEditableItem)e.Item; |
RadioButton rdbtn1 = (RadioButton)dataItem["Male"].FindControl("RadioButton2"); |
RadioButton rdbtn2 = (RadioButton)dataItem["Female"].FindControl("RadioButton4"); |
string strtxt1 = rdbtn1.Checked.ToString(); |
string strtxt2 = rdbtn2.Checked.ToString(); |
} |
} |
Princy.
Hi Princy,
Thx for the answer, but it is not the solution for binding the date to the buttons in the EditTemplateColumn. I cannot Bind("Male"):
DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'Male'.
Martin
Attached to this message, is a small application, which handles a functionality close to the one that you mentioned.
I hope it helps.
Greetings,
Yavor
the Telerik team
Check out Telerik Trainer, the state of the art learning tool for Telerik products.
In your database called test you have two colums: one for male and one for female.
That's to easy :-) I have ONE column in my table named Gender and it contains a F for Female and a M for Male.
I issued a support ticket to telerik 161905 with a demo example.
regards martin
We will review the ticket, and address it shortly. We can continue our communication there, in order to avoid duplicate posts.
Best wishes,
Yavor
the Telerik team
Check out Telerik Trainer, the state of the art learning tool for Telerik products.
The GridTemplateColumns with the radiobuttons:
<telerik:GridTemplateColumn HeaderText="Male" UniqueName="Male" >
<ItemTemplate>
<asp:RadioButton ID="RadioButtonMaleItem" runat="server" Enabled="false"/>
</ItemTemplate>
<EditItemTemplate>
<asp:RadioButton ID="RadioButtonMaleEditItem" runat="server" GroupName="MF" Checked='<%# RadGrid1.MasterTableView.IsItemInserted ? false : (string) Eval("Gender") == "M" %>'/>
</EditItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn HeaderText="Female" UniqueName="Female" >
<ItemTemplate>
<asp:RadioButton ID="RadioButtonFemakeItem" runat="server" Enabled="false" />
</ItemTemplate>
<EditItemTemplate>
<asp:RadioButton ID="RadioButtonFemaleEditItem" runat="server" GroupName="MF" Checked='<%# RadGrid1.MasterTableView.IsItemInserted ? true :(string) Eval("Gender")== "F" %>'/>
</EditItemTemplate>
</telerik:GridTemplateColumn>
protected
void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
GridDataItem item = e.Item as GridDataItem;
if (item["Gender"].Text == "M")
(item.FindControl("RadioButtonMaleItem") as RadioButton).Checked = true;
else
if (item["Gender"].Text == "F")
(item.FindControl("RadioButtonFemaleItem") as RadioButton).Checked = true;
}
}
case solved (for me)
regards martin