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

Populate Radcombo in EditForm

3 Answers 171 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Neil
Top achievements
Rank 1
Neil asked on 19 Jul 2010, 12:46 PM
Hi all,

I have two tables in an SQL database. One contains Customer details and the other contains Contact names. There is a one to many relationship between Customers and Contacts. I have a Radgrid for the Customers table and I'm using a pop-up EditForm for insert/update. On the EditForm there is a RadCombo to select a contact. I only want to populate this combo with the Contact Names for the selected Customer.

Below is my code. Currently it' not populating the Combo Box at all. What am I doing wrong?

Many thanks in advance,

Neil

<%@ Page Title="" Language="VB" MasterPageFile="~/Pages/SiteMaster.master" AutoEventWireup="false" CodeFile="TestCustomers.aspx.vb" Inherits="Pages_Bookings_Customers" %>
  
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
  
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
    
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="phTopContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="phMainContent" Runat="Server">
  
      
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" 
        DefaultLoadingPanelID="RadAjaxLoadingPanel1">
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="RadGrid1">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="RadGrid1" LoadingPanelID="RadAjaxLoadingPanel1" />
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManager>
    <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" 
        MinDisplayTime="100" Skin="Sunset"
    </telerik:RadAjaxLoadingPanel>    
      
    <div style="float: none; clear: both">
  
        <telerik:RadGrid ID="RadGrid1"
            runat="server"
            DataSourceID="dsCustomers"
            AllowAutomaticInserts="True" 
            AllowAutomaticUpdates="True" 
            AllowFilteringByColumn="True" 
            AllowPaging="True" 
            AllowSorting="True" 
            GridLines="None"
            Width="500px"
            Height="700px" 
            PageSize="20" 
            ShowStatusBar="True" 
            Skin="Sunset"
            EnableAJAXLoadingTemplate="True"
            EnableAJAX="True">
  
            <MasterTableView 
                DataKeyNames="CustomerID" 
                DataSourceID="dsCustomers"
                AutoGenerateColumns="False" 
                CommandItemDisplay="Top" 
                EditMode="PopUp">
                      
            <RowIndicatorColumn>
            <HeaderStyle Width="20px"></HeaderStyle>
            </RowIndicatorColumn>
  
            <ExpandCollapseColumn>
            <HeaderStyle Width="20px"></HeaderStyle>
            </ExpandCollapseColumn>
              
                <Columns>
                    <telerik:GridEditCommandColumn ButtonType="ImageButton" 
                        UniqueName="EditCommandColumn" 
                        HeaderStyle-Width="40px">
                    </telerik:GridEditCommandColumn>
                      
                    <telerik:GridBoundColumn DataField="CustomerID" 
                        DataType="System.Int32" 
                        HeaderText="CustomerID" 
                        ReadOnly="True" 
                        SortExpression="CustomerID" 
                        UniqueName="CustomerID" 
                        Visible="true">
                    </telerik:GridBoundColumn>
                      
                    <telerik:GridBoundColumn DataField="CustName" 
                        HeaderText="Customer Name" 
                        SortExpression="CustName" 
                        UniqueName="CustName" 
                        Visible="True">
                    </telerik:GridBoundColumn>
                     
                </Columns>
  
            <EditFormSettings 
                EditFormType="Template"              
                CaptionFormatString='Edit Customer: {0}' 
                CaptionDataField="CustName" 
                InsertCaption="Add New Customer" 
                PopUpSettings-Width="400px">
                <EditColumn UniqueName="EditCommandColumn1"></EditColumn>
                <FormTemplate>
                    <table id="outer_table">
                        <tr>
                              
                            <td  NOWRAP>
                                <asp:Label ID="Label1" runat="server" Text="Customer Name:" AssociatedControlID="tbCustName" />
                            </td>
                            <td>    
                                <telerik:RadTextBox ID="tbCustName" 
                                    runat="server" 
                                    width="250px"
                                    Text='<%# Bind( "CustName" ) %>'>
                                </telerik:RadTextBox>
                            </td>    
                       </tr>
   
                               <tr>
                                    <td nowrap style="vertical-align: top">    
                                        <asp:Label ID="Label17" runat="server" Text="Default Booking Contact:" AssociatedControlID="cbBookingContact" />
                                    </td>
                                    <td
                                        <telerik:RadComboBox ID="cbBookingContact"                                             
                                            Runat="server" 
                                            Skin="Sunset"                                            
                                            DataSourceID="dsBookingContact" 
                                            DataTextField="ContactName" 
                                            DataValueField="ContactID"
                                            SelectedValue='<%# Bind("DefaultBookingContactID") %>' 
                                            Width="255" 
                                            MarkFirstMatch="True" 
                                            Filter="Contains" 
                                            AppendDataBoundItems="True" 
                                            Text="Select a contact">
                                            <Items>
                                                <telerik:RadComboBoxItem  Value="" Text=""/>
                                            </Items>
                                              
                                        </telerik:RadComboBox>
                                    </td>
                               </tr>
                               <tr>
                                    <td align="left" colspan="2">
                                        <asp:Button ID="btnUpdate" 
                                            Text='<%# IIf((TypeOf(Container) is GridEditFormInsertItem), "Insert", "Update") %>'
                                            runat="server" 
                                            CommandName='<%# IIf((TypeOf(Container) is GridEditFormInsertItem), "PerformInsert", "Update")%>' />
                                           
                                        <asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False"
                                                CommandName="Cancel"></asp:Button></td>
                               </tr>
                            </table
  
   
          
                     
                </FormTemplate>
  
            </EditFormSettings>
            </MasterTableView>
              
            <GroupingSettings CaseSensitive="False" />
              
            <ClientSettings>
                <Scrolling AllowScroll="True" UseStaticHeaders="True" />
            </ClientSettings>
        </telerik:RadGrid>
          
        <asp:SqlDataSource ID="dsCustomers" runat="server" 
            ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
            SelectCommand="SELECT Customers.CustomerID, Customers.CustName, Customers.DefaultBookingContactID FROM Customers LEFT OUTER JOIN AccountStatus ON Customers.AccountStatusID = AccountStatus.AccountStatusID ORDER BY Customers.CustCode" 
            InsertCommand="INSERT INTO [Customers] ([CustName], [DefaultBookingContactID]) VALUES (@CustName, @CustCode,  @DefaultBookingContactID)" 
            UpdateCommand="UPDATE [Customers] SET [CustName] = @CustName, [DefaultBookingContactID] = @DefaultBookingContactID WHERE [CustomerID] = @CustomerID">
  
            <UpdateParameters>
                <asp:Parameter Name="CustName" Type="String" />
                <asp:Parameter Name="DefaultBookingContactID" Type="Int32" />
                <asp:Parameter Name="CustomerID" Type="Int32" />
            </UpdateParameters>
            <InsertParameters>
                <asp:Parameter Name="CustName" Type="String" />
                <asp:Parameter Name="DefaultBookingContactID" Type="Int32" />
            </InsertParameters>
        </asp:SqlDataSource>
          
  
         <asp:SqlDataSource ID="dsBookingContact" runat="server" 
            ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
            SelectCommand="SELECT ContactID, ContactName, CustomerID FROM Contacts WHERE (CustomerID = @CustomerID) ORDER BY ContactName">
             <SelectParameters>
                 <asp:ControlParameter ControlID="RadGrid1" Name="CustomerID" 
                     PropertyName="SelectedValue" />
             </SelectParameters>
        </asp:SqlDataSource>    
  
    </div>
</asp:Content>

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 20 Jul 2010, 08:43 AM
Hello Neil,

You can try the following method to achieve this. Store the DataKeyValue in a session variable and in SqlDataSource, populate the RadComboBox  by using SessionParameter which identifies the name stored in the current Session object.

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
   {
       if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
       {
           GridEditFormItem edititem = (GridEditFormItem)e.Item;
           Session["CustomerID"] = edititem.GetDataKeyValue("CustomerID").ToString(); // store the DataKeyValue in a Session variable
       }
   }

ASPX:
<asp:SqlDataSource ID="dsBookingContact" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
        SelectCommand="SELECT ContactID, ContactName, CustomerID FROM Contacts WHERE (CustomerID = @CustomerID) ORDER BY ContactName">
        <SelectParameters>
            <asp:SessionParameter SessionField="CustomerID" Name="CustomerID" />
        </SelectParameters>
</asp:SqlDataSource>

Hope this helps,
Princy.
0
Neil
Top achievements
Rank 1
answered on 22 Jul 2010, 11:42 AM
Hi Princy

Thanks for the reply. Your suggestion worked and filled the combo box but I have now hit a binding problem. More of that in a moment.
Inspired by the first part of your code behind, I then tried to do the whole thing that way rather than using session parameters.

Heres' my revised code.

ASPX
<asp:SqlDataSource ID="dsBookingContact" runat="server" 
     ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
     SelectCommand="SELECT ContactID, ContactName FROM Contacts WHERE (CustomerID = @CustomerID) ORDER BY ContactName">
     <SelectParameters>
         <asp:Parameter Name="CustomerID" />
     </SelectParameters>
</asp:SqlDataSource>

vb.net
Imports Telerik.Web.UI
Partial Class Pages_Bookings_Customers
    Inherits System.Web.UI.Page
  
    Protected Sub RadGrid1_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemDataBound
  
        If TypeOf e.Item Is GridEditFormItem And e.Item.IsInEditMode Then
            Dim edititem As GridEditFormItem = e.Item
            Dim myCustomerID As Integer = edititem.GetDataKeyValue("CustomerID").ToString()
  
            Dim myCBox As RadComboBox = Utilities.FindControlRecursive(RadGrid1, "cbBookingContact")
            myCBox.Items.Clear() ' to avoid appending
            myCBox.Items.Add(New RadComboBoxItem("", ""))  'add back null item
  
            dsBookingContact.SelectParameters("CustomerID").DefaultValue = myCustomerID
  
            dsBookingContact.DataBind()
        End If
  
    End Sub
  
End Class

This fills the combobox  in the way I want. Note I had to clear the items first as they new items were appending to the previous selection. Dont know why that was happening but perhaps you might know? Anyhow it works now.

Now back to the binding problem. I had to comment out the SelectedValue line to get this far as i got this error.
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

How do I get round this?
Thanks in advance.

Neil

<telerik:RadComboBox
      ID="cbBookingContact"                                        
      Runat="server"                                            
      DataSourceID="dsBookingContact" 
      DataTextField="ContactName" 
      DataValueField="ContactID"
      SelectedValue='<%# Bind("DefaultBookingContactID") %>' 
      Width="255" 
      MarkFirstMatch="True" 
      Filter="Contains" 
      AppendDataBoundItems="True">
      <Items>
          <telerik:RadComboBoxItem  Value="" Text="" />
      </Items>                                         
</telerik:RadComboBox>
0
Pavlina
Telerik team
answered on 22 Jul 2010, 12:18 PM
Hi Neil,

I suggest that you go through the following link, which elaborates on this matter and see if it helps:
http://www.telerik.com/community/forums/aspnet-ajax/grid/having-problem-in-databinding-to-radcombo-in-grideditableitem.aspx

Kind regards,
Pavlina
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
Tags
Grid
Asked by
Neil
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Neil
Top achievements
Rank 1
Pavlina
Telerik team
Share this question
or