473,395 Members | 1,393 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Can't catch Datagrid event! Please help.

My datagrid displays just fine. When I click on a row it posts
back but I can't seem to catch the event to do anything. My
handler never gets called. Can anyone please tell me why?
In the .aspx file:
------------------
<asp:DataGrid id="DataGrid1" runat="server" Width="400px"OnSelectedIndexChanged="DataGrid1_Sel ectedIndexChanged">
In the code behind:
-------------------
private void Page_Load(object sender, System.EventArgs e)
{
DataGrid1.ItemCreated += new DataGridItemEventHandler(this.Item_Created);
DataGrid1.SelectedIndexChanged += new EventHandler(this.DataGrid1_SelectedIndexChanged);

dbClass db = new dbClass();
DataSet ds = db.GetList();
DataGrid1.DataSource = ds;
DataGrid1.DataBind();
}

void Item_Created(Object sender, DataGridItemEventArgs e)
{
e.Item.Attributes.Add ("onmouseover", "this.style.backgroundColor='beige';this.style.cur sor='hand'");
e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor='white';");
e.Item.Attributes.Add("onclick", "javascript:__doPostBack('" + "DataGrid1:" + "ctrl" + e.Item.ItemIndex + ":ctrl0','')");
}

public void DataGrid1_SelectedIndexChanged (Object sender, EventArgs e)
{
Label2.Text = DataGrid1.SelectedItem.Cells[3].Text;
myTextBox.Text += "Hello from SelectedIndexChanged";
}
Thanks for your help.
Nov 18 '05 #1
5 3078
Pat
What happens if you comment out the "e.Item.Attributes.Add("onclick",
"javascript:__doPostBack('" + "DataGrid1:" + "ctrl" + e.Item.ItemIndex +
":ctrl0','')");" ??
"Bruce W.1" <bw@noDirectEmail.com> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
My datagrid displays just fine. When I click on a row it posts
back but I can't seem to catch the event to do anything. My
handler never gets called. Can anyone please tell me why?
In the .aspx file:
------------------
<asp:DataGrid id="DataGrid1" runat="server" Width="400px"OnSelectedIndexChanged="DataGrid1_Sel ectedIndexChanged">

In the code behind:
-------------------
private void Page_Load(object sender, System.EventArgs e)
{
DataGrid1.ItemCreated += new DataGridItemEventHandler(this.Item_Created);
DataGrid1.SelectedIndexChanged += new EventHandler(this.DataGrid1_SelectedIndexChanged);
dbClass db = new dbClass();
DataSet ds = db.GetList();
DataGrid1.DataSource = ds;
DataGrid1.DataBind();
}

void Item_Created(Object sender, DataGridItemEventArgs e)
{
e.Item.Attributes.Add ("onmouseover", "this.style.backgroundColor='beige';this.style.cur sor='hand'"); e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor='white';"); e.Item.Attributes.Add("onclick", "javascript:__doPostBack('" + "DataGrid1:" + "ctrl" + e.Item.ItemIndex + ":ctrl0','')"); }

public void DataGrid1_SelectedIndexChanged (Object sender, EventArgs e)
{
Label2.Text = DataGrid1.SelectedItem.Cells[3].Text;
myTextBox.Text += "Hello from SelectedIndexChanged";
}
Thanks for your help.

Nov 18 '05 #2
Pat wrote:
What happens if you comment out the "e.Item.Attributes.Add("onclick",
"javascript:__doPostBack('" + "DataGrid1:" + "ctrl" + e.Item.ItemIndex +
":ctrl0','')");" ??

================================================== ======

It does not post back (when clicking in the DataGrid) when this line is
commented out.
Nov 18 '05 #3
Two suggestions....

1. In your .aspx file, try adding a space between "400px" and
OnSelectedIndexChanged. It might matter it might not. Dumber things
have happened to me.

2. Try removing the OnSelectedIndexChanged declaration in the .aspx
You are already setting up the event handler in the Page_Load. It
might be messing it up.

Good luck,

Neil

"Bruce W.1" <bw@noDirectEmail.com> wrote in message news:<#s*************@tk2msftngp13.phx.gbl>...
My datagrid displays just fine. When I click on a row it posts
back but I can't seem to catch the event to do anything. My
handler never gets called. Can anyone please tell me why?
In the .aspx file:
------------------
<asp:DataGrid id="DataGrid1" runat="server" Width="400px"OnSelectedIndexChanged="DataGrid1_Sel ectedIndexChanged">
In the code behind:
-------------------
private void Page_Load(object sender, System.EventArgs e)
{
DataGrid1.ItemCreated += new DataGridItemEventHandler(this.Item_Created);
DataGrid1.SelectedIndexChanged += new EventHandler(this.DataGrid1_SelectedIndexChanged);

dbClass db = new dbClass();
DataSet ds = db.GetList();
DataGrid1.DataSource = ds;
DataGrid1.DataBind();
}

void Item_Created(Object sender, DataGridItemEventArgs e)
{
e.Item.Attributes.Add ("onmouseover", "this.style.backgroundColor='beige';this.style.cur sor='hand'");
e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor='white';");
e.Item.Attributes.Add("onclick", "javascript:__doPostBack('" + "DataGrid1:" + "ctrl" + e.Item.ItemIndex + ":ctrl0','')");
}

public void DataGrid1_SelectedIndexChanged (Object sender, EventArgs e)
{
Label2.Text = DataGrid1.SelectedItem.Cells[3].Text;
myTextBox.Text += "Hello from SelectedIndexChanged";
}
Thanks for your help.

Nov 18 '05 #4
nfedin wrote:
Two suggestions....

1. In your .aspx file, try adding a space between "400px" and
OnSelectedIndexChanged. It might matter it might not. Dumber things
have happened to me.

2. Try removing the OnSelectedIndexChanged declaration in the .aspx
You are already setting up the event handler in the Page_Load. It
might be messing it up.

Good luck,

Neil

================================================== ====

Good suggestions, but they did not work.

Is there a way to look at the event that caused the postback?
Nov 18 '05 #5
I should add that this is based on this article:
http://www.dotnetbips.com/displayarticle.aspx?id=205

I loaded the source code for this article and it works fine. You click
on a name in the datagrid and it puts the name in a label.

The only big difference I see in the code (other than being VB instead
of C#) is this line:

Protected WithEvents Label1 As System.Web.UI.WebControls.Label

I'm not sure why the label would be an event handler.
Nov 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bob Rosen | last post by:
The description given in "Customizing Items Dynamically in the DataList or DataGrid Web Server Control" (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vbCode/...
1
by: Supa Hoopsa | last post by:
I have a standard combo box in a datagrid (DataGridEnableComboBoxColumn) and I want to catch the SelectionChangeCommitted event. Does anyone know how to do this?
1
by: xrow | last post by:
Hello I have a simple webservice / c# application that receives data from server and prints the data in the asp:datagrid control I have problem when sorting data in datagrid I have created...
7
by: mhw | last post by:
How to catch the enter key of TestBox that DataGrid hits ?
3
by: needin4mation | last post by:
I'm writing c# asp.net. I have a dataset loading when the page loads and it has an itemdatabound event. When there is an error in the itemdatabound event I get the catch in the calling...
3
by: vinayak | last post by:
Hi I am displaying data in Datagrid in ASP.NET with Edit/Update functionality for each row. On the same page I have 2 Button controls which submits the request to server. These button controls...
1
by: Teep | last post by:
New to .net framework and in a bit of a crunch, I have found myself trying to figure out things from online examples. I have a datagrid which displays records from a SQL table, I also have two...
0
by: Gene Hubert | last post by:
I'm trying to catch the KeyUp event in textbox of a DataGrid. I'm picking up the keydown and keypress events ok, but not keyup. Can anyone see what is wrong with this code. I been fighting with...
5
by: Just Me | last post by:
Given a button name Btn_5 and Index=5 I want to do something like dim zz as string = Btn_??Index??.Text or given an array of buttons, do:
10
by: mttc | last post by:
I read articles that suggest preventing delete by throwing Exception from RowDeleting Event. I not understand where I can catch this Error?
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.