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

Access RadComboBox from GridTemplateColumn

7 Answers 314 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Najid Hanif
Top achievements
Rank 2
Najid Hanif asked on 29 Jul 2012, 04:29 PM
I can't seem to figure out how to get the selected value or text from a RadComboBox in a GridTemplateColumn. Actually seems like I can't find the control at all.  The edit Form is a popup. The grid is in a user control. 

Here is my column 
<telerik:GridTemplateColumn UniqueName="ProdWorkstation" HeaderText="ProdWorkstation">
    <ItemTemplate>
    <%#DataBinder.Eval(Container.DataItem, "ProdWorkstation")%>
    </ItemTemplate>
    <EditItemTemplate>
        <telerik:RadComboBox runat="server" ID="RadComboBox_SelectWorkStation" Height="300px" Width="750px" DataSourceID="LCM_UserWorkstationList" EnableLoadOnDemand="true"
        ItemsPerRequest="20" EnableAutomaticLoadOnDemand="False" DataTextField="Workstation" DataValueField="Workstation"
        style="margin-bottom: 0px" AutoPostBack="True" HighlightTemplatedItems="true" CausesValidation="False" Skin="Office2007"
         OnItemsRequested="RadComboBox1_ItemsRequested">
 
            <HeaderTemplate>
                <ul>
                    <li class="col75">user_id</li>
                    <li class="col200">Workstation</li>
                    <li class="col200">Name</li>
                    <li class="col200">Profile</li>
                </ul>
            </HeaderTemplate>
            <ItemTemplate>
                <ul>
                    <li class="col75"><%# DataBinder.Eval(Container.DataItem, "user_id")%></li>
                    <li class="col200"><%# DataBinder.Eval(Container.DataItem, "Workstation")%></li>
                    <li class="col200"><%# DataBinder.Eval(Container.DataItem, "Name") %></li>
                    <li class="col200"><%# DataBinder.Eval(Container.DataItem, "Profile")%></li>
                </ul>
            </ItemTemplate>
        </telerik:RadComboBox>
    </EditItemTemplate>
</telerik:GridTemplateColumn>

Here is the codebehind where I try and get the text:
if (e.CommandName == "PerformInsert")
            {
                GridEditFormInsertItem EditForm = (GridEditFormInsertItem)e.Item;
                RadComboBox combo = EditForm["ProdWorkstation"].Controls[0] as RadComboBox;
                string ProdWorkstation = combo.Text; //NULL value

How can I do this?

Thanks!

7 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 30 Jul 2012, 05:00 AM
Hello,

Try accessing the combobox as shown below.
C#:
protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
 if (e.CommandName == "PerformInsert")
 {
   GridEditFormInsertItem item = (GridEditFormInsertItem)e.Item;
   RadComboBox combo = (RadComboBox)item.FindControl("ProdWorkstation");
 }
}

Thanks,
Shinu.
0
Najid Hanif
Top achievements
Rank 2
answered on 30 Jul 2012, 12:28 PM
Thanks but unfortunately I still get the same error: Object reference not set to an instance of an object.

Any other ideas?

Thanks

 
0
Jayesh Goyani
Top achievements
Rank 2
answered on 30 Jul 2012, 01:04 PM
Hello,

protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == RadGrid.PerformInsertCommandName)
        {
            GridEditFormInsertItem item = (GridEditFormInsertItem)e.Item;
            // If you get item is null then use GridEditableItem inplace of GridEditFormInsertItem
            RadComboBox combo = (RadComboBox)item.FindControl("RadComboBox_SelectWorkStation");
            //OR
            RadComboBox combo = (RadComboBox)item["ProdWorkstation"].FindControl("RadComboBox_SelectWorkStation");
        }
    }


Thanks,
Jayesh Goyani
0
Najid Hanif
Top achievements
Rank 2
answered on 30 Jul 2012, 01:36 PM
Jayesh, Getting closer! Big thanks for getting me this far. I really appreciate it. Both of those work and the control is found but the .SelectedValue and .Text are NULL

RadComboBox combo = (RadComboBox)EditForm["ProdWorkstation"].FindControl("RadComboBox_SelectWorkStation");
 string ProdWorkstation = combo.Text; // return value is NULL 

Thanks
0
Andrey
Telerik team
answered on 01 Aug 2012, 12:19 PM
Hi,

You get the Null value because when you are using ItemTemplate there is not value property specified for the items in the combobox. The Text property is inherited from the WebControl class and it is not overridden and that is why it returns Null.

In order to achieve your goal you could use the following approach:

protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == RadGrid.PerformInsertCommandName)
        {
            GridEditFormInsertItem EditForm = (GridEditFormInsertItem)e.Item;              
            RadComboBox combo = (RadComboBox)EditForm["ProdWorkstation"].FindControl("RadComboBox_SelectWorkStation");
            string ProdWorkstation = combo.SelectedItem.Text;
        }
    }

Give this approach a try and let me know if you need further assistance.

Regards,
Andrey
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Najid Hanif
Top achievements
Rank 2
answered on 01 Aug 2012, 01:58 PM
Hi Andrey, I get a NullReferenceException  on  string ProdWorkstation = combo.SelectedItem.Text;

Any other ideas?

Thanks
0
Accepted
Jayesh Goyani
Top achievements
Rank 2
answered on 02 Aug 2012, 09:26 AM
Hello,

Please check below demo its working correctly on my side.

<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnItemDataBound="RadGrid1_ItemDataBound"
           OnNeedDataSource="RadGrid1_NeedDataSource"
           oninsertcommand="RadGrid1_InsertCommand">
           <MasterTableView DataKeyNames="ID,Name" ClientDataKeyNames="ID,Name" CommandItemDisplay="Top"  EditMode="EditForms">
               <Columns>
                   <telerik:GridTemplateColumn UniqueName="ProdWorkstation" HeaderText="ProdWorkstation">
                       <ItemTemplate>
                           <%#DataBinder.Eval(Container.DataItem, "Name")%>
                       </ItemTemplate>
                       <EditItemTemplate>
                           <telerik:RadComboBox runat="server" ID="RadComboBox_SelectWorkStation" Height="300px"
                               Width="750px" DataSourceID="SqlDataSource1" EnableLoadOnDemand="true" ItemsPerRequest="20"
                               EnableAutomaticLoadOnDemand="False" DataTextField="Name" DataValueField="ID"
                               AutoPostBack="True" HighlightTemplatedItems="true" CausesValidation="False" Skin="Office2007"
                               OnItemsRequested="RadComboBox1_ItemsRequested">
                               <HeaderTemplate>
                                   Name
                               </HeaderTemplate>
                               <ItemTemplate>
                                   <%# DataBinder.Eval(Container.DataItem, "Name") %></li>
                               </ItemTemplate>
                           </telerik:RadComboBox>
                       </EditItemTemplate>
                   </telerik:GridTemplateColumn>
                   <telerik:GridEditCommandColumn>
                   </telerik:GridEditCommandColumn>
               </Columns>
           </MasterTableView>
       </telerik:RadGrid>
       <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TelerikTestDataBaseConnectionString %>"
           SelectCommand="SELECT [ID], [Name] FROM [TelerikTable1]"></asp:SqlDataSource>
protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
        {
            GridEditFormInsertItem item = (GridEditFormInsertItem)e.Item;
            // If you get item is null then use GridEditableItem inplace of GridEditFormInsertItem
            RadComboBox combo = (RadComboBox)item.FindControl("RadComboBox_SelectWorkStation");
 
            string strSelectedText;
 
            if (combo.SelectedItem != null)
            {
                strSelectedText = combo.SelectedItem.Text;
            }
            else
            {
                strSelectedText = combo.Text;
            }
        }
 
 protected void RadComboBox1_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
        {
            
        }
 
 
  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;
        }


Thanks,
Jayesh Goyani
Tags
Grid
Asked by
Najid Hanif
Top achievements
Rank 2
Answers by
Shinu
Top achievements
Rank 2
Najid Hanif
Top achievements
Rank 2
Jayesh Goyani
Top achievements
Rank 2
Andrey
Telerik team
Share this question
or