The code below is supposed for adding a RadioButtonList to an existing form which uses telerik controls. An appropriate radio button is displayed as selected when the page renders the first time. After that the user can select a different radio button, and that will trigger OnSelectedIndexChanged to save the new value to the database. Somehow the condition:if (e.Item is GridEditableItem && e.Item.IsInEditMode)
is never met. I see samples on the internet but cannot make this works. I'd appreciate it if someone can correct the codes that I have below.
I want the page to display 2 radio buttons without making the user click on any link (such as Edit) to see the 2 radio buttons.
<
telerik:RadGrid
ID
=
"RadGrid1"
runat
=
"server"
AutoGenerateColumns
=
"False"
ShowStatusBar
=
"True"
GridLines
=
"Both"
OnNeedDataSource
=
"RadGrid1_NeedDataSource"
OnItemDataBound
=
"RadGrid1_ItemDataBound"
CellSpacing
=
"2"
OnSelectedIndexChanged
=
"RadGrid1_OnSelectedIndexChanged"
>
<
MasterTableView
NoMasterRecordsText
=
"data not found"
Width
=
"100%"
GridLines
=
"Both"
TableLayout
=
"Fixed"
>
<
Columns
>
<
telerik:GridBoundColumn
Visible
=
"false"
DataField
=
"id"
SortExpression
=
"id"
UniqueName
=
"id"
/>
<
telerik:GridTemplateColumn
UniqueName
=
"TemplateColumn"
HeaderText
=
"Country"
>
<
ItemTemplate
>
<
asp:Label
ID
=
"Label1"
runat
=
"server"
>test list</
asp:Label
>
</
ItemTemplate
>
<
EditItemTemplate
>
<
asp:RadioButtonList
CellPadding
=
"0"
DataValueField
=
"rbtestList"
SelectedValue='<%#Eval("rbtestList")%>'
CellSpacing="0" ID="rbtestList" runat="server" RepeatColumns="4"
RepeatDirection="Vertical" TabIndex="7" >
<
asp:ListItem
Text
=
"0"
Value
=
"0"
/>
<
asp:ListItem
Text
=
"1"
Value
=
"1"
/>
</
asp:RadioButtonList
>
</
EditItemTemplate
>
</
telerik:GridTemplateColumn
>
</
Columns
>
</
MasterTableView
>
</
telerik:RadGrid
>
protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
{
RadGrid1.DataSource = this.dbData;
}
protected override void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (this.dbData.Tables[0].Rows.Count <
1
)
{
RadGrid1.MasterTableView.IsItemInserted
=
true
;
}
else
{
RadGrid1.MasterTableView.IsItemInserted
=
false
;
}
}
}
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
totalRows
=
dbData
.Tables[0].Rows.Count;
if (e.Item is GridEditableItem && e.Item.IsInEditMode)
{
GridEditableItem
item
= e.Item as GridEditableItem;
if (totalRows > 0)
{
RadioButtonList rbltestList = (RadioButtonList)item.FindControl("rbtestList");
rbltestList.SelectedValue = dbData.Tables[0].Rows[0]["testList"].ToString().Trim();
}
}
}
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GridBoundColumn testListColumn =
(GridBoundColumn)RadGrid1.MasterTableView.GetColumn("rbtestList");
}
}