Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Datagrid Control > Dropdown event in datagrid

Reply
Thread Tools

Dropdown event in datagrid

 
 
RH
Guest
Posts: n/a
 
      09-10-2003
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.
 
Reply With Quote
 
 
 
 
Elefterios Melissaratos
Guest
Posts: n/a
 
      09-10-2003
RH <> wrote in message news:<(E-Mail Removed)>. ..
> 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">
<aspataGrid 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>
<aspropDownList DataSource ='<%# GetColors()%>'
DataTextField="ColorName" DataValueField="ColorCode"
OnSelectedIndexChanged="DDL_SelectedIndexChanged" AutoPostBack="True"
ID="ddL" Runat="server"></aspropDownList>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</aspataGrid>
<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("Syste m.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;
}
}

}
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
1 Gridview. Dropdown A is column from database, Dropdown B is column from database, Data in A and B must be from same row. anonymoushamster@gmail.com ASP .Net 2 11-07-2007 12:40 PM
ajax cascading dropdown: second dropdown disabled acadam ASP .Net 0 12-27-2006 10:59 AM
bind a dropdown in a column in a datagrid based on the dropdown value selected in another column of the datagrid. vishnu ASP .Net 1 03-25-2006 01:24 PM
How to add a Dropdown list to a datagrid at runtime (dynamic) without using template columns in ASP.NET and still have the ability to us the datagrid Update event. Daniel Roth ASP .Net Datagrid Control 0 04-05-2005 03:58 AM
Select dropdown box bleeds into Javascript dropdown menu Mike HTML 1 12-18-2003 09:49 PM



Advertisments