简体   繁体   中英

Alert Message is not showing

I am new in development, I want to show the alert message on button click after that I want to redirect the on another page. my button code is like below:

 protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
    Response.Write("<script type='text/javascript'> alert('Please wait for approval!');</script>");
                Response.Redirect("~/profile/scrapbook.aspx?uid=" + Request.QueryString["uid"], false);
}

in this case alert is not working, If I remove the Response.Redirect then it will works properly.

Please suggest me how to call he alert message after that redirect on another page.

You are mixing server side and client side code.

The Response.Redirect("~/profile/scrapbook.aspx?uid= is server side and the Alert won't wait the action of the user to execute it...

What you will need is to do you redirect in Javascript or to use something else than an Alert for the message.

Solution 1

You display a Alert message in Javascript (client side) when the user press okay you do a redirect in Javascript.

Solution 2

You display a message in the HTML with a button in ASP with an event that will do a call to the server and redirect your user to the page you desire.

Add your script like this:

 const string scriptString = "<script type='text/javascript'> alert('Your friend request sent to the user! Please wait for approval!');</script>";
                ClientScriptManager script = Page.ClientScript;
                script.RegisterClientScriptBlock(GetType(), "randomName", scriptString);

Do not use Response.Write to render your JavaScript. Use RegisterClientScriptBlock or RegisterStartupScript instead.

Your example should be using RegisterStartupScript , since it's rendering executing script, and not function declarations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM