Dropdown event in datagrid

Discussion in 'ASP .Net Datagrid Control' started by RH, Sep 10, 2003.

  1. RH

    RH Guest

    Hi,
    I have a dropdownlist in a datagrid and want to respond to the
    SelectedItemChanged event. I can't seem to find the dropdownlist in
    the class list and therefore cannot code the event. How can I respond
    to the event?
    TIA
    Remco.
    RH, Sep 10, 2003
    #1
    1. Advertising

  2. RH <> wrote in message news:<>...
    > Hi,
    > I have a dropdownlist in a datagrid and want to respond to the
    > SelectedItemChanged event. I can't seem to find the dropdownlist in
    > the class list and therefore cannot code the event. How can I respond
    > to the event?
    > TIA
    > Remco.



    Below I provide some code:

    In the .aspx file I define a datagrid with two columns:
    1. One <asp:BoundColumn>
    2. One Template column that includes the dropdown list.

    In the Dropdownlist I have specified several attributes among them:
    a. DataSource ='<%# GetColors()%>' // the GetColors() function is
    defined in the codebehind.
    b. OnSelectedIndexChanged="DDL_SelectedIndexChanged" // the event
    handler
    (see codebehind )

    Please see review both the .aspx and code behind files which I include
    below:

    I hope this helps

    Regards

    Lefteris


    ---- .ASPX file--------------------------------------------

    <%@ Page language="c#" Codebehind="DropDownInDataGrid.aspx.cs"
    AutoEventWireup="false" Inherits="ASPNETDataGrid.DropDownInDataGrid"
    %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>DropDownInDataGrid</title>
    <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" Content="C#">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema"
    content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body MS_POSITIONING="GridLayout">
    <form id="Form1" method="post" runat="server">
    <asp:DataGrid AutoGenerateColumns="False" id="DG" style="Z-INDEX:
    101; LEFT: 74px; POSITION: absolute; TOP: 109px"
    runat="server" Width="230px" Height="141px">
    <Columns>
    <asp:BoundColumn DataField="ObjectName"></asp:BoundColumn>
    <asp:TemplateColumn>
    <ItemTemplate>
    <asp:DropDownList DataSource ='<%# GetColors()%>'
    DataTextField="ColorName" DataValueField="ColorCode"
    OnSelectedIndexChanged="DDL_SelectedIndexChanged" AutoPostBack="True"
    ID="ddL" Runat="server"></asp:DropDownList>
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    </asp:DataGrid>
    <asp:Label id="Label1" style="Z-INDEX: 102; LEFT: 389px; POSITION:
    absolute; TOP: 56px" runat="server"
    Width="128px"></asp:Label>
    </form>
    </body>
    </HTML>

    -----CODE BEHIND file-----------------------------------------
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;

    namespace ASPNETDataGrid
    {
    /// <summary>
    /// Summary description for DropDownInDataGrid.
    /// </summary>
    public class DropDownInDataGrid : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.Label Label1;
    protected System.Web.UI.WebControls.DataGrid DG;

    private void Page_Load(object sender, System.EventArgs e)
    {
    // Put user code to initialize the page here
    if (! IsPostBack)
    {
    DG.DataSource = GetObjects();
    DG.DataBind();
    }
    }

    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {

    this.Load += new System.EventHandler(this.Page_Load);

    }
    #endregion

    protected DataTable GetObjects()
    {
    DataTable dt = new DataTable();
    DataColumn dc = new
    DataColumn("ObjectName",System.Type.GetType("System.String"));
    dt.Columns.Add(dc);

    DataRow dr = dt.NewRow();
    dr["ObjectName"] = "Automobile";
    dt.Rows.Add(dr);

    dr = dt.NewRow();
    dr["ObjectName"] = "Boat";
    dt.Rows.Add(dr);

    dr = dt.NewRow();
    dr["ObjectName"] = "Airplane";
    dt.Rows.Add(dr);

    return dt;

    }
    protected DataTable GetColors()
    {
    DataTable dt = new DataTable();
    DataColumn dc = new
    DataColumn("ColorName",System.Type.GetType("System.String"));
    dt.Columns.Add(dc);
    dc = new DataColumn("ColorCode",System.Type.GetType("System.Int32"));
    dt.Columns.Add(dc);
    DataRow dr = dt.NewRow();
    dr["ColorName"] = "Red";
    dr["ColorCode"] = 1;
    dt.Rows.Add(dr);

    dr = dt.NewRow();
    dr["ColorName"] = "Blue";
    dr["ColorCode"] = 2;
    dt.Rows.Add(dr);

    dr = dt.NewRow();
    dr["ColorName"] = "Green";
    dr["ColorCode"] = 3;
    dt.Rows.Add(dr);

    return dt;
    }

    protected void DDL_SelectedIndexChanged(object source,EventArgs e)
    {
    DropDownList dd = source as DropDownList;
    if (dd != null)
    {
    Label1.Text = dd.SelectedItem.Text;
    }
    }

    }
    }
    Elefterios Melissaratos, Sep 10, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. vishnu
    Replies:
    1
    Views:
    1,187
    Patrick.O.Ige
    Mar 25, 2006
  2. Mike
    Replies:
    1
    Views:
    791
  3. acadam
    Replies:
    0
    Views:
    744
    acadam
    Dec 27, 2006
  4. Replies:
    2
    Views:
    429
  5. Steve Myers
    Replies:
    2
    Views:
    263
    Steve Myers
    Nov 20, 2003
Loading...

Share This Page