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

RadioButtonList within FormTemplate

4 Answers 162 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kit
Top achievements
Rank 1
Kit asked on 08 Nov 2010, 11:06 AM
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:

'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();
                }
            }
        }
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
<asp:RadioButtonList ID="StudyPeriodID" DataSourceID="odsStudyPeriods" SelectedValue='<%# GetRadioValue(Eval("StudyPeriodId")) %>' DataTextField="StudyPeriodLabel" DataValueField="StudyPeriodID" AppendDataBoundItems="true" runat="server" />
where GetRadioValue is
protected int GetRadioValue(object val)
        {
            if (val.ToString() == string.Empty)
            {
                return -1;
            }
            else
            {
                return CommonFunctions.IntOr0(val.ToString());
            }
        }
but this isn't working either.

Am I going about this the wrong way?

Thanks in advance

Kit

4 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 08 Nov 2010, 12:12 PM
Hello Kit,

One suggestion is setting the SelectedValue property of RadioButtonList from code behind when the grid is in edit mode. Sample code is given below.

ASPX:
<asp:RadioButtonList ID="StudyPeriodID" DataSourceID="odsStudyPeriods"
DataTextField="StudyPeriodLabel" DataValueField="StudyPeriodID" AppendDataBoundItems="true" runat="server" />

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
     {
         if (e.Item is GridEditFormItem && e.Item.IsInEditMode && !e.Item.OwnerTableView.IsItemInserted)
         {
             GridEditFormItem editFormItem = (GridEditFormItem)e.Item;
             GridDataItem item = (GridDataItem)editFormItem.ParentItem;
             DataRowView rowview = (DataRowView)item.DataItem;
             RadioButtonList studyPeriod = (RadioButtonList)editFormItem.FindControl("StudyPeriodID");
             studyPeriod.SelectedValue = rowview["StudyPeriodId"].ToString();
         }
    }

Thanks,
Princy.
0
Kit
Top achievements
Rank 1
answered on 08 Nov 2010, 12:25 PM
Yep, that's it, sorted.  Minor change as my DataItem was a custom class, not your standard DataRowView, but other than that its all good.

Just out of interest, as I'm calling ItemDataBound for this, is there a performance or consistency benefit to setting all the item's values using this, or is there no harm in having a mix of Bind's in the ASPX and this function in code behind?

Thanks Princy, that only took up the better part of 8 hours!

Kit
0
Tsvetina
Telerik team
answered on 10 Nov 2010, 04:17 PM
Hi Kit,

There would be no sensible difference in performance between the two ways of populating the controls in your edit form. The choice should mostly depend on the way it is easier for you to maintain your page's functionalities. Here is what MSDN states to be the two main disadvantages of embedded code blocks:
  • when the code is mixed on the page with markup, it can be difficult to debug and maintain. 
  • because the code is executed only during the page's render phase, you have substantially less flexibility than with code-behind or script-block code in scoping your code to the appropriate stage of page processing.

If these are not worries for you, you can stick with the code blocks.


Sincerely yours,

Tsvetina
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Kit
Top achievements
Rank 1
answered on 11 Nov 2010, 10:33 AM
Hi Tsevina, thanks for your reply.

I'm going to leave it as it is, and put a dirty great big comment in the markup to point people in the right direction.  I don't anticipate any further functionality requirements to complicate matters for the time being, so this is probably the best/easiest way to continue for now.

Thanks again.

Kit
Tags
Grid
Asked by
Kit
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Kit
Top achievements
Rank 1
Tsvetina
Telerik team
Share this question
or