tags:

views:

103

answers:

5

I am trying to submit an asp.net form (Default1.aspx) using jquery $.post() however I am unable to post to the other page (Test1.aspx). Here's what I have written

$("#Button1").click(function(e) {

                var t1 = $('#txt1').val();
                var t2 = $('#txt2').val();
                $.post('Test1.aspx', { text1: t1, text2: t2 }, function(result) {
                    alert(result);
                });
return false;
});

Markup is as here:

<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="txt1" runat="server">
            </asp:TextBox>
        <asp:TextBox ID="txt2" runat="server">
            </asp:TextBox>   
        <asp:Button ID="Button1" runat="server" 
            Text="Submit" />
    </div>
    </form>
</body>

How can i write the code to submit for using $.post() and read values on Test1.aspx. Can you give a working example.

UPDATE: I do not have a problem reading the values..the issue is the page is not navigating to Test1.aspx.

A: 

Your IDs are probably wrong. You're using the ID's you have in the markup, but once the page is rendered the IDs change to chain of naming container IDs and such. You could try doing something like:

$("#<%= Button1.ClientID %>").click(function(e) {
    var t1 = $('#<%= txt1.ClientID %>').val();
    var t2 = $('#<%= txt2.ClientID %>').val();
    $.post('Test1.aspx', { text1: t1, text2: t2 }, function(result) {
         alert(result);
    });
    return false;
});
Cory Larson
It seems i was not clear..I do not have a problem fetching the values..the page is not navigating to Test1.aspx..that's the issue
Satya
When using `$.post()`, you're posting the values to `Text1.aspx` via AJAX. The page is being navigated to behind the scenes. Are you looking for a redirect or something?
Cory Larson
yes i want to show the users that the values have been passed..how can i do that..can you suggest something apart from redirecting? i was originally planning to redirect and then display the transferred value..anyways waiting for your answer
Satya
also instead of using individual text fields, i plan to use $.post('Test1.aspx', $("#form1").serialize(), function(result) { alert(result); }); but alert() does not show anything
Satya
+1  A: 

Hi there

Simple!

$('#<%= Button1.ClientID %>').click(function(e) {

                var t1 = $('#<%= txt1.ClientID  %>').val();
                var t2 = $('#<%= txt2.ClientID  %>').val();
                $.post('Test1.aspx', { text1: t1, text2: t2 }, function(result) {
                    alert(result);
                });
return false;
});

When referencing to an ASP object in Client side, you need to use ClientID.

Update: about the redirecting.

$.post() does an HTTP AJAX POST to the file, not a HTTP POST. Thus it will not redirect, but do the post in backend. Instead should create another hidden form and submit it.

<script type="text/javascript">
$('#<%= Button1.ClientID %>').click(function(e) {

                var t1 = $('#<%= txt1.ClientID  %>').val();
                var t2 = $('#<%= txt2.ClientID  %>').val();

                $('#frmSInput1').val(t1);
                $('#frmSInput2').val(t2);
                $('#frmSubmit').submit();
return false;
});
</script>
<form id="frmSubmit" method="post" action="Test1.aspx">
  <input type="hidden" id="frmSInput1" name="text1" value="" />
  <input type="hidden" id="frmSInput2" name="text2" value="" />
</form>
thephpdeveloper
It seems i was not clear..I do not have a problem fetching the values..the page is not navigating to Test1.aspx..that's the issue
Satya
$.post() is an AJAX function. it posts via AJAX, not posting data to the page! http://docs.jquery.com/Ajax/jQuery.post
thephpdeveloper
Thanks Mauris..ok let us say i do not want to redirect and use your original example..how will i confirm that the fields got serialized and how to read it?
Satya
serialized? meaning? You mean to check whether it has been data has been sent?
thephpdeveloper
Yes i want to confirm that..
Satya
actually instead of using individual text fields, i plan to use $.post('Test1.aspx', $(this).serialize(), function(result) { alert(result); });
Satya
well if that's the cause then it shouldn't be a problem. but that way you can't redirect to the new page - only to get whatever the page returns.
thephpdeveloper
The reason that it's not navigating to Test1.aspx, is because the post is being done asynchronously, via Jquery - the browser effectively knows nothing about the operation and subsequently doesn't navigate to Test1.aspx. The primary reason for post via Jquesry is to prevent to browser form posting the form and navigating. If you want to navigate to test1.aspx, there's little to be gained by overriding the default browser behaviour.
belugabob
A: 

There's a pretty good article that should get you started here: http://dotnet.dzone.com/news/eliminating-postbacks-setting-

lomaxx
A: 

Assuming your jQuery function is in the same page that contains the ASP.NET form, you'll need to access the form fields using the IDs that are generated by ASP.NET on the client side, like so:

$("#<%=Button1.ClientID%>").click(function(e) 
{
    var t1 = $('#<%=txt1.ClientID%>').val();
    var t2 = $('#<%=txt2.ClientID%>').val();

    $.post('Test1.aspx', { text1: t1, text2: t2 }, function(result) {
        alert(result);
    });

    return false;
});
Tim S. Van Haren
A: 

I think you misunderstood the way the post method works. What happens is that in the background, jquery will submit the data you provide to the url you provide. The whole idea here is that your browser will not navigate to the page!

Anyway, if you want to navigate to a page using javascript, doesn't a simple 'document.location' work?

document.location = theurlIwantToGoTo;
Erik van Brakel
Ok thanks..may be i have actually not followed..I thought that after i have posted data, I can go on the other page and read it..How can I read the date on test1.aspx?
Satya
I tried Response.Write(Request["text1"]); on Test1.aspx but got nothing
Satya