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

How to get Radgrid dropdown list template column selected value in InsertCommand?

10 Answers 927 Views
Grid
This is a migrated thread and some comments may be shown as answers.
JJ
Top achievements
Rank 1
JJ asked on 11 Feb 2011, 08:09 PM
 

How to get Radgrid dropdown list template column selected value  in InsertCommand?


I used below, doesn't work.  Also, how to make the text filter work for a dropdownlist column?

 

 

 

DropDownList dl;

 

 

dl = newItem[

 

"Language"].Controls[0] as DropDownList;

 

 

 

 

String SelectLanguageID = dl.SelectedValue;

 

 

 

----------------------------------------------------------------
<
telerik:GridTemplateColumn HeaderText="Language" UniqueName="Language" 

 

 

 

AutoPostBackOnFilter="true" CurrentFilterFunction="Contains"

 

 

 

ShowFilterIcon="false" FilterControlWidth="200px" >

 

 

 

 

 

 

 

 

<EditItemTemplate> 

 

 

 

<asp:DropDownList ID="ddlLanguage" runat="server"> 

 

 

 

</asp:DropDownList> 

 

 

 

</EditItemTemplate> 

 

 

 

<FooterTemplate> 

 

 

 

<asp:Label ID="lblLanguage" runat="server">

 

 

 

 

 

</asp:Label> 

 

 

 

</FooterTemplate> 

 

 

 

<ItemTemplate>

 

 

 

 

 

 

 

 

 

 

 

 

</ItemTemplate>

 

 

 

 

 

 

 

 

</telerik:GridTemplateColumn>


Thanks!

10 Answers, 1 is accepted

Sort by
0
Elliott
Top achievements
Rank 2
answered on 11 Feb 2011, 08:35 PM
DropDownList dl;
dl = newItem["Language"].Controls[0] as DropDownList;
String SelectLanguageID = dl.SelectedValue;
<telerik:GridTemplateColumn HeaderText="Language" UniqueName="Language"
AutoPostBackOnFilter="true" CurrentFilterFunction="Contains"
ShowFilterIcon="false" FilterControlWidth="200px" >
 
<EditItemTemplate>
<asp:DropDownList ID="ddlLanguage" runat="server">
</asp:DropDownList>
</EditItemTemplate>
 
<FooterTemplate>
    <asp:Label ID="lblLanguage" runat="server">
    </asp:Label>
</FooterTemplate>
 
<ItemTemplate>
</ItemTemplate>
 
</telerik:GridTemplateColumn>
better?

I know how to do it in code behind - I do all my IO in code - but that isn't what the poster is asking

anyway - to do it in code behind
on an ItemCommand event
if e.CommandName is "PerformInsert"
cast e.Item as GridDataItem
cast e.item("unique name").Controls(0) as RadComboBox (I used a Rad Combo Box, not a drop down list)
the rad combo box selected value I passed into the insert method

hope that helps
0
JJ
Top achievements
Rank 1
answered on 11 Feb 2011, 09:25 PM
Thanks.

It won't recognize the item in e.item["unique name"].Controls(0) as DropDownList

But I tried below, it works.

 

GridEditableItem editedItem = (GridEditableItem)e.Item;

 

 

 

DropDownList dl;

 

 

dl = (

DropDownList)editedItem.FindControl("ddlLanguage");


The filter for the dropdown column still doesn't work for me.

 

0
Iana Tsolova
Telerik team
answered on 17 Feb 2011, 05:41 AM
Hi JJ,

Try setting the DataField property of the column. You can refer to the below article for more information:
http://www.telerik.com/help/aspnet-ajax/grdimplementingfilteringfortemplatecolumns.html

Regards,
Iana
the Telerik team
0
Vidisha
Top achievements
Rank 1
answered on 08 Aug 2011, 05:28 PM
Y all solution link is not working?
0
Jayesh Goyani
Top achievements
Rank 2
answered on 08 Aug 2011, 08:13 PM
Hello JJ,

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DropDownDemo.aspx.cs" Inherits="TelerikTest.Web.DropDownDemo" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <telerik:RadScriptManager runat="server" ID="RadScriptManager1">
        </telerik:RadScriptManager>
        <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnNeedDataSource="RadGrid1_NeedDataSource"
            OnItemDataBound="RadGrid1_ItemDataBound" OnInsertCommand="RadGrid1_InsertCommand"
            OnUpdateCommand="RadGrid1_UpdateCommand" AllowFilteringByColumn="true">
            <MasterTableView CommandItemDisplay="Top">
                <Columns>
                    <telerik:GridEditCommandColumn UniqueName="gecc">
                    </telerik:GridEditCommandColumn>
                    <telerik:GridBoundColumn DataField="ID" HeaderText="ID" UniqueName="ID">
                    </telerik:GridBoundColumn>
                    <telerik:GridTemplateColumn DataField="Name" HeaderText="Name" UniqueName="Name">
                        <ItemTemplate>
                            <%# Eval("Name") %>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>' Visible="false"></asp:Label>
                            <asp:DropDownList ID="DropDownList1" runat="server">
                            </asp:DropDownList>
                        </EditItemTemplate>
                    </telerik:GridTemplateColumn>
                </Columns>
            </MasterTableView>
        </telerik:RadGrid>
    </div>
    </form>
</body>
</html>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
 
namespace TelerikTest.Web
{
    public partial class DropDownDemo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
 
        }
 
        protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.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"},
                new { ID = 6, Name ="Name6"},
                new { ID = 7, Name = "Name7"},
                new { ID = 8, Name = "Name8"},
                new { ID = 9, Name = "Name9"},
                new { ID = 10, Name = "Name10"},
                new { ID = 11, Name ="Name11"},
                new { ID = 12, Name = "Name12"},
                new { ID = 13, Name = "Name13"},
                new { ID = 14, Name = "Name14"},
                new { ID = 15, Name = "Name15"}
            };
 
            RadGrid1.DataSource = data;
        }
 
        protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            if (e.Item is GridEditableItem && e.Item.IsInEditMode)
            {
                GridEditableItem edititem = (GridEditableItem)e.Item;
                DropDownList DropDownList1 = (DropDownList)edititem.FindControl("DropDownList1");
                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"},
                new { ID = 6, Name ="Name6"},
                new { ID = 7, Name = "Name7"},
                new { ID = 8, Name = "Name8"},
                new { ID = 9, Name = "Name9"},
                new { ID = 10, Name = "Name10"},
                new { ID = 11, Name ="Name11"},
                new { ID = 12, Name = "Name12"},
                new { ID = 13, Name = "Name13"},
                new { ID = 14, Name = "Name14"},
                new { ID = 15, Name = "Name15"}
            };
                DropDownList1.DataSource = data;
                DropDownList1.DataTextField = "Name";
                DropDownList1.DataValueField = "ID";
                DropDownList1.DataBind();
 
                Label lblName = edititem.FindControl("lblName") as Label;
                if (!string.IsNullOrEmpty(lblName.Text))
                {
                    DropDownList1.SelectedValue = lblName.Text;
                }
            }
        }
 
        protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
        {
 
            GridEditableItem gridEditFormItem = (GridEditableItem)e.Item;
 
            DropDownList DropDownList1 = gridEditFormItem.FindControl("DropDownList1") as DropDownList;
            string strTemp = DropDownList1.SelectedItem.Text;
 
        }
 
        protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            GridEditableItem gridEditFormItem = (GridEditableItem)e.Item;
             
            DropDownList DropDownList1 = gridEditFormItem.FindControl("DropDownList1") as DropDownList;
            string strTemp = DropDownList1.SelectedItem.Text;
 
        }
    }
}

Let me know if any concern.

Thanks,
Jayesh Goyani
0
Sebastian
Telerik team
answered on 09 Aug 2011, 09:39 AM
Hi guys,

And here is the updated url to the help topic my colleague Iana pointed in her post:
http://www.telerik.com/help/aspnet-ajax/grid-implementing-filtering-for-gridtemplatecolumns.html
 
Greetings,
Sebastian
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

0
Saqib
Top achievements
Rank 1
answered on 24 Jul 2013, 10:39 AM
 @Jayesh Goyani
 i would like to say thanks alot to you,your example is exellent ,the difficult thing was to find the proper DropDownlist controls for insert and update,And your code is perfect,I have successfully completed my Insert and update operation by the help of your code.
Regards
Saqib
0
Saqib
Top achievements
Rank 1
answered on 24 Jul 2013, 10:40 AM

0
João
Top achievements
Rank 1
answered on 14 May 2014, 01:54 PM
Hi Jayesh Goyani,

I converted the code to VB but it doesn´t work, can you tell me why?
In New (), it tells me "Type Expected"!

Dim data As dynamic = New () {
                New With { Key .ID = 1, Key .Name = "Name1" },
                New With { Key .ID = 2, Key .Name = "Name2" },
                New With { Key .ID = 3, Key .Name = "Name3" },
                New With { Key .ID = 4, Key .Name = "Name4" },
                New With { Key .ID = 5, Key .Name = "Name5" }}
DropDownList1.DataSource = data

Thank you,
João
0
Eyup
Telerik team
answered on 19 May 2014, 10:26 AM
Hi João,

You can use the following approach to create a dummy source:
DropDownList1.DataSource = Enumerable.Range(0, 5).[Select](Function(x) New With {.ID = x, .Name = "Name " & x})
DropDownList1.DataTextField = "Name"
DropDownList1.DataValueField = "ID"
DropDownList1.DataBind()

Hope this helps.

Regards,
Eyup
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid
Asked by
JJ
Top achievements
Rank 1
Answers by
Elliott
Top achievements
Rank 2
JJ
Top achievements
Rank 1
Iana Tsolova
Telerik team
Vidisha
Top achievements
Rank 1
Jayesh Goyani
Top achievements
Rank 2
Sebastian
Telerik team
Saqib
Top achievements
Rank 1
João
Top achievements
Rank 1
Eyup
Telerik team
Share this question
or