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

RadGrid FormTemplate DropDownList with DataSource

1 Answer 215 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Stephen Giordano
Top achievements
Rank 1
Stephen Giordano asked on 25 Mar 2011, 10:27 PM
Hello,

I have seen this topic in many different forum posts and demos on the site and I have tried so many different things I am just totally confused about how to make this work.

I have a RadGrid in which I am doing Inserts and Updates through an ObjectDataSource.  In the FormTemplate among other controls I have two dropdownlists that are both populated by their own ObjectDataSources.  The SelectParameter of the second dropdownlist is the selectedvalue from the first dropdownlist.  Before I converted this page into a RadGrid I simply used a ControlParameter for the ObjectDataSource of the second dropdownlist referencing the first one.  Now it seems that I can't do that within the FormTemplate of the grid for multiple reasons.

So what I need to accomplish is the following:

1.  I want the dropdowns to show the correct item as selected when that item is "edited".
2.  I want the second dropdown to be repopulated with the correct items when the first dropdown is changed.
3.  I want the correct item saved to the database on insert and update.

These are pretty obvious but at this point I'm not sure what I need to do to make that happen.

Here is the important snippet of my aspx code:

<FormTemplate>
    <asp:Panel ID="pnlEditPanel" runat="server" DefaultButton="btnUpdate" CssClass="edit_panel" TabIndex="100">
        <table border="0" cellspacing="0" cellpadding="0">
        <tr>
            <td>Group:</td>
            <td>
                <asp:ObjectDataSource ID="odsGroupList" runat="server" SelectMethod="dsGroupList" TypeName="NUFOLibrary.CMembers">
                    <SelectParameters>
                        <asp:SessionParameter Name="sUserID" SessionField="UserID" Type="String" />
                        <asp:Parameter Name="sPublic" DefaultValue="0" Type="String" />
                    </SelectParameters>
                </asp:ObjectDataSource>        
                <asp:DropDownList ID="lstGroups" runat="server" DataSourceID="odsGroupList" DataTextField="Group" DataValueField="ID" AutoPostBack="True" AppendDataBoundItems="true" OnSelectedIndexChanged="lstGroups_SelectedIndexChanged" SelectedValue='<%# Bind("Group_ID") %>'>
                    <asp:ListItem Value="0" Text="(Select One)" />
                </asp:DropDownList>                                
            </td>
        </tr>  
        <tr>
            <td>Position:</td>
            <td>
                <asp:ObjectDataSource ID="odsPositionList" runat="server" SelectMethod="dsPositionList" TypeName="NUFOLibrary.CMembers">
                    <SelectParameters>
                        <asp:SessionParameter Name="sUserID" SessionField="UserID" Type="String" />
                        <asp:ControlParameter Name="sGroupID" ControlID="lstGroups" Type="String" />
                    </SelectParameters>
                </asp:ObjectDataSource>        
                <asp:DropDownList ID="lstPositions" runat="server" DataSourceID="odsPositionList" DataTextField="Position" DataValueField="ID" SelectedValue='<%# Bind("Position_ID") %>'>
                    <asp:ListItem Value="0" Text="(Select One)" />
                </asp:DropDownList>                                    
            </td>
        </tr>

So on an edit/update command this works.  It displays the correct item as selected in the second dropdownlist.  I am fine here because I actually set enable=false on an edit because they are not allowed to edit that field so I don't have to worry about changing the first dropdownlist and repopulating the second.

But the problem is on an insert.  When I try to add a new entry I immediately get an error that the second dropdownlist has a SelectedValue that is invalid because it doesn't exist in the list of items.

Maybe this is because I am setting the default empty value (or in my case 0)?

Here are the important parts of my codebehind:

Protected Sub gvMembers_ItemCommand(ByVal source As Object, ByVal e As GridCommandEventArgs) Handles gvMembers.ItemCommand
    If (e.CommandName = RadGrid.InitInsertCommandName) Then
        e.Canceled = True
        Dim newValues As System.Collections.Specialized.ListDictionary = New System.Collections.Specialized.ListDictionary()
        newValues("Group_ID") = "0"
        newValues("Facility_ID") = "0"
        newValues("Position_ID") = "0"
        e.Item.OwnerTableView.InsertItem(newValues)
    End If
End Sub
Private Sub gvMembers_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles gvMembers.ItemDataBound
    If e.Item.DataItem Is Nothing Then Exit Sub
    If "Membership".Equals(e.Item.OwnerTableView.Name) Then
        If (TypeOf e.Item Is GridEditFormItem AndAlso e.Item.IsInEditMode) Then
            Dim editFormItem As GridEditFormItem = CType(e.Item, GridEditFormItem)
            DirectCast(editFormItem.FindControl("lstGroups"), DropDownList).Enabled = False
            DirectCast(editFormItem.FindControl("lstFacilities"), DropDownList).Enabled = False
            DirectCast(editFormItem.FindControl("lstPositions"), DropDownList).Enabled = False
            DirectCast(editFormItem.FindControl("rdpStartDate"), RadDatePicker).Enabled = False
            DirectCast(editFormItem.FindControl("rdpEndDate"), RadDatePicker).Enabled = True
        End If
        If (TypeOf e.Item Is GridEditFormInsertItem) AndAlso (e.Item.OwnerTableView.IsItemInserted) Then
            Dim editFormItem As GridEditFormItem = CType(e.Item, GridEditFormItem)
            DirectCast(editFormItem.FindControl("lstGroups"), DropDownList).Enabled = True
            DirectCast(editFormItem.FindControl("lstFacilities"), DropDownList).Enabled = True
            DirectCast(editFormItem.FindControl("lstPositions"), DropDownList).Enabled = True
            DirectCast(editFormItem.FindControl("rdpStartDate"), RadDatePicker).Enabled = True
            DirectCast(editFormItem.FindControl("rdpEndDate"), RadDatePicker).Enabled = True
        End If
    End If
End Sub
Protected Sub lstGroups_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim lstGroups As DropDownList = DirectCast(sender, DropDownList)
    Dim editItem As GridEditFormItem = DirectCast(lstGroups.NamingContainer, GridEditFormItem)
    Dim lstFacilities As DropDownList = DirectCast(editItem.FindControl("lstFacilities"), DropDownList)
    Dim lstPositions As DropDownList = DirectCast(editItem.FindControl("lstPositions"), DropDownList)
    Dim rdpStartDate As RadDatePicker = DirectCast(editItem.FindControl("rdpStartDate"), RadDatePicker)
    Dim rdpEndDate As RadDatePicker = DirectCast(editItem.FindControl("rdpEndDate"), RadDatePicker)
    'Dim odsPositionList = New ObjectDataSource
    'odsPositionList.ID = "odsPositionList"
    'odsPositionList.SelectMethod = "dsPositionList"
    'odsPositionList.TypeName = "NUFOLibrary.CMembers"
    'odsPositionList.SelectParameters.Add(New Parameter("sUserID", TypeCode.String, HttpContext.Current.Session("UserID").ToString))
    'odsPositionList.SelectParameters.Add(New Parameter("sGroupID", TypeCode.String, lstGroups.SelectedValue))
    'lstPositions.DataSource = odsPositionList
    'lstPositions.DataBind()
    If lstGroups.SelectedValue = "3" Then
        lstFacilities.Enabled = False
    Else
        lstFacilities.Enabled = True
    End If
    If lstGroups.SelectedValue = "4" Then
        rdpStartDate.DbSelectedDate = ""
    Else
        rdpStartDate.DbSelectedDate = Now().ToString
    End If
    rdpEndDate.DbSelectedDate = ""
End Sub

I had tried moving the populating of the second dropdown to the SelectedIndexChanged of the first dropdownlist (as you see commented out) but that causes the problem that when it tries to save it doesn't save the item to the database because I had to remove the SelectedValue='<%# Bind("Position_ID") %>' for the aspx page because of another error.

Someone please help me to figure out the best way to accomplish this.  Do I need to do the entire thing in code including the insert/update tot he database instead of letting the grid do it?

thanks,

Steve

1 Answer, 1 is accepted

Sort by
0
Accepted
Mira
Telerik team
answered on 31 Mar 2011, 08:33 AM
Hello Stephen,

Please use the approach from the Using related RadComboBoxes in RadGrid code library in order to implement related RadComboBoxes for Insert and Update operations in RadGrid.

I hope this helps.

All the best,
Mira
the Telerik team
Tags
Grid
Asked by
Stephen Giordano
Top achievements
Rank 1
Answers by
Mira
Telerik team
Share this question
or