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

11 posts, 0 answers
  1. JJ
    JJ avatar
    147 posts
    Member since:
    May 2010

    Posted 11 Feb 2011 Link to this post

     

    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!

  2. Elliott
    Elliott avatar
    391 posts
    Member since:
    May 2010

    Posted 11 Feb 2011 Link to this post

    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
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. JJ
    JJ avatar
    147 posts
    Member since:
    May 2010

    Posted 11 Feb 2011 Link to this post

    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.

     

  5. Iana Tsolova
    Admin
    Iana Tsolova avatar
    3388 posts

    Posted 16 Feb 2011 Link to this post

    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
  6. Vidisha
    Vidisha avatar
    1 posts
    Member since:
    May 2011

    Posted 08 Aug 2011 Link to this post

    Y all solution link is not working?
  7. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 08 Aug 2011 Link to this post

    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
  8. Sebastian
    Admin
    Sebastian avatar
    9934 posts

    Posted 09 Aug 2011 Link to this post

    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.

  9. Saqib
    Saqib avatar
    2 posts
    Member since:
    Jul 2013

    Posted 24 Jul 2013 Link to this post

     @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
  10. Saqib
    Saqib avatar
    2 posts
    Member since:
    Jul 2013

    Posted 24 Jul 2013 Link to this post


  11. João
    João avatar
    8 posts
    Member since:
    Apr 2014

    Posted 14 May 2014 in reply to Jayesh Goyani Link to this post

    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
  12. Eyup
    Admin
    Eyup avatar
    3005 posts

    Posted 19 May 2014 Link to this post

    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.

     
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017