Scriptmanager alert not displaying properly in same page

Gani_tpt 1,686 Reputation points
2024-04-29T17:12:15.2666667+00:00

I am doing some simple screen add functionality and after end of saving, i am using scriptmanager.RegisterStartupScript() for save message.

when i run the program, the alert have not been throwing properly.

it showing same page but, without controls like new page.

I want to show the alert in the same page without any control disturbing.

How to do this.

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,303 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,363 questions
0 comments No comments
{count} votes

Accepted answer
  1. Lan Huang-MSFT 26,121 Reputation points Microsoft Vendor
    2024-04-30T02:40:19.3533333+00:00

    Hi @Gani_tpt,

    Scriptmanager alert not displaying properly in same page

    I do not quite understand you. Don't you want to achieve something similar to Scriptmanager alerts?

    I offer two methods that may help you.

    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Record Inserted Successfully')", true);

    User's image

    Or you can try using Bootstrap modal popups.

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
         <link rel="stylesheet" href='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.3/css/bootstrap.min.css' media="screen" />
     <script type="text/javascript" src='https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.3.min.js'></script>
     <script type="text/javascript" src='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.3/js/bootstrap.min.js'></script>
        <script type="text/javascript">
        function ShowPopup(title, body) {
            $("#MyPopup .modal-title").html(title);
            $("#MyPopup .modal-body").html(body);
            $("#MyPopup").modal("show");
        }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:Button ID="btnShowPopup" runat="server" Text="Show Popup" OnClick="ShowPopup"
        CssClass="btn btn-info btn-lg" />
            <!-- Modal Popup -->
            <div id="MyPopup" class="modal fade" role="dialog">
                <div class="modal-dialog">
                    <!-- Modal content-->
                    <div class="modal-content">
                        <div class="modal-header">
                            <h4 class="modal-title"></h4>
                            <button type="button" class="close" data-dismiss="modal">
                                &times;</button>
                        </div>
                        <div class="modal-body">
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-danger" data-dismiss="modal">
                                Close</button>
                        </div>
                    </div>
                </div>
            </div>
            <!-- Modal Popup -->
        </form>
    </body>
    </html>
    
    protected void ShowPopup(object sender, EventArgs e)
    {
        string title = "Greetings";
        string body = "Data inserted successfully!!!";
        ClientScript.RegisterStartupScript(this.GetType(), "Popup", "ShowPopup('" + title + "', '" + body + "');", true);
    }
    

    User's image

    Or you can simply use a Label control to display the information.

     <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
      <asp:Label ID="Label1" runat="server" ForeColor="Red"></asp:Label>
    
     protected void Button1_Click(object sender, EventArgs e)
     {
         Label1.Text = "Record Inserted Successfully";                              
     }
    

    User's image

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


2 additional answers

Sort by: Most helpful
  1. Michael Taylor 49,076 Reputation points
    2024-04-29T17:42:19.86+00:00

    You have provided us no code so we have no way of really helping you here. Provide your load logic for your page, whatever logic you're trying to do in the script, any server side handlers that are responding to the script events or "save", etc. The more information you provide the easier it is to help you.

    Note that startup scripts have to be inserted onto the page when it initially loads, not after a callback (which is how script manager works) IIRC. But without seeing your code it is hard to say. Ensure you have registered startup scripts as part of your page's load process and/or postback handling.


  2. Bruce (SqlWork.com) 57,646 Reputation points
    2024-04-29T17:52:59.3266667+00:00

    your code is doing a browser submit, server process, browser pre-render the response. if all you put in the page built by submit event is the alert, then that's all that is there. you need to render the page with all the content along with the alert.

    a common approach is to use javascript to:

    • ajax submit the form
    • display alert
    • redirect to next page (document.location = "...")
    0 comments No comments