469,578 Members | 1,445 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,578 developers. It's quick & easy.

GridView RowCommand event not firing!!

I am using a GridView inside a UserControl which has a template column for
deleting the rows. Before databinding the gridview i am attaching the
RowCommand and RowDataBound event.

I am using the RowDataBound event to set the commandargument of the delete
button. The event is being fired and works fine.

When I press the delete button, the RowCommand event is not firing! and
neither is the RowDeleting (the button's commandName is "Delete")
Any idea why?


Aug 8 '06 #1
6 27638
The order that event handlers execute can cause this kind of problem. Why are
you setting the command argument of the delete button in the RowDataBound
event, why not just declare it in the markup? Try that and see if the
RowCommand event fires.

"Kevin Attard" wrote:
I am using a GridView inside a UserControl which has a template column for
deleting the rows. Before databinding the gridview i am attaching the
RowCommand and RowDataBound event.

I am using the RowDataBound event to set the commandargument of the delete
button. The event is being fired and works fine.

When I press the delete button, the RowCommand event is not firing! and
neither is the RowDeleting (the button's commandName is "Delete")
Any idea why?


Aug 8 '06 #2
I've tried it but it still wont fire the event.
Here is the code:

protected void Page_Load(object sender, EventArgs e)
{
SetLinkedTable(GetSource())
}

private void SetLinkedTable(DataTable source)
{
gvLinkedTable = (GridView)skin.FindControl("gvLinkedTable");
AttachEvents();
gvLinkedTable.DataSource = source;
gvLinkedTable.DataBind();
this.Controls.Add(skin);
}

private void AttachEvents()
{
gvLinkedTable.RowCommand +=new
GridViewCommandEventHandler(gvLinkedTable_RowComma nd);
gvLinkedTable.RowDataBound += new
GridViewRowEventHandler(gvLinkedTable_RowDataBound );
}

private void gvLinkedTable_RowCommand(object sender,
GridViewCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
int pk = Convert.ToInt32(e.CommandArgument);
DeleteItem(pk);
SetLinkedTable(dt);
}
}


"clickon" <cl*****@discussions.microsoft.comwrote in message
news:D8**********************************@microsof t.com...
The order that event handlers execute can cause this kind of problem. Why
are
you setting the command argument of the delete button in the RowDataBound
event, why not just declare it in the markup? Try that and see if the
RowCommand event fires.

"Kevin Attard" wrote:
>I am using a GridView inside a UserControl which has a template column
for
deleting the rows. Before databinding the gridview i am attaching the
RowCommand and RowDataBound event.

I am using the RowDataBound event to set the commandargument of the
delete
button. The event is being fired and works fine.

When I press the delete button, the RowCommand event is not firing! and
neither is the RowDeleting (the button's commandName is "Delete")
Any idea why?

Aug 8 '06 #3
Again i think you are coming up against an order events fire type problem,
you are hooking up your GridView event handlers programatically in the
PageLoad event, how do you know that the PageLoad event fires before the
RowCommand event, if the RowCommand event fires before the PageLoad event
then the event has already happened before you even wire up the event handler.

Again i can't see any good reason for hooking up your event handlers
programatically from your code, the reason you do that usually is if the
control to which you are hooking up the event handlers has also been created
programatically. If you are creating the GridView declaritively in the mark
up then also wire up your event handlers like this.

"Kevin Attard" wrote:
I've tried it but it still wont fire the event.
Here is the code:

protected void Page_Load(object sender, EventArgs e)
{
SetLinkedTable(GetSource())
}

private void SetLinkedTable(DataTable source)
{
gvLinkedTable = (GridView)skin.FindControl("gvLinkedTable");
AttachEvents();
gvLinkedTable.DataSource = source;
gvLinkedTable.DataBind();
this.Controls.Add(skin);
}

private void AttachEvents()
{
gvLinkedTable.RowCommand +=new
GridViewCommandEventHandler(gvLinkedTable_RowComma nd);
gvLinkedTable.RowDataBound += new
GridViewRowEventHandler(gvLinkedTable_RowDataBound );
}

private void gvLinkedTable_RowCommand(object sender,
GridViewCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
int pk = Convert.ToInt32(e.CommandArgument);
DeleteItem(pk);
SetLinkedTable(dt);
}
}


"clickon" <cl*****@discussions.microsoft.comwrote in message
news:D8**********************************@microsof t.com...
The order that event handlers execute can cause this kind of problem. Why
are
you setting the command argument of the delete button in the RowDataBound
event, why not just declare it in the markup? Try that and see if the
RowCommand event fires.

"Kevin Attard" wrote:
I am using a GridView inside a UserControl which has a template column
for
deleting the rows. Before databinding the gridview i am attaching the
RowCommand and RowDataBound event.

I am using the RowDataBound event to set the commandargument of the
delete
button. The event is being fired and works fine.

When I press the delete button, the RowCommand event is not firing! and
neither is the RowDeleting (the button's commandName is "Delete")
Any idea why?


Aug 8 '06 #4
Kevin Attard wrote:
I am using a GridView inside a UserControl which has a template
column for deleting the rows. Before databinding the gridview i am
attaching the RowCommand and RowDataBound event.

I am using the RowDataBound event to set the commandargument of the
delete button. The event is being fired and works fine.

When I press the delete button, the RowCommand event is not firing!
and neither is the RowDeleting (the button's commandName is "Delete")
Any idea why?
Do not databind on postback.
Use a construct like:
If(Not Page.IsPostback) Then
GridView1.DataBind()
End If

If you databind again on postback, the event handling of child buttons is
broken.

--

Riki
Aug 8 '06 #5
The reason of wiring the events at runtime is because I am creating a
dynamic form from the database. Controls are created depending on the data
of the database.

Sorry, the code should have been like this: I'm using the constructor not
the pageload event

public myUserControl()
{
SetLinkedTable(GetSource())
}

This constructor is called in the overidden method CreateChildControls() of
another usercontrol
( the form creator usercontrol)
"clickon" <cl*****@discussions.microsoft.comwrote in message
news:85**********************************@microsof t.com...
Again i think you are coming up against an order events fire type problem,
you are hooking up your GridView event handlers programatically in the
PageLoad event, how do you know that the PageLoad event fires before the
RowCommand event, if the RowCommand event fires before the PageLoad event
then the event has already happened before you even wire up the event
handler.

Again i can't see any good reason for hooking up your event handlers
programatically from your code, the reason you do that usually is if the
control to which you are hooking up the event handlers has also been
created
programatically. If you are creating the GridView declaritively in the
mark
up then also wire up your event handlers like this.

"Kevin Attard" wrote:
>I've tried it but it still wont fire the event.
Here is the code:

protected void Page_Load(object sender, EventArgs e)
{
SetLinkedTable(GetSource())
}

private void SetLinkedTable(DataTable source)
{
gvLinkedTable = (GridView)skin.FindControl("gvLinkedTable");
AttachEvents();
gvLinkedTable.DataSource = source;
gvLinkedTable.DataBind();
this.Controls.Add(skin);
}

private void AttachEvents()
{
gvLinkedTable.RowCommand +=new
GridViewCommandEventHandler(gvLinkedTable_RowComm and);
gvLinkedTable.RowDataBound += new
GridViewRowEventHandler(gvLinkedTable_RowDataBoun d);
}

private void gvLinkedTable_RowCommand(object sender,
GridViewCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
int pk = Convert.ToInt32(e.CommandArgument);
DeleteItem(pk);
SetLinkedTable(dt);
}
}


"clickon" <cl*****@discussions.microsoft.comwrote in message
news:D8**********************************@microso ft.com...
The order that event handlers execute can cause this kind of problem.
Why
are
you setting the command argument of the delete button in the
RowDataBound
event, why not just declare it in the markup? Try that and see if the
RowCommand event fires.

"Kevin Attard" wrote:

I am using a GridView inside a UserControl which has a template column
for
deleting the rows. Before databinding the gridview i am attaching the
RowCommand and RowDataBound event.

I am using the RowDataBound event to set the commandargument of the
delete
button. The event is being fired and works fine.

When I press the delete button, the RowCommand event is not firing!
and
neither is the RowDeleting (the button's commandName is "Delete")
Any idea why?



Aug 8 '06 #6
I've eliminated the databinding on each postback as you suggested. When the
delete button is pressed, the RowCommand does not fire and when the page
loads, the gridview displays empty rows! So if I don't bind with every
postback, the data is lost. When Ive tried to press the button the second
time, the event is fired! Why is the event not firing on the first load but
only on the second?

"Riki" <ri**@dontnagme.comwrote in message
news:ub*************@TK2MSFTNGP03.phx.gbl...
Kevin Attard wrote:
>I am using a GridView inside a UserControl which has a template
column for deleting the rows. Before databinding the gridview i am
attaching the RowCommand and RowDataBound event.

I am using the RowDataBound event to set the commandargument of the
delete button. The event is being fired and works fine.

When I press the delete button, the RowCommand event is not firing!
and neither is the RowDeleting (the button's commandName is "Delete")
Any idea why?

Do not databind on postback.
Use a construct like:
If(Not Page.IsPostback) Then
GridView1.DataBind()
End If

If you databind again on postback, the event handling of child buttons is
broken.

--

Riki

Aug 9 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Mike P | last post: by
13 posts views Thread by AG | last post: by
7 posts views Thread by =?Utf-8?B?cGF0cmlja2RyZA==?= | last post: by
1 post views Thread by Just Me | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.