0

I have one aspx page from which I am posting form like

            var form = document.createElement("form");
            form.setAttribute("method", 'post');
            form.setAttribute("action", '/my.ashx');

            var field = document.createElement("input");
            field.setAttribute("type", "hidden");
            field.setAttribute("name", 'myVal');
            field.setAttribute("value", "1234627373737377");
            form.appendChild(field);

            document.body.appendChild(form);
            form.submit();

And I am trying to access myVal on my.ashx page by using var myValue= context.Request["myVal"]; but I am getting null value.

snehad
  • 127
  • 11
  • `setAttribute` smells fishy to me. I would try setting properties directly like in the following examples. 1) http://stackoverflow.com/questions/2086718/info-on-javascript-document-createelement 2) http://stackoverflow.com/questions/3991204/how-to-create-a-form-dynamically-using-javascript – dana Aug 13 '13 at 05:24

3 Answers3

0

Try this on .ashx page:

HtmlInputHidden input = PreviousPage.FindControl("myVal") as HtmlInputHidden;
string previousValue = input.Value;

include the assembly reference on the top of the page:

using System.Web.UI.HtmlControls;

Although not tested, but hope it would work.

Khadim Ali
  • 2,548
  • 3
  • 33
  • 61
  • showing error that 'Previous Page' does not exist in the current context – snehad Aug 12 '13 at 13:04
  • 1
    if you can change the technique slightly? instead of creating the hidden input type, place the input element in the page html. then test if it works? `` – Khadim Ali Aug 12 '13 at 13:08
0

Try

myValue = context.Request.Form["myVal"]

Udpated:

You're missing an attribute with form form.setAttribute("enctype", 'multipart/form-data'); with out setting it your Form variable in Request will not be populated.

vendettamit
  • 14,315
  • 2
  • 32
  • 54
  • used this Still not working var form = document.createElement("form"); form.setAttribute("enctype", 'multipart/form-data'); form.setAttribute("method", 'post'); form.setAttribute("action", '/my.ashx'); var field = document.createElement("input"); field.setAttribute("type", "hidden"); field.setAttribute("name", 'myVal'); field.setAttribute("value", "1234567890"); form.appendChild(field); document.body.appendChild(form); form.submit() – snehad Aug 12 '13 at 13:14
  • Try debug in your handler when receiving the request. Check if your request have Form property initialized. Rest looks good to me. – vendettamit Aug 12 '13 at 13:21
0

Add this while submitting: field.setAttribute("id", 'myVal');

And access it like: myValue = context.Request.Form["myVal"]

Visual
  • 1
  • 1