1

Here is my current code:

Here is for default.aspx

<body>
    <form id="form1" runat="server">
    <div id="mydiv">

    </div>
    </form>
    <script>
        $(document).ready(function () {

            $.ajax({
                url: 'Default2.aspx',
                data: "{ 'name': '" + "randel" + "' }",

                type: "POST",
                success: function () {

                    // alert('insert was performed.');
                    $("#mydiv").empty();

                    $("#mydiv").load("Default2.aspx #div");

                },
                error: function (data, status, jqXHR) { alert(jqXHR); }
            });

        })
    </script>
</body>

Then for Default2.aspx, I want to access the data like this:

protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = Request.Form["name"].ToString();

    }

1 Answer 1

2

This looks like you want to use WebMethod in ASP.NET:

$(document).ready(function () {
            $.ajax({
                url: 'Default2.aspx/HelloWorld',
                data: "{ 'name': '" + "randel" + "' }",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    // alert('insert was performed.');
                    $("#mydiv").empty();
                    $("#mydiv").html(data);
                },
                error: function (data, status, jqXHR) { alert(jqXHR); }
            });
        });

and in your code behind you should do this:

[WebMethod()]
public static string HelloWorld(string name)
{
string message = "Hello " + name;
return message;
}

WebMethods are in some kind better than doing __doPostBack() because you controll all client-server traffic using for example jQuery. More about WebMethods: here or just google WebMethods ASP.NET.

And If you want to receive some form value you should put it on $.ajax data parameter and add the same parameter in WebMethod.

EDITED

From the code you post I see that you want send from Default.aspx some data to Default2.aspx and load some content from Default2.aspx (#div).

You can do this:

$.ajax({
                url: "/Default2.aspx",
                type: "GET",
                dataType: "html",
                async: false,
                data: { "name": "randel"
                },
                success: function (obj) {
                    // obj will contain the complete contents of the page requested
                    // use jquery to extract just the html inside the body tag
                    $content = $(obj).find('body #div').html();
                    // then update the dialog contents with this and show it
                }
         });

And in code behind:

protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = Request.QueryString["name"];
    }
Sign up to request clarification or add additional context in comments.

5 Comments

No, I want to access the data in the Default2.aspx page not in a webservice but thank you for the immediate reply. Thanks++
@randelramirez1 Take a look now.
thank for that solution but will I wanted to use "POST", but thanks++ :D
You cannot do POST in this case.
[WebMethod] is an attribute which can be applied to any function of webforms and not just a webservice.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.