I am having a really bad nightmare. One that is driving my feets into my grave soon.
I have a GridView which have an ImageButton column assigned with a Command Argument value during RowDataBound event. This is required because the Command Argument value is determined during runtime and its used for some operation later when the button is
clicked.
When the button is clicked (postback occuring), the associated runtime determined value will be lost, therefore to reinstate the value back, in my Page_Load event, I have the following code:
If Not IsPostBack Then
'Do some operations and Bind GridView
Else
'ReBind the GridView
End If
Thereafter, ideally the RowCommand of the GridView should fire which I will write some code to use the Command Argument value of the button to do some operation.
But this is not the fairytale land where all things are beautiful. I encounter the Invalid postback exception as soon as I click on the Image Button. In experimenting, I added in a regular Link Button to perform Exactly the same thing but I do not experience
the exception at all.
After further researching, I have attempt to turn off the EventValidation at the page level but by doing so, the RowCommand of the Grid is not firing after the image button is clicked. Although the exception does not occurs anymore. I have tried registering
the individual image button's Client ID using the RegisterForEventValidation in the Render method and it doesn't work as well.
Can someone tell me why is it that it doesn't work with an Image Button but with a Link Button? And why is it that turning off EventValidation affects the handler for the RowCommand of the GridView? Most importantly, how do i resolve this issue??
This is a major stumbling block for my progress and I really hope that someone (esp if any Microsoft folks is around here) can help me out here. [:'(]
I'm not an expert far from it, I can't answer your question but i usually turn image columns into a template field. I have plenty of grid views in a management area with lots of image buttons, and i approach it a bit different to you i would just have an
on click event on the image. Then to identify the sender i use sender as object if i need other info i use selected row, then get the command from there then i would perform code based on that and rebind grid view cutting out the page load if statement. Also
when i get a problem like this i may use a different event to fill a command arg like in the image template field i might use ondatabinding run code from there to bind the command arg.
My command arguments are based on data coming from a database but i don't have to rebind the grid on postback. Isn't viewstate for that
As i say I'm not an expert and I'm sure others know better than me, But i always turn bound columns to template field when i write code against them. Seems strange that it works with a link though
I once had same trouble and searched the web for hours and i couldn't find any real answer to the problem. I had to guess in the end.
Sorry if i haven't helped that much but I'm sure that approachh in a new way to do the same thing you will solve it.(maybe).......
Richard
Marked as answer by rexlin on Nov 16, 2006 01:41 AM
Thanks a lot for your post. Really appreciate it. You have helped a trillion. [:D]
I understand what you are saying and where you are coming from. My apology first for not making myself clear. I am already using a Template Column with ImageButton defined during Design time. I am binding the Command Argument value during RowDataBound but there
is a slight twist here.
I didn't fully explain what's the operation I was refering to during the DataBound and why is there a need to rebind the grid during Postback in my earlier post is because I do not want to make the post too lengthy and confusing. What happen here is that during
RowDataBound, I programatically create a "Child" row below each of the "Row Item" which the Child Row has some effects on the value which I suppose to be assigning into the Command Argument of the ImageButton. Therefore in order for the RowCommand to work
as desired using the Command Argument value of the ImageButton, need to be rebinded as since the "Child" row are programmatically created, the viewstate is not able to persist these information (or can it?).
My problem is when I rebind the Gridview, in the Page_Load, this Invalid Postback or callback argument exception is thrown. Right now, I am not able to think of any other work around for this issue.
If I am to look at a complete redesign of the way the "Child" rows are created, it is going to be a massive effort as it affects quite a number of areas in my project.
Hi mohawk523 and for those who might be experiencing similar problem as I did,
After much head banging, mouse trashing and monitor throwing, I have finally solved the issue! [:D]
The Invalid Postback or Callback argument that is raised when I do a "Double Bind" is due to the change in the generated Row ID of each GridView row.
Apparently if my assumption is not wrong, the generated Row IDs in the Postback seems to differ from the row id generated in the initial post. Therefore when I perform a Double Bind there is a conflict with these Row IDs which violated the EventValidation rules
and causes the Invalid Postback argument exception. I am not exactly 100% sure of my assumption either.
But I managed to resolve my issue by manually assigning each of the Row ID a unique ID during the RowDataRound event as follows:
e.Row.ID = [Generated Unique ID]
By doing so, I ensure that each row's ID is unique and consistent and it does not violate the EventValidation rules.
Hope that this helps to prevent others from banging their head as hard as I did.
You can assign the ID for each row in the GridView via the sample code below. The code belows will give each row a unique ID from a global variable.
Dim intGRID as Integer = 1 'Global declaration
Protected Sub SampleGridView_RowDataBound(ByVal sender
As Object, ByVal e
As System.Web.UI.WebControls.GridViewRowEventArgs) Handles SampleGridView.RowDataBound
e.Row.ID = "sampleGridRow_" & Cstr(intGRID)
intGRID += 1 'Increase intGRID by one
Just to second Paula, emptycan's solution worked great for me. Manually setting and incrementing (from 1 in increments of 1) the Row ID in the gridview RowDataBound event resoloves the error on postback. Thanks!! You resolved what appears to be a big headache.
Henk, not quite. The Guid would be different for each postback, recreating the original problem. Emptycan's solution works because his code will recreate the exact same IDs on each postback.
-Jason
henk
Hi all!
emptycan, thank you very much! Your solution is excellent!
If you don't want use additional variables, you can do this:
Its strange.. but what solved this issue for me was changing de text value of the buttonfield of the gridview to "Button". Before convert it to Template Field. The value I used before was the action it used...
Actually... what I said before is not true... the whole things works fine after the second databind is executed...... When I first load de page when I click in the image button i receive de error but If I click in a button that databinds the gridview again...
then.. the image buttons works just fine.... Anyone explains that?
emptycan
Member
30 Points
6 Posts
Invalid postback or callback argument with GridView's ImageButton Nightmare
Nov 14, 2006 02:46 AM|LINK
Hi all,
I am having a really bad nightmare. One that is driving my feets into my grave soon.
I have a GridView which have an ImageButton column assigned with a Command Argument value during RowDataBound event. This is required because the Command Argument value is determined during runtime and its used for some operation later when the button is clicked.
When the button is clicked (postback occuring), the associated runtime determined value will be lost, therefore to reinstate the value back, in my Page_Load event, I have the following code:
If Not IsPostBack Then
'Do some operations and Bind GridView
Else
'ReBind the GridView
End If
Thereafter, ideally the RowCommand of the GridView should fire which I will write some code to use the Command Argument value of the button to do some operation.
But this is not the fairytale land where all things are beautiful. I encounter the Invalid postback exception as soon as I click on the Image Button. In experimenting, I added in a regular Link Button to perform Exactly the same thing but I do not experience the exception at all.
After further researching, I have attempt to turn off the EventValidation at the page level but by doing so, the RowCommand of the Grid is not firing after the image button is clicked. Although the exception does not occurs anymore. I have tried registering the individual image button's Client ID using the RegisterForEventValidation in the Render method and it doesn't work as well.
Can someone tell me why is it that it doesn't work with an Image Button but with a Link Button? And why is it that turning off EventValidation affects the handler for the RowCommand of the GridView? Most importantly, how do i resolve this issue??
This is a major stumbling block for my progress and I really hope that someone (esp if any Microsoft folks is around here) can help me out here. [:'(]
mohawk523
Member
141 Points
42 Posts
Re: Invalid postback or callback argument with GridView's ImageButton Nightmare
Nov 14, 2006 07:24 AM|LINK
Hi emptycan,
I'm not an expert far from it, I can't answer your question but i usually turn image columns into a template field. I have plenty of grid views in a management area with lots of image buttons, and i approach it a bit different to you i would just have an on click event on the image. Then to identify the sender i use sender as object if i need other info i use selected row, then get the command from there then i would perform code based on that and rebind grid view cutting out the page load if statement. Also when i get a problem like this i may use a different event to fill a command arg like in the image template field i might use ondatabinding run code from there to bind the command arg.
My command arguments are based on data coming from a database but i don't have to rebind the grid on postback. Isn't viewstate for that
As i say I'm not an expert and I'm sure others know better than me, But i always turn bound columns to template field when i write code against them. Seems strange that it works with a link though
I once had same trouble and searched the web for hours and i couldn't find any real answer to the problem. I had to guess in the end.
Sorry if i haven't helped that much but I'm sure that approachh in a new way to do the same thing you will solve it.(maybe).......
Richard
emptycan
Member
30 Points
6 Posts
Re: Invalid postback or callback argument with GridView's ImageButton Nightmare
Nov 15, 2006 01:42 AM|LINK
Hi mohawk523,
Thanks a lot for your post. Really appreciate it. You have helped a trillion. [:D]
I understand what you are saying and where you are coming from. My apology first for not making myself clear. I am already using a Template Column with ImageButton defined during Design time. I am binding the Command Argument value during RowDataBound but there is a slight twist here.
I didn't fully explain what's the operation I was refering to during the DataBound and why is there a need to rebind the grid during Postback in my earlier post is because I do not want to make the post too lengthy and confusing. What happen here is that during RowDataBound, I programatically create a "Child" row below each of the "Row Item" which the Child Row has some effects on the value which I suppose to be assigning into the Command Argument of the ImageButton. Therefore in order for the RowCommand to work as desired using the Command Argument value of the ImageButton, need to be rebinded as since the "Child" row are programmatically created, the viewstate is not able to persist these information (or can it?).
My problem is when I rebind the Gridview, in the Page_Load, this Invalid Postback or callback argument exception is thrown. Right now, I am not able to think of any other work around for this issue.
If I am to look at a complete redesign of the way the "Child" rows are created, it is going to be a massive effort as it affects quite a number of areas in my project.
emptycan
Member
30 Points
6 Posts
Re: Invalid postback or callback argument with GridView's ImageButton Nightmare
Nov 15, 2006 02:16 AM|LINK
Hi mohawk523 and for those who might be experiencing similar problem as I did,
After much head banging, mouse trashing and monitor throwing, I have finally solved the issue! [:D]
The Invalid Postback or Callback argument that is raised when I do a "Double Bind" is due to the change in the generated Row ID of each GridView row.
Apparently if my assumption is not wrong, the generated Row IDs in the Postback seems to differ from the row id generated in the initial post. Therefore when I perform a Double Bind there is a conflict with these Row IDs which violated the EventValidation rules and causes the Invalid Postback argument exception. I am not exactly 100% sure of my assumption either.
But I managed to resolve my issue by manually assigning each of the Row ID a unique ID during the RowDataRound event as follows:
e.Row.ID = [Generated Unique ID]
By doing so, I ensure that each row's ID is unique and consistent and it does not violate the EventValidation rules.
Hope that this helps to prevent others from banging their head as hard as I did.
Cheers! [:D]
paulafernand...
Member
80 Points
27 Posts
Re: Invalid postback or callback argument with GridView's ImageButton Nightmare
Nov 15, 2006 09:31 PM|LINK
Hi emptycan
I want to ask you if can post an example on how you do the assign of an unique Id to a row.
I'm trying but I'm not getting there...
I understood that it has to be done on the RowDataBound of the grid, but what to you do then?
Thank's...
Paula
emptycan
Member
30 Points
6 Posts
Re: Invalid postback or callback argument with GridView's ImageButton Nightmare
Nov 16, 2006 12:45 AM|LINK
Hi Paula,
You can assign the ID for each row in the GridView via the sample code below. The code belows will give each row a unique ID from a global variable.
Dim intGRID as Integer = 1 'Global declaration
Protected Sub SampleGridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles SampleGridView.RowDataBound
e.Row.ID = "sampleGridRow_" & Cstr(intGRID)
intGRID += 1 'Increase intGRID by one
End Sub
Hope this helps.
dwilliams459
Member
77 Points
39 Posts
Re: Invalid postback or callback argument with GridView's ImageButton Nightmare
Jan 12, 2007 12:53 PM|LINK
.Net Senior Developer
henk
Member
20 Points
7 Posts
Re: Invalid postback or callback argument with GridView's ImageButton Nightmare
Mar 28, 2007 12:03 PM|LINK
emptycan, thank you very much! Your solution is excellent! [Yes]
If you don't want use additional variables, you can do this:
protected void SomeGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
}
jmoran1
Member
2 Points
3 Posts
Re: Invalid postback or callback argument with GridView's ImageButton Nightmare
Jul 23, 2007 08:49 PM|LINK
Henk, not quite. The Guid would be different for each postback, recreating the original problem. Emptycan's solution works because his code will recreate the exact same IDs on each postback.
-Jason
andrespibr
Member
2 Points
1 Post
Re: Invalid postback or callback argument with GridView's ImageButton Nightmare
Mar 15, 2008 04:36 PM|LINK
Hello guys,
Its strange.. but what solved this issue for me was changing de text value of the buttonfield of the gridview to "Button". Before convert it to Template Field. The value I used before was the action it used...
Actually... what I said before is not true... the whole things works fine after the second databind is executed...... When I first load de page when I click in the image button i receive de error but If I click in a button that databinds the gridview again... then.. the image buttons works just fine.... Anyone explains that?
André.