Dropdown event in datagrid

R

RH

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.
 
E

Elefterios Melissaratos

RH said:
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;
}
}

}
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top