.NET Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgramming.NET Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old May 3rd, 2005, 06:16 PM
GopalS GopalS is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2005
Posts: 1 GopalS User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 14 sec
Reputation Power: 0
Unhappy Dynamic Dropdown within Datagrid

I am developing a asp.net page which has a datagrid popluating dynamically generated dropdown lists bound to the ID within the datagrid.
I need to javascript to be triggered on index changed of the dropdown list.

The placeholder generates a list of the parent procedures.
On Click they populate the datagrid with child procedures.
Each child procedure has a certain locations where the procedures are performed.

I am able to generate the dropdown lists but am having trouble with getting the onSelectedItemIndexChanged to fire the javascript.

Please help.


The code is as follows.

linkButton.aspx
**************
<%@ Page language="c#" Codebehind="LinkButton.aspx.cs" AutoEventWireup="false" Inherits="Training.WebForm9" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm9</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:PlaceHolder id="PlaceHolder1" runat="server"></asp:PlaceHolder>
<aspataGrid id="dgProcedures" DataKeyField="ProcedureID" AutoGenerateColumns="False" style="Z-INDEX: 101; LEFT: 24px; POSITION: absolute; TOP: 104px"
runat="server" OnItemDataBound="BuildLocationDropdown">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:CheckBox Runat="server" ID="ParentID"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="ChildName" HeaderText="Procedure"></asp:BoundColumn>
<asp:BoundColumn DataField="MaxDuration" HeaderText="Max Duration"></asp:BoundColumn>
<asp:BoundColumn DataField="ParentID" HeaderText="Parent ID"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Locations"></asp:TemplateColumn>
</Columns>
</aspataGrid>
</form>
<script language="javascript">
function getLocation(i)
{
// i = LocationID from the dropdown generated
alert ("i");
}

</script>
</body>
</HTML>
**********


linkButton.aspx.cs

*******************
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
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 Training
{
/// <summary>
/// Summary description for WebForm9.
/// </summary>
public class WebForm9 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.PlaceHolder PlaceHolder1;
protected System.Web.UI.WebControls.DataGrid dgProcedures;

DataSet ds=new DataSet();

private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
BindData(2);
}
LinkButton filterButton = new LinkButton();
Database db=new Database();
SqlDataReader reader = null;
db.RunProc("getParentList", out reader);

while (reader.Read())
{
filterButton= new LinkButton();
filterButton.BackColor=System.Drawing.Color.Beige;
filterButton.Text=reader.GetString(1);
filterButton.CommandArgument=Convert.ToString(read er.GetInt32(0));
filterButton.Command+=new CommandEventHandler(this.filterData);
PlaceHolder1.Controls.Add(filterButton);
PlaceHolder1.Controls.Add(new LiteralControl(" | "));
}
}
public void filterData(object sender, CommandEventArgs e)
{
BindData(Int32.Parse(e.CommandArgument.ToString()) );
}

public void BindData(int ParentID)
{
Database db=new Database();
SqlDataReader reader=null;
SqlParameter[] prams=
{
db.MakeInParam("@ParentID", SqlDbType.Int, 4, ParentID)
};
db.RunProc("getChildList", prams, out reader);
dgProcedures.DataSource=reader;

string SqlLocation="Select * from tblLocation";
SqlConnection conn;
conn=DBUtility.getDatabaseConnection();

SqlCommand cmd=new SqlCommand(SqlLocation, conn);
SqlDataAdapter lstDA= new SqlDataAdapter(cmd);
conn.Open();
lstDA.Fill(ds,"tblLocation");
dgProcedures.DataBind();
}

public void BuildLocationDropdown(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType.Equals(ListItemType.Item) || e.Item.ItemType.Equals(ListItemType.AlternatingIte m))
{

DropDownList lstLocation = new DropDownList();
string dlLocationName="";
int ProcedureID=Int32.Parse(((System.Data.Common.DbDat aRecord)e.Item.DataItem)["ProcedureID"].ToString());
DataView LocProcedures = new DataView();
LocProcedures=ds.Tables["tblLocation"].DefaultView;
LocProcedures.RowFilter="ProcedureID="+ProcedureID;
lstLocation.DataSource=LocProcedures;
lstLocation.DataTextField="LocationName";
lstLocation.DataValueField="LocationID";

lstLocation.DataBind();
lstLocation.Items.Insert(0,new ListItem("Select Location","0"));
Int32 LocationID = Convert.ToInt32(lstLocation.SelectedIndex);
e.Item.Cells[4].Controls.Add(lstLocation);

lstLocation.Attributes.Add("OnSelectedIndexChanged", "javascript:getLocation(LocationID)");
lstLocation.Attributes.Add("runat","Server");
lstLocation.Attributes.Add("AutoPostBack","True");
lstLocation.Attributes.Add("DataTextField", "LocationName");
lstLocation.Attributes.Add("DataValueField", "LocationID");

}
}


#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
}
}

************

Reply With Quote
  #2  
Old May 24th, 2005, 02:10 PM
HAdecco HAdecco is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2005
Posts: 1 HAdecco User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 m 57 sec
Reputation Power: 0
Did you find the solution?

Hello Gopals,

I am trying to do the same thing, and the event doesn't fire, I dont understand why. I will really appreciate if you can share the solution if you found one?

Thanks in advance.
HAdecco.

Quote:
Originally Posted by GopalS
I am developing a asp.net page which has a datagrid popluating dynamically generated dropdown lists bound to the ID within the datagrid.
I need to javascript to be triggered on index changed of the dropdown list.

The placeholder generates a list of the parent procedures.
On Click they populate the datagrid with child procedures.
Each child procedure has a certain locations where the procedures are performed.

I am able to generate the dropdown lists but am having trouble with getting the onSelectedItemIndexChanged to fire the javascript.

Please help.


The code is as follows.

linkButton.aspx
**************
<%@ Page language="c#" Codebehind="LinkButton.aspx.cs" AutoEventWireup="false" Inherits="Training.WebForm9" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm9</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:PlaceHolder id="PlaceHolder1" runat="server"></asp:PlaceHolder>
<aspataGrid id="dgProcedures" DataKeyField="ProcedureID" AutoGenerateColumns="False" style="Z-INDEX: 101; LEFT: 24px; POSITION: absolute; TOP: 104px"
runat="server" OnItemDataBound="BuildLocationDropdown">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:CheckBox Runat="server" ID="ParentID"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="ChildName" HeaderText="Procedure"></asp:BoundColumn>
<asp:BoundColumn DataField="MaxDuration" HeaderText="Max Duration"></asp:BoundColumn>
<asp:BoundColumn DataField="ParentID" HeaderText="Parent ID"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Locations"></asp:TemplateColumn>
</Columns>
</aspataGrid>
</form>
<script language="javascript">
function getLocation(i)
{
// i = LocationID from the dropdown generated
alert ("i");
}

</script>
</body>
</HTML>
**********


linkButton.aspx.cs

*******************
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
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 Training
{
/// <summary>
/// Summary description for WebForm9.
/// </summary>
public class WebForm9 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.PlaceHolder PlaceHolder1;
protected System.Web.UI.WebControls.DataGrid dgProcedures;

DataSet ds=new DataSet();

private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
BindData(2);
}
LinkButton filterButton = new LinkButton();
Database db=new Database();
SqlDataReader reader = null;
db.RunProc("getParentList", out reader);

while (reader.Read())
{
filterButton= new LinkButton();
filterButton.BackColor=System.Drawing.Color.Beige;
filterButton.Text=reader.GetString(1);
filterButton.CommandArgument=Convert.ToString(read er.GetInt32(0));
filterButton.Command+=new CommandEventHandler(this.filterData);
PlaceHolder1.Controls.Add(filterButton);
PlaceHolder1.Controls.Add(new LiteralControl(" | "));
}
}
public void filterData(object sender, CommandEventArgs e)
{
BindData(Int32.Parse(e.CommandArgument.ToString()) );
}

public void BindData(int ParentID)
{
Database db=new Database();
SqlDataReader reader=null;
SqlParameter[] prams=
{
db.MakeInParam("@ParentID", SqlDbType.Int, 4, ParentID)
};
db.RunProc("getChildList", prams, out reader);
dgProcedures.DataSource=reader;

string SqlLocation="Select * from tblLocation";
SqlConnection conn;
conn=DBUtility.getDatabaseConnection();

SqlCommand cmd=new SqlCommand(SqlLocation, conn);
SqlDataAdapter lstDA= new SqlDataAdapter(cmd);
conn.Open();
lstDA.Fill(ds,"tblLocation");
dgProcedures.DataBind();
}

public void BuildLocationDropdown(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType.Equals(ListItemType.Item) || e.Item.ItemType.Equals(ListItemType.AlternatingIte m))
{

DropDownList lstLocation = new DropDownList();
string dlLocationName="";
int ProcedureID=Int32.Parse(((System.Data.Common.DbDat aRecord)e.Item.DataItem)["ProcedureID"].ToString());
DataView LocProcedures = new DataView();
LocProcedures=ds.Tables["tblLocation"].DefaultView;
LocProcedures.RowFilter="ProcedureID="+ProcedureID;
lstLocation.DataSource=LocProcedures;
lstLocation.DataTextField="LocationName";
lstLocation.DataValueField="LocationID";

lstLocation.DataBind();
lstLocation.Items.Insert(0,new ListItem("Select Location","0"));
Int32 LocationID = Convert.ToInt32(lstLocation.SelectedIndex);
e.Item.Cells[4].Controls.Add(lstLocation);

lstLocation.Attributes.Add("OnSelectedIndexChanged", "javascript:getLocation(LocationID)");
lstLocation.Attributes.Add("runat","Server");
lstLocation.Attributes.Add("AutoPostBack","True");
lstLocation.Attributes.Add("DataTextField", "LocationName");
lstLocation.Attributes.Add("DataValueField", "LocationID");

}
}


#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
}
}

************

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgramming.NET Development > Dynamic Dropdown within Datagrid


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT