• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 426
  • Last Modified:

Invalid postback or callback argument. - asp.net

I have a web application built on Asp.Net 1.1. Everything worked fine on my local machine IIS. I then published the application on Web (added as website on IIS on our Exchange Server). Here are the two cases:

(1) If I set the ASP.NET Version in the IIS as 1.1, then the RequiredFieldValidators do not work. This is really bad because I have used it all over the web application.
(2) If I set the ASP.NET version in the IIS as 2.0, then the RequiredFieldValidators work. But in one of the pages I need make a simple call to another website
<a href = 'thirdpartywebsite.com'></a>

When I click on that link I get the attached error.
I don't get this error when the ASP.NET version is 2.0.

What should I do to fix both these errors ?


error.JPG
0
Angel02
Asked:
Angel02
  • 4
  • 3
2 Solutions
 
ddayx10Commented:
Well that doesn't make a lot of sense...hmmm.

You usually see that error in .net 2 and above when trying to do a postback from JavaScript using __DoPostBack.

There is no reason for that error to occur when clicking a link to a different page entirely. The error involves securing the postback to the same page. In this case you are not doing a postback and you are going to another page entirely.

The only thing that makes sense is if you have JavaScript handling the click events of your anchor tags and trying to postback to capture some information(as well as go to another page)?? It's a pretty good guess that there is more interaction going on than a simple click of an anchor.

Anyway the simple solution under normal circumstances is to set EnableEventValidation=False in that page's directive...maybe that will work here as well.
0
 
Easwaran ParamasivamCommented:
Have a linkbutton instead of <a> tag and in the onclick event call the javascript function having below code.

window.open('thirdpartywebsite.com')

This won't cause for postback as this is client side function.
0
 
Angel02Author Commented:
@ddayx10
You are right. "there is more interaction going on than a simple click of an anchor."
It is a datagrid and I display the link in one of the columns like in the attached code.

I realized what was causing the error. But I don't know why!
I had the following code in Datagrid.ItemDataBound method

If TypeOf (e.Item.FindControl("selRow")) Is LinkButton Then
                Dim button As LinkButton = CType(e.Item.FindControl("selRow"), LinkButton)
                e.Item.Attributes("onclick") = Page.GetPostBackClientHyperlink(button, "")
            End If

This was throwing the "Invalid Postback ..." error. When I changed the method to handle Datagrid.ItemCreated, the error was gone.

@All
The problem is I don't want to use <a href....> as my url contains a query string with secured ID. I am trying to add the following code in DataGrid.ItemCommand

Sub Select_Item(ByVal sendar As System.Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles MyDataGrid.ItemCommand
     
writestring = "<script language='javascript'>window.open(' http://www.thirdpartywebsite/index.aspx?the_cid=" & e.Item.Cells(6).Text & "'");</script>"
HttpContext.Current.Response.Write(writestring)

End Sub


But when I post the web application on the web, the ItemCommand is never reached. The control is dying at the ItemCreated. What can be wrong?




<asp:TemplateColumn ItemStyle-HorizontalAlign="center" HeaderStyle-HorizontalAlign="Center">
<HeaderTemplate>
	Name
</HeaderTemplate>
<ItemTemplate>
<a href='http://www.thirdpartywebsite.com/index.aspx?the_id=<%#DataBinder.Eval(Container.DataItem,"the_id")%>' target="_blank">													

<%#DataBinder.Eval(Container.DataItem,"strname")%></a>
			</ItemTemplate>
				</asp:TemplateColumn>

Open in new window

0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
ddayx10Commented:
Well I'm not sure what you mean by "the control is dying at the ItemCreated". Do you mean the DataGrid is not even rendered, or do you mean you just never see the ItemCommand fire?

I imaging everying is fine in building the DataGrid but I can see why the ItemCommand for the sample you have posted will not run. The itemcommand will not fire unless you have a server control where you currently have your <a>(link). A LinkButton or a button is needed to cause the event to fire.

You might want to change things around a little like this:

NOTE: The names of my columns and grid are alittle different than yours so you will need to do a little editing.


<asp:TemplateColumn>
      <itemtemplate>
        <asp:LinkButton ID="LnkBtn" runat="server"
          CommandArgument='<%#DataBinder.Eval(Container.DataItem,"ItemNumber")%>'
          Text='<%#DataBinder.Eval(Container.DataItem, "Name")%>' />
      </itemtemplate>
</asp:TemplateColumn>


    Sub Select_Item(ByVal sendar As System.Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid.ItemCommand
        Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), "script", _
        String.Format("window.open('http://www.thirdpartywebsite/index.aspx?the_cid={0}');", e.CommandArgument), True)
    End Sub
0
 
Angel02Author Commented:
The datagrid is rendered and ItemCreated is also fired. But ItemCommand is just never fired.
I saw what you asked me to do. I did something like that. I will paste little more of my code, where you will see I have another invisible column with a linkbutton. this is how my datagrid looks

<asp:datagrid id="MyDataGrid" runat="server" Width="724px" HeaderStyle-CssClass="stylewhitebold"
      OnItemCommand="Select_Item" OnItemCreated="Create_Item" ShowHeader="True" BorderColor="#ffffff"
      GridLines="Both" AutoGenerateColumns="False">
      <Columns>
            <asp:TemplateColumn HeaderText="Pick" Visible="false" ItemStyle-Width="20" ItemStyle-Wrap="False">
            <ItemTemplate>
            <asp:LinkButton ID="selRow" Runat="server" Text="<img border=0 src=/images/arrow.gif height=10 weight=18>"
            CommandName="Select"></asp:LinkButton>
            </ItemTemplate>
            </asp:TemplateColumn>
            <asp:TemplateColumn ItemStyle-HorizontalAlign="center" >
            <HeaderTemplate>
            Name
            </HeaderTemplate>
            <ItemTemplate>      <%#DataBinder.Eval(Container.DataItem,"strname")%>
            </ItemTemplate>
            </asp:TemplateColumn>
</Columns>
</datagrid>


Sub Create_Item(ByVal sendar As System.Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles MyDataGrid.ItemDataBound
        Dim itemType As ListItemType = e.Item.ItemType
        If (itemType = ListItemType.Header) Then
            Return
        Else
            If TypeOf (e.Item.FindControl("selRow")) Is LinkButton Then
                Dim button As LinkButton = CType(e.Item.FindControl("selRow"), LinkButton)
                e.Item.Attributes("onclick") = Page.GetPostBackClientHyperlink(button, "")
            End If
        End If
End Sub

When I click on any row in the database, the ItemCommand is not fired at all. Is there any other event or any other way I can add the window.open  upon clicking the row or the "Name" column?
0
 
Angel02Author Commented:
I must mention again that the ItemCommand is being fired on my local machine. But when I published the web application on the web and tested, it is not being fired at all.
0
 
ddayx10Commented:
And yet you posted code with a template that had nothing in it that would call an itemcommand.

*******
Sub Select_Item(ByVal sendar As System.Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles MyDataGrid.ItemCommand
     
    writestring = "<script language='javascript'>window.open(' http://www.thirdpartywebsite/index.aspx?the_cid=" & e.Item.Cells(6).Text & "'");</script>"
HttpContext.Current.Response.Write(writestring)
End Sub

<asp:TemplateColumn ItemStyle-HorizontalAlign="center" HeaderStyle-HorizontalAlign="Center">
<HeaderTemplate>
      Name
</HeaderTemplate>
<ItemTemplate>
<a href='http://www.thirdpartywebsite.com/index.aspx?the_id=<%#DataBinder.Eval(Container.DataItem,"the_id")%>' target="_blank">                                                                              

<%#DataBinder.Eval(Container.DataItem,"strname")%></a>
</ItemTemplate>
</asp:TemplateColumn>
************************


That code intrinsically will not cause an itemcommand to fire. What am I supposed to think?

If it works locally for you then this is alot like the 1st time around where there is something important going on that you are not mentioning. The link in your itemtemplate (<a>) will not cause a postback and therefore will not cause the itemcommand to fire.

So I'll assume it works locally because you have something going on you didnt mention that makes the link cause a postback (very confusing). Why the item command doesnt work? You haven't given enough information to say. Why is it dying at the itemcreated event? In that case (apples to apples, meaning both versions are identical) if it works locally and not live then my 1st guess is there's a difference in the data between the two sources and it is erroring out on some conversion.

Am I supposed to be answering why the itemcommand is not firing? If so explain how you are getting a postback out of the datagrid to fire the itemcommand.

If why no itemcreated completes on live server? In that case all you can do is surround the code with a try/catch and get it to report to you the ex.message if one exists. Unfortunately there could be a million reasons this is happening.

0
 
Angel02Author Commented:
There has been some misunderstanding in my forums. Sorry about that. There are two different cases here.
Case One:
I was using Itemtemplate <a> when I started the thread. It was throwing me the 'Invalid postback...' error. I replaced the ItemDataBound with ItemCreated and the error was gone. If I clicked the <a> link I could open the new window.Everything was working. I did not want the ItemCommand to work in this case.

Case Two:
After looking at EaswaranP post, I realized I don't want to use Itemtemplate <a> because 'View Source' would show up the link and other reasons. So I removed the <a> Itemtemplate and posted new code in post ID:37074011. This code was not firing ItemCommand.
I had a linkbutton already in my code   (post ID:37074011) name <asp:LinkButton ID="selRow"> which I thought would do the postback but it didn't. Then I tried your code:

 

 
<asp:datagrid id="MyDataGrid" runat="server" Width="724px" HeaderStyle-CssClass="stylewhitebold"
	OnItemCommand="Select_Item" OnItemCreated="Create_Item" ShowHeader="True" BorderColor="#ffffff"
			GridLines="Both" AutoGenerateColumns="False">
<Columns>
	<asp:TemplateColumn ItemStyle-HorizontalAlign="center" ItemStyle-ForeColor="#ffffff" HeaderStyle-HorizontalAlign="Center">
											<HeaderTemplate>
												Name
											</HeaderTemplate>
											<ItemTemplate>	
											<asp:LinkButton ID="LnkBtnName" runat="server"
          Text='<%#DataBinder.Eval(Container.DataItem,"strname")%>' />
											</ItemTemplate>
										</asp:TemplateColumn>
										<asp:BoundColumn DataField="the_id" HeaderText="id" HeaderStyle-HorizontalAlign="Center" ItemStyle-Width="20" Visible="false" ItemStyle-HorizontalAlign="Center" ItemStyle-ForeColor="#ffffff"></asp:BoundColumn>
	</Columns>
</asp:datagrid>

Open in new window



The good news is now the ItemCommand is being fired !
Anyway, now at least I know what the code is doing. Pretty clear picture. Thanks for the help.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now