Hello all.
I'm using nested grids to add, edit and delete data. It was working fine, and I've AJAX'd it up. Then the requirements changed and a RadioButtonList (StudyPeriodID) was required in the FormTemplate for the top level of data. This has thrown up a problem for adding a new record, as it errors out with:
Code below:
I have removed the sub-level grid, and a bunch of irrelevant columns and suchlike for brevity.
The problem is with the StudyPeriodID RadioButtonList. How do I get it to ignore SelectedValue when creating a new record, but use it when editing an existing record? I have tried:
1. Using ItemDataBound to catch the event and avoid the binding. Unfortunately I couldn't see how to detect the Add New event. All the documentation I found was for editing. Hence this code:
doesn't work because e.Item is not a GridEditFormItem, and IsInEditMode is false.
2. I tried using a custom function on the binding. So in the ASPX file you see
where GetRadioValue is
but this isn't working either.
Am I going about this the wrong way?
Thanks in advance
Kit
I'm using nested grids to add, edit and delete data. It was working fine, and I've AJAX'd it up. Then the requirements changed and a RadioButtonList (StudyPeriodID) was required in the FormTemplate for the top level of data. This has thrown up a problem for adding a new record, as it errors out with:
'StudyPeriodID' has a SelectedValue which is invalid because it does not exist in the list of items.
As the edit and add new use the same FormTemplate, the code to display an existing record (with a StudyPeriodID value) doesn't work for showing a blank form to add a new record.Code below:
<
telerik:RadGrid
ID
=
"grdProfileIntakes"
AutoGenerateColumns
=
"False"
ItemStyle-VerticalAlign
=
"Top"
OnNeedDataSource
=
"grdProfileIntakes_NeedDataSource"
ShowStatusBar
=
"true"
GridLines
=
"None"
OnItemDataBound
=
"grdProfileIntakes_ItemDataBound"
OnDetailTableDataBind
=
"grdProfileIntakes_DetailDataBind"
runat
=
"server"
>
<
MasterTableView
DataKeyNames
=
"IntakeId"
CommandItemDisplay
=
"TopAndBottom"
NoMasterRecordsText
=
"No Intakes to display."
Height
=
"100%"
Width
=
"100%"
ItemStyle-VerticalAlign
=
"Top"
>
<
RowIndicatorColumn
Visible
=
"True"
/>
<
CommandItemSettings
AddNewRecordText
=
"Add new intake"
/>
<
ExpandCollapseColumn
Visible
=
"True"
/>
<
Columns
>
<
telerik:GridEditCommandColumn
ButtonType
=
"ImageButton"
UniqueName
=
"btnEditIntake"
/>
<
telerik:GridBoundColumn
DataField
=
"IntakeTitle"
HeaderText
=
"Title"
SortExpression
=
"IntakeTitle"
UniqueName
=
"IntakeTitle"
/>
<
telerik:GridBoundColumn
DataField
=
"StudyPeriodLabel"
HeaderText
=
"Study Period"
SortExpression
=
"StudyPeriodLabel"
UniqueName
=
"StudyPeriodLabel"
/>
</
Columns
>
<
EditFormSettings
EditFormType
=
"Template"
>
<
EditColumn
UniqueName
=
"IntakeEditColumn"
></
EditColumn
>
<
FormTemplate
>
<
table
border
=
"0"
cellpadding
=
"2"
width
=
"500px"
>
<
tr
>
<
td
width
=
"50px"
><
asp:Label
ID
=
"Label5"
AssociatedControlID
=
"IntakeTitle"
runat
=
"server"
>Title:</
asp:Label
></
td
>
<
td
width
=
"200px"
>
<
asp:TextBox
ID
=
"IntakeTitle"
Width
=
"150px"
Text='<%# Bind("IntakeTitle") %>' TextMode="SingleLine" runat="server"
MaxLength="20" />
</
td
>
</
tr
>
<
tr
>
<
td
width
=
"50"
><
asp:Label
ID
=
"Label1"
AssociatedControlID
=
"StudyPeriodID"
runat
=
"server"
>Study Period:</
asp:Label
></
td
>
<
td
width
=
"200px"
>
<
asp:RadioButtonList
ID
=
"StudyPeriodID"
DataSourceID
=
"odsStudyPeriods"
SelectedValue='<%# GetRadioValue(Eval("StudyPeriodId")) %>' DataTextField="StudyPeriodLabel" DataValueField="StudyPeriodID" AppendDataBoundItems="true" runat="server" />
</
td
>
</
tr
>
</
table
>
<
asp:Button
ID
=
"btnIntakeUpdate"
CommandName
=
"Update"
CommandArgument
=
"Intake"
Text
=
"Save"
runat
=
"server"
CausesValidation
=
"true"
/>
<
asp:Button
ID
=
"btnIntakeCancel"
CommandName
=
"Cancel"
Text
=
"Cancel"
runat
=
"server"
CausesValidation
=
"false"
/>
<
asp:Button
ID
=
"btnIntakeDelete"
CommandName
=
"Delete"
CommandArgument
=
"Intake"
OnClientClick
=
"if(!confirm('Delete this Intake from the profile?')) return false;"
Text
=
"Delete"
runat
=
"server"
CausesValidation
=
"false"
/>
</
FormTemplate
>
</
EditFormSettings
>
</
MasterTableView
>
</
telerik:RadGrid
>
I have removed the sub-level grid, and a bunch of irrelevant columns and suchlike for brevity.
The problem is with the StudyPeriodID RadioButtonList. How do I get it to ignore SelectedValue when creating a new record, but use it when editing an existing record? I have tried:
1. Using ItemDataBound to catch the event and avoid the binding. Unfortunately I couldn't see how to detect the Add New event. All the documentation I found was for editing. Hence this code:
protected
void
grdProfileIntakes_ItemDataBound(
object
sender, GridItemEventArgs e)
{
if
((e.Item
is
GridEditFormItem) && (!e.Item.IsInEditMode))
{
GridEditFormItem editFormItem = (GridEditFormItem)e.Item;
RadioButtonList studyPeriod = (RadioButtonList)editFormItem.FindControl(
"StudyPeriodID"
);
if
(studyPeriod !=
null
)
{
studyPeriod.ClearSelection();
}
}
}
2. I tried using a custom function on the binding. So in the ASPX file you see
<
asp:RadioButtonList
ID
=
"StudyPeriodID"
DataSourceID
=
"odsStudyPeriods"
SelectedValue='<%# GetRadioValue(Eval("StudyPeriodId")) %>' DataTextField="StudyPeriodLabel" DataValueField="StudyPeriodID" AppendDataBoundItems="true" runat="server" />
protected
int
GetRadioValue(
object
val)
{
if
(val.ToString() ==
string
.Empty)
{
return
-1;
}
else
{
return
CommonFunctions.IntOr0(val.ToString());
}
}
Am I going about this the wrong way?
Thanks in advance
Kit