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

Retrieving values from ComboBox in an FormTemplate

4 Answers 95 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Anders
Top achievements
Rank 1
Anders asked on 09 Jul 2013, 07:42 AM
I'm fairly new to the Telerik components (by the way a great set of components) and now I have a problem retrieving data in the insertCommand method.
I have RadGrid containing a FormTemplate for editing the data.

<EditFormSettings EditFormType="Template">
    <FormTemplate>
        <div id="editdetail">
            <h2>
                Truck information</h2>                 
            <table class="detailtable" cellspacing="2" cellpadding="1" width="100%" border="0"
                rules="none" style="border-collapse: collapse;">
                <tr>
                    <td class="detaillabel">
                        Brand:
                    </td>
                    <td>
                        <asp:PlaceHolder ID="phBrandComboBox" runat="server"></asp:PlaceHolder>
                    </td>
                </tr>
            </table>
        </div>
    </FormTemplate>
</EditFormSettings>

In ItemDatBound I inject a UserControle containing a Telerik ComboBox in the PlaceHolder.

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MyTruckItemComboBox.ascx.cs"
    Inherits="Truck_marketplace.MyPortfolio.User_controls.MyTruckItemComboBox" %>
 
    <head>
        <title>MyTruckItemComboBox</title>
    </head>
    <body>
        <telerik:RadComboBox ID="cbMyTruckItemComboBox"
            OnSelectedIndexChanged="cbMyTruckItemComboBox_SelectedIndexChanged"
            runat="server">
        </telerik:RadComboBox>
    </body>
</html>

protected void grMyTruck_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
    if (e.Item is Telerik.Web.UI.GridEditableItem && e.Item.IsInEditMode)
    {
        // Insert + Edit mode.
 
        Telerik.Web.UI.GridEditableItem EditItem = (Telerik.Web.UI.GridEditableItem)e.Item;
        //Brand
        MyPortfolio.User_controls.MyTruckItemComboBox ComboBoxBrand = (MyPortfolio.User_controls.MyTruckItemComboBox)LoadControl("MyTruckItemComboBox.ascx");
        ComboBoxBrand.ID = "cbBrand";
        ComboBoxBrand.GroupID = "BRD";
        ComboBoxBrand.Languagecode = "en-US";
    }
     
    if (e.Item is Telerik.Web.UI.GridEditFormInsertItem || e.Item is Telerik.Web.UI.GridDataInsertItem)
    {
        // Insert mode
        //Brand
        ComboBoxBrand.BindComboBox2();
        PlaceHolder phBrandComboBox = (PlaceHolder)e.Item.FindControl("phBrandComboBox");
        phBrandComboBox.Controls.Add(ComboBoxBrand);
    }
}

In the InsertCommand I the try to find the ComboBox and reading the SelectedValue from it. But I dont have any luck.
All of the code in the code here fails because the cb is null.

protected void grMyTruck_InsertCommand(object sender, GridCommandEventArgs e)
{
    NMHG.Data.Truck_marketplace.Advert.Advert NewAdvert = new NMHG.Data.Truck_marketplace.Advert.Advert();
 
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        GridEditableItem editableItem = e.Item as GridEditableItem;
 
        UserControl userControl = editableItem.FindControl(GridEditFormItem.EditFormUserControlID) as UserControl;
        Telerik.Web.UI.RadComboBox cb = userControl.FindControl("cbBrand") as Telerik.Web.UI.RadComboBox;
        var temp = cb.SelectedValue;
 
        PlaceHolder phBrandComboBox = (PlaceHolder)editableItem.FindControl("phBrandComboBox");
        Telerik.Web.UI.RadComboBox cb2 = phBrandComboBox.FindControl("cbBrand") as Telerik.Web.UI.RadComboBox;
        var temp2 = cb2.SelectedValue;
 
        Telerik.Web.UI.RadComboBox cb3 = editableItem.FindControl("cbBrand") as Telerik.Web.UI.RadComboBox;
        var temp3 = cb3.SelectedValue;
 
        Telerik.Web.UI.RadComboBox cb4 = editableItem.FindControl("cbMyTruckItemComboBox") as Telerik.Web.UI.RadComboBox;
        var temp4 = cb4.SelectedValue;
    }
}

Can some one please help - Thanks.

I properly do some thing stupid so please bare with me ;-)

Anders Pedersen

4 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 09 Jul 2013, 09:47 AM
Hello,

MyTruckItemComboBox.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="MyTruckItemComboBox.ascx.cs"
    Inherits="MyTruckItemComboBox" %>
<telerik:RadComboBox ID="cbMyTruckItemComboBox" OnSelectedIndexChanged="cbMyTruckItemComboBox_SelectedIndexChanged"
    runat="server">
</telerik:RadComboBox>

MyTruckItemComboBox.ascx.cs
public partial class MyTruckItemComboBox : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
         
        
 
    }
    protected void cbMyTruckItemComboBox_SelectedIndexChanged(object sender, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
    {
 
    }
}

Forum.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Forum.aspx.cs" Inherits="Forum" %>
 
<%@ Register Src="~/mytruckitemcombobox.ascx" TagPrefix="uc" TagName="MyTruckItemComboBox" %>
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnNeedDataSource="RadGrid1_NeedDataSource"
        OnItemCommand="RadGrid1_ItemCommand"
        OnInsertCommand="RadGrid1_InsertCommand"
        onitemdatabound="RadGrid1_ItemDataBound">
        <ExportSettings OpenInNewWindow="true">
        </ExportSettings>
        <MasterTableView DataKeyNames="ID" CommandItemDisplay="Top">
            <CommandItemSettings ShowExportToPdfButton="true" ShowExportToExcelButton="true"
                ShowExportToCsvButton="true" />
            <Columns>
                <telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID" Visible="false">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="Name" UniqueName="Name" HeaderText="Name">
                </telerik:GridBoundColumn>
                <telerik:GridEditCommandColumn>
                </telerik:GridEditCommandColumn>
            </Columns>
            <EditFormSettings EditFormType="Template">
                <FormTemplate>
                    <div>
                        Brand:
                        <uc:MyTruckItemComboBox runat="server" ID="ucMyTruckItemComboBox" />
                        <asp:Button ID="btnUpdate" Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>'
                                    runat="server" CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>'>
                                </asp:Button
                                <asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False"
                                    CommandName="Cancel"></asp:Button>
                    </div>
                </FormTemplate>
            </EditFormSettings>
        </MasterTableView>
    </telerik:RadGrid>

Forum.aspx.cs
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        dynamic data = new[] {
            new { ID = 1, Name ="Name1"},
            new { ID = 2, Name = "Name2"},
            new { ID = 3, Name = "Name3"},
             new { ID = 4, Name = "Name4"},
            new { ID = 5, Name = "Name5"}
        };
 
        RadGrid1.DataSource = data;
 
    }
 
 
    protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
    {
        if (e.CommandName == RadGrid.ExportToPdfCommandName ||
            e.CommandName == RadGrid.ExportToExcelCommandName ||
            e.CommandName == RadGrid.ExportToCsvCommandName)
        {
            if (RadGrid1.MasterTableView.Columns.FindByUniqueName("ID") != null)
            {
                RadGrid1.MasterTableView.Columns.FindByUniqueName("ID").Visible = true;
            }
            RadGrid1.Rebind();
        }
    }
 
    protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
    {
        GridEditableItem editableItem = e.Item as GridEditableItem;
        MyTruckItemComboBox myTruckItemComboBox = editableItem.FindControl("ucMyTruckItemComboBox") as MyTruckItemComboBox;
        RadComboBox cbMyTruckItemComboBox = myTruckItemComboBox.FindControl("cbMyTruckItemComboBox") as RadComboBox;
        string strValue = cbMyTruckItemComboBox.SelectedValue;
        string strText = cbMyTruckItemComboBox.Text;
    }
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridEditFormInsertItem && e.Item.IsInEditMode)
        {
            GridEditableItem editableItem = e.Item as GridEditableItem;
            MyTruckItemComboBox myTruckItemComboBox = editableItem.FindControl("ucMyTruckItemComboBox") as MyTruckItemComboBox;
            RadComboBox cbMyTruckItemComboBox = myTruckItemComboBox.FindControl("cbMyTruckItemComboBox") as RadComboBox;
 
            DataTable dt = new DataTable();
            dt.Columns.Add("Shipper", typeof(string));
            dt.Columns.Add("ShipperTemp", typeof(string));
            dt.Rows.Add("CShipper1", "1");
            dt.Rows.Add("BShipper2", "2");
            dt.Rows.Add("AShipper3", "3");
            dt.Rows.Add("EShipper4", "4");
            dt.Rows.Add("DShipper5", "5");
            cbMyTruckItemComboBox.DataSource = dt;
            cbMyTruckItemComboBox.DataTextField = "Shipper";
            cbMyTruckItemComboBox.DataValueField = "ShipperTemp";
            cbMyTruckItemComboBox.DataBind();
        }
    }




Thanks,
Jayesh Goyani
0
Anders
Top achievements
Rank 1
answered on 09 Jul 2013, 12:01 PM
Thanks Jayesh Goyani,

But it didn't solve my problem. 

I was not precis in my explanation and the code I posted was simplified. My problem is that not only have one ComboBox in this FormTemplate - but 16. I want to reuse the UserControl and therefor I set some properties and in the CodeBehind of the UserControl I dynamically load the ComboBox items depending on the properties.

That's was the reason for that I tried to set the ID of ComboBox.

MyTruckItemComboBox myTruckItemComboBox = EditItem.FindControl("MyTruckItemComboBoxBrand") as   MyTruckItemComboBox;
   myTruckItemComboBox.ID = "cbBrandxxx";
   myTruckItemComboBox.GroupID = "BRD";
   myTruckItemComboBox.Languagecode = "en-US";

I tried to do the same thing with your code example but when want to look up the ComboBox with the "FindControl" and the ID I have set, he returns a null.

How can I solve this problem best?

Anders Pedersen


0
Jayesh Goyani
Top achievements
Rank 2
answered on 09 Jul 2013, 12:38 PM
Hello,

Please try with the below cod snippet..

<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnNeedDataSource="RadGrid1_NeedDataSource"
       OnItemCommand="RadGrid1_ItemCommand" OnInsertCommand="RadGrid1_InsertCommand"
       OnItemDataBound="RadGrid1_ItemDataBound"
       onitemcreated="RadGrid1_ItemCreated">
       <ExportSettings OpenInNewWindow="true">
       </ExportSettings>
       <MasterTableView DataKeyNames="ID" CommandItemDisplay="Top">
           <CommandItemSettings ShowExportToPdfButton="true" ShowExportToExcelButton="true"
               ShowExportToCsvButton="true" />
           <Columns>
               <telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID" Visible="false">
               </telerik:GridBoundColumn>
               <telerik:GridBoundColumn DataField="Name" UniqueName="Name" HeaderText="Name">
               </telerik:GridBoundColumn>
               <telerik:GridEditCommandColumn>
               </telerik:GridEditCommandColumn>
           </Columns>
           <EditFormSettings EditFormType="Template">
               <FormTemplate>
                   <div>
                       <asp:PlaceHolder ID="ComboboxPlaceHolder" runat="server"></asp:PlaceHolder>
                        
                       <asp:Button ID="btnUpdate" Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>'
                           runat="server" CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>'>
                       </asp:Button
                       <asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False"
                           CommandName="Cancel"></asp:Button>
                   </div>
               </FormTemplate>
           </EditFormSettings>
       </MasterTableView>
   </telerik:RadGrid>

protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
   {
       dynamic data = new[] {
           new { ID = 1, Name ="Name1"},
           new { ID = 2, Name = "Name2"},
           new { ID = 3, Name = "Name3"},
            new { ID = 4, Name = "Name4"},
           new { ID = 5, Name = "Name5"}
       };
 
       RadGrid1.DataSource = data;
 
   }
 
 
   protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
   {
       if (e.CommandName == RadGrid.ExportToPdfCommandName ||
           e.CommandName == RadGrid.ExportToExcelCommandName ||
           e.CommandName == RadGrid.ExportToCsvCommandName)
       {
           if (RadGrid1.MasterTableView.Columns.FindByUniqueName("ID") != null)
           {
               RadGrid1.MasterTableView.Columns.FindByUniqueName("ID").Visible = true;
           }
           RadGrid1.Rebind();
       }
   }
 
   protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
   {
       GridEditableItem editableItem = e.Item as GridEditableItem;
 
       PlaceHolder ComboboxPlaceHolder = editableItem.FindControl("ComboboxPlaceHolder") as PlaceHolder;
 
       for (int i = 0; i < 2; i++)
       {
           MyTruckItemComboBox myTruckItemComboBox = ComboboxPlaceHolder.FindControl("myTruckItemComboBox" + i) as MyTruckItemComboBox;
 
           RadComboBox cbMyTruckItemComboBox = myTruckItemComboBox.FindControl("cbMyTruckItemComboBox") as RadComboBox;
           string strValue = cbMyTruckItemComboBox.SelectedValue;
           string strText = cbMyTruckItemComboBox.Text;
       }
   }
   protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
   {
       if (e.Item is GridEditFormInsertItem && e.Item.IsInEditMode)
       {
           GridEditableItem editableItem = e.Item as GridEditableItem;
 
           PlaceHolder ComboboxPlaceHolder = editableItem.FindControl("ComboboxPlaceHolder") as PlaceHolder;
 
           for (int i = 0; i < 2; i++)
           {
               MyTruckItemComboBox myTruckItemComboBox = ComboboxPlaceHolder.FindControl("myTruckItemComboBox" + i) as MyTruckItemComboBox;
 
               RadComboBox cbMyTruckItemComboBox = myTruckItemComboBox.FindControl("cbMyTruckItemComboBox") as RadComboBox;
 
               DataTable dt = new DataTable();
               dt.Columns.Add("Shipper", typeof(string));
               dt.Columns.Add("ShipperTemp", typeof(string));
               dt.Rows.Add("CShipper1_" + i, "1");
               dt.Rows.Add("BShipper2", "2");
               dt.Rows.Add("AShipper3", "3");
               dt.Rows.Add("EShipper4", "4");
               dt.Rows.Add("DShipper5", "5");
               cbMyTruckItemComboBox.DataSource = dt;
               cbMyTruckItemComboBox.DataTextField = "Shipper";
               cbMyTruckItemComboBox.DataValueField = "ShipperTemp";
               cbMyTruckItemComboBox.DataBind();
           }
       }
   }
   protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
   {
       if (e.Item is GridEditFormInsertItem && e.Item.IsInEditMode)
       {
           GridEditableItem editableItem = e.Item as GridEditableItem;
 
           PlaceHolder ComboboxPlaceHolder = editableItem.FindControl("ComboboxPlaceHolder") as PlaceHolder;
 
           for (int i = 0; i < 2; i++)
           {
 
               MyTruckItemComboBox myTruckItemComboBox = (MyTruckItemComboBox)LoadControl("MyTruckItemComboBox.ascx");
               myTruckItemComboBox.ID = "myTruckItemComboBox" + i;
               ComboboxPlaceHolder.Controls.Add(myTruckItemComboBox);
           }
       }
   }

Note : Please make sure all the usercontrol have different ID.

Thanks,
Jayesh Goyani
0
Anders
Top achievements
Rank 1
answered on 09 Jul 2013, 02:24 PM
Thanks Jayesh Goyani

I think I got it to work, placing the injection of the UserControl in ItemCreate and giving the UserControle a unique ID did the trick for me - I think. I have to test it further tomorrow - I have a meeting now :-(

Thanks again. But I don't promise that wont be back tomorrow ;-) 


Anders Pedersen   
Tags
Grid
Asked by
Anders
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Anders
Top achievements
Rank 1
Share this question
or