0
votes

I want to get the response value,but there is not response when I click the button; Why don't submit the form in asp.net?

My code:

<script type="text/javascript">
$(document).ready(function() { 
 var options = {
        target:        '#htmlTarget',
        dataType  :        'json',
        url   :        'Response.aspx',
        type  :        'post',
        beforeSubmit:  showRequest,
        success:       function(msg){ alert(msg.txt + '\n\n' + msg.item); }
 };
 $('#myForm1').ajaxForm(options);

});

function showRequest(formData, jqForm,options) {
    return true;
}

$.fn.serializeNoViewState = function()
{
   return this.find("input,textarea,select,hidden").not("[type=hidden][name^=__]").serialize();    
}

</script>
<form id="myForm1" runat="server">
<div id="htmlTarget"></div>

<input type="text" name="txt">
<br>
<input type="radio" name="item" value="A">A
<input type="radio" name="item" value="B">B
<input type="radio" name="item" value="C">C
<input type="radio" name="item" value="D">D
<br><br>
<input type="submit" name="submitbtn" value="submit">

</form>

Response.cs

public partial class Response : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;
        if (Request.Form["txt"] != null && Request.Form["item"] != null)
            Response.Write("{txt: '" + Request["txt"] + "',item: '" + Request["item"] + "'}");

    }
}
1

1 Answers

0
votes

Could it be because of id-mangling? ASP.NET feels that it can freely change your IDs for tags with runat="server". Check your generated HTML and check the ID of the form.

To solve that particular case, you can add a wrapping element that does not runat=server to the form, and target the form throught that one.

<div id="myform_wrapper">
    <!-- your form here -->
</div>

and target it with

$("#myform_wrapper form")