Can't catch Datagrid event! Please help.

B

Bruce W.1

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_SelectedIndexChanged">


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.cursor='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.
 
P

Pat

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


Bruce W.1 said:
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_SelectedIndexChanged">


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.cursor='hand'");
"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.
 
B

Bruce W.1

Pat said:
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.
 
N

nfedin

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
 
B

Bruce W.1

nfedin said:
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?
 
B

Bruce W.1

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.
 

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

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top