0

I`ve got a link in a Webforms page which opens in a new window, and posts to a form other than the page itself. This is done like this:

JS:

var submitFunc = function(){
    document.forms[0].action = 'http://urlToPostTo/somePage.aspx';  
    document.forms[0].target = '_blank'; 
    document.forms[0].submit();
}

The link:

<input type="hidden" id="myDataId" value="12345" />     
<a href="javascript:submitFunc();">Search</a>

As you can see, this overrides the target of the main <form>, and attempts to post to a different page. The whole point is that this should post/submit to a different page, while avoiding passing the data of "myDataId" as a Get parameter.

The above code would work, but causes an error due to viewstate validation.

Question: Is there any way for me to either satisfy the requirements here, or somehow bypass the viewstate validation so I can post my data this way?

Kjartan
  • 18,591
  • 15
  • 71
  • 96

1 Answers1

1

Sounds like you just need to change the PostBackUrl of the button that submits the form.

A simple example PostToPage1.aspx that posts to PostToPage2.aspx (instead of a postback):

<form id="form1" runat="server">
<div>
    <asp:TextBox ID="text1" runat="server" /><br />
    <asp:TextBox ID="text2" runat="server" />
    <asp:Button ID="btn1" runat="server" PostBackUrl="~/PostToPage2.aspx" />
</div>
</form>

You can then inspect the Request in PostToPage2.aspx to check if it's a POST, etc. and then access the Request.Form collection in the Request in Page_Load.

An overly simplified sample for PostToPage2.aspx (do add proper validation):

if (!Page.IsPostBack && Request.RequestType == "POST")
    {
        if (Request.Form != null && Request.Form.Keys.Count > 0)
        {
           .....

Hth...

EdSF
  • 11,753
  • 6
  • 42
  • 83
  • Thanks for your input, but this gives an error saying I can only use one single Server-side `form` tag. I'm trying out some variations of this now. – Kjartan Oct 03 '12 at 06:34
  • @Kjartan yes, the above **isn't** adding yet another server side form. It is using the _same server side form_ that all ASP.net Web Forms pages use. – EdSF Oct 03 '12 at 14:17
  • Yup, I finally realized that, and managed to get it working. Thanks again for your help! :) – Kjartan Oct 04 '12 at 05:23
  • how do I get value of `text1` in PostToPage2.aspx? – Ronaldinho Learn Coding May 03 '16 at 17:31
  • @RonaldinhoLearnCoding there already is a sample above that inspects the HTTP POSTed values. At the end of the day, it's the `name` form field (rendered by Asp.net) of whatever input type control in the "origin" page - just like any other vanilla html form that POSTs data somewhere... – EdSF May 03 '16 at 21:17
  • @EdSF I see, that `name` rendered by asp.net is really ugly though, not `text1` or `text2` but something like `ctl00$ctl00$MainContent$MainContent$text1`, crazy!. – Ronaldinho Learn Coding May 03 '16 at 21:36
  • 1
    @RonaldinhoLearnCoding Yes, that's how it gets rendered. You can try [`static`](https://msdn.microsoft.com/en-us/library/system.web.ui.control.clientidmode(v=vs.110).aspx) mode and see if it helps. You can also try ["html server controls" instead of asp.net controls](http://stackoverflow.com/a/7860706/304683) and see if that makes it simpler. If you don't need to manipulate server side, then just use a standard ` – EdSF May 04 '16 at 00:22