Selectedindexchanged event not firing on gridview

  • Thread starter Thread starter Julia B
  • Start date Start date
J

Julia B

Hi all, sorry this is a re-post as I didn't get any responses a couple of
weeks ago, so I'm trying again.

I've got a gridview that has a a link button added under certain
circumstances. When the link button is pressed, I assumed that the
selectedindexchanged event of the gridview would fire but it doesn't. The
page does post back but nothing else happens. Here's the code I use for
adding the link button to the gridview:

If Me.tbStatus.Text <>
RC.RC_Web_AccountMonitor.CommonUtilities.ARStatus.Live.ToString And
Me.tbStatus.Text <>
RC.RC_Web_AccountMonitor.CommonUtilities.ARStatus.Terminated.ToString Then
Dim bfld As New ButtonField
bfld.ButtonType = ButtonType.Link
bfld.CommandName = "btnAmend"
bfld.Text = "Amend"
Me.dgAccs.Columns.Insert(0, bfld)
End If

Any ideas on why the event doesn't fire? Or should I be using another event,
or adding the button in a different way?

Thanks

Julia
 
SelectedIndexChanged fires only when a row's Select button is clicked, not
when any other control on the row is.

Although adding controls programmatically is possible, it is almost never
necessary. Much better idea is to have the column in the grid and to
hide/show it as needed in RowDataBound event.
 
Mark Rae said:
Unless you make it do so programatically by adding the following code to
the GridView's RowDataBound event:

protected void MyGridView_RowDataBound(object sender, GridViewRowEventArgs
e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick",
ClientScript.GetPostBackEventReference(MyGridView, "Select$" +
e.Row.RowIndex.ToString()));
e.Row.Style.Add("cursor", "pointer");
}
}


IMO, a Select button column in a GridView is a complete waste of screen
real estate. I never include one unless the client specifically asks for
it and, having seen the effect of the above, they never do...

With your first snippet, you can have the Select button nicely hidden.
 
Hi, unfortunately I do need a button on the grid view which will take users
to another page.

I tried the rowdatabound suggestion for my gridview as follows:

If e.Row.RowType = DataControlRowType.DataRow Then
e.Row.Attributes.Add("onclick",
ClientScript.GetPostBackEventReference(Me.dgAccs, "Select$",
e.Row.RowIndex.ToString()))
e.Row.Style.Add("cursor", "pointer")
End If

I get an error on page load which is "InvalidOperationException occurred -
RegisterForEventValidation can only be called during Render();"

Any ideas?
Thanks
Julia



Eliyahu Goldin said:
Mark Rae said:
Unless you make it do so programatically by adding the following code to
the GridView's RowDataBound event:

protected void MyGridView_RowDataBound(object sender, GridViewRowEventArgs
e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick",
ClientScript.GetPostBackEventReference(MyGridView, "Select$" +
e.Row.RowIndex.ToString()));
e.Row.Style.Add("cursor", "pointer");
}
}


IMO, a Select button column in a GridView is a complete waste of screen
real estate. I never include one unless the client specifically asks for
it and, having seen the effect of the above, they never do...

With your first snippet, you can have the Select button nicely hidden.

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
 
Thanks Mark, I've updated the code (missed the &) and I don't get an error.
The page loads fine. However when the link button is pressed the
selectedindexchanged event is still not firing on the gridview.

Is there something I'm missing?

Julia
 
Yes, I've put a breakpoint on the selectedindexchanged event for the gridview
and it doesn't get fired. The page is posted back though.
Julia
 
[Please don't top-post]
Apologies!!


That's not actually what I asked...
Again, apologies, I misread.... I've tried it again and no, the RowDataBound
event does not get fired when the button is clicked on the gridview. The page
gets posted back though.

Julia
 
More information to clarify....

Julia B said:
[Please don't top-post]
Apologies!!
However when the link button is pressed the selectedindexchanged
event is still not firing on the gridview.

Is there something I'm missing?

Hmm - are you sure that the event is actually being wired up
correctly...?
If you put a breakpoint in the RowDataBound event, does it break
there...?

Yes, I've put a breakpoint on the selectedindexchanged event for the
gridview
and it doesn't get fired. The page is posted back though.

That's not actually what I asked...
Again, apologies, I misread.... I've tried it again and no, the RowDataBound
event does not get fired when the button is clicked on the gridview. The page
gets posted back though.
I realised I probably didn't give clear enough information in the last post.
The RowDataBound event DOES fire when the page is first loaded so the code
you provided doesn't crash and gridview loads. However when the button is
pressed on the gridview, neither the RowDataBound or the SelectedIndexChanged
events fire, which means that the code I need to work doesn't and nothing
happens for the user.[/QUOTE]
 
Mark Rae said:
[Please don't top-post]

Mark, sincere apologies for deluging you with replies.....

I tried a couple of things and had a complete rethink. I've finally decided
to go with a select button and hide it and this seems to be working. I think
I'll take that route in future. Thanks for all your help and I won't top post
again :-)
 
Back
Top