Click here to Skip to main content
15,879,474 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How do I write AJAX code to write a simple message?

I am not sure how "Message.ascx" is generated or made in advance.

No error code and the message in div content does not show.

JavaScript
[WebMethod]

public static string LoadUserControl(string message)
{

    using (Page page = new Page())
    {

        UserControl userControl = (UserControl)page.LoadControl("Message.ascx");

        (userControl.FindControl("lblMessage") as Label).Text = message;

        page.Controls.Add(userControl);

        using (StringWriter writer = new StringWriter())
        {

            page.Controls.Add(userControl);

            HttpContext.Current.Server.Execute(page, writer, false);

            return writer.ToString();

        }

    }

}


<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type = "text/javascript">

        $(document).ready(function () {
            $("#<%= Button1.ClientID %>").live("click", function () {
                $.ajax({
                    type: "POST",
                    url: "MyPage.aspx/LoadUserControl",
                    data: "{message: '" + $("#<%= TextBox1.ClientID %>").val() + "'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (r) {
                        $("#Content").append(r.d);
                    }
                });
            });
        });
    </script>



ASP.NET
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Load" />
<div id = "Content"></div>
Posted
Updated 3-Oct-14 13:12pm
v2

1 solution

When you click on the Button1, the page is reloads.
That may be the reason.

Brs,
--
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900