开发者

Html form method inside Asp.net form not posting data

I am trying to post data using javascript to generic handler but I am unable to upload.

Can anyone suggest me where I am going wrong?

Here is my code:

    <script type="text/javascript">

         function postdata() {
             var fieldValue = document.getElementById("field1").value;
             postwith("Upload.ashx", { field1: fieldValue });
         }

         function postwith(to, p) {
             var myForm = document.createElement("form");
             myForm.method = "post";
             myForm.a开发者_JAVA百科ction = to;
             for (var k in p) {
                 var myInput = document.createElement("input");
                 myInput.setAttribute("name", k);
                 myInput.setAttribute("value", p[k]);
                 myForm.appendChild(myInput);
             }
             document.body.appendChild(myForm);
             myForm.submit();
             document.body.removeChild(myForm);
         }

</script>

This is the actual thing I need to do with Form:

<form id="Form1" action="Upload.ashx" method="POST"  enctype="multipart/form-data"
    runat="server">

As I am having a master page I need to do this way:

  <button type="submit" class="start"  OnClientClick="postdata(); return false;">
                Start upload</button>


Rather than the form approach, consider using XmlHttpRequest: http://www.ibm.com/developerworks/web/library/wa-ajaxintro2/

If you are using JQuery or Microsoft ASP.NET AJAX frameworks, there are helpers to make this much, MUCH easier (JQuery with $.ajax and ASP.NET AJAX with Sys.Net.WebRequest I believe).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜