Hi,

i have got a popup box on a aspx page using JavaScript - what i would like to happen is after the user clicks the box for the browser to navigate to a different aspx page.

i have tried using response.redirect and other methods - to no avail. i think that i need to modify the javascript - but i have no idea how.

free cyber sweeties to anyone that can solve my issue :) lol

//Displays a pop up box 
               string message = ("<script type = 'text/javascript'> window.onload=function(){alert('Drink Added!')};</script>");
               ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", message);

thanks :) xx

Recommended Answers

All 5 Replies

if its not possible to get the page to redirect on the user clicking the ok button in the dialouge box, can anyone suggest a different way of creating a notification box and redirecting when the user clicks ok? :) thanks

Here is the javascript code that can send redirect to another page.

var anotherpage = "another.html";
window.location.href = anotherpage;

In your case, while the alert box is visible, no other javascript is executed on the page. This means that you don't need to know when the user clicks the OK button. You know that OK was clicked if javascript code written after the alert() is executing.

body.onload = function() {
    alert("Drink Added!");
    // The following line will be executed when the alert box is dismissed by the user.
    window.location.href = "url_of_another_page.aspx";
};

Hope this helps.

Thank you very much for your help :)

however its still not working :( - this may be my fault as i didnt mention the script is being called on a button press.

if i modify to this

//run method from dll and if it returns true
            if (db.InsertUpdateData(cmd) == true)
            {   //Displays a pop up box utilising javascript to confirm the add operation
                string message = ("<script type = 'text/javascript'> body.onload=function(){alert('Item Type Added!'); window.location.href = NavHome.aspx;};</script>");
                ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", message);
            }

the pop up is not displayed at all

if i modify to this

//run method from dll and if it returns true
            if (db.InsertUpdateData(cmd) == true)
            {   //Displays a pop up box utilising javascript to confirm the add operation
                string message = ("<script type = 'text/javascript'> window.onload=function(){alert('Item Type Added!'); window.location.href = NavHome.aspx;};</script>");
                ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", message);
            }

the window still displays on the same page and no redirect happens here is the full code section of my page that is used

//called on button press
  public void Item(ABDLL db){
    
        //SQL Insert Query to be passed to the DLL
        string strQuery = "INSERT INTO food (food) values (@food) ";

        try
        {
            //Create SQL Cmd     
            SqlCommand cmd = new SqlCommand(strQuery);
            //Add Paramaters
            cmd.Parameters.Add("@food", SqlDbType.VarChar).Value = TextBox1.Text;
            
            //run method from dll and if return true
            if (db.InsertUpdate(cmd) == true)
            {   //Displays a pop up box
                string message = ("<script type = 'text/javascript'> window.onload=function(){alert('food Type Added!'); window.location.href = NavHome.aspx;};</script>");
                ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", message);
            }
            else
            {    //Displays a pop up box 
                string message = ("<script type = 'text/javascript'> window.onload=function(){alert('Error food Type Not Added')};</script>");
                ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", message);
                //displays error
                lblMessage.Text = db.getError();
            }
        }
        catch (System.Data.SqlClient.SqlException ex)
        {
            lblMessage.Text = ex.Message;
        }
    }

any suggestions as to why its not working?

thanks again for your post though :)

window.location.href expects a string value to be assigned to it. If you look at the code I posted:

var anotherpage = "another.html";
// variable 'anotherpage' is a javascript string here
window.location.href = anotherpage;

But your C# code produces the following javascript:

<script type = 'text/javascript'>
window.onload = function() {
    alert('food Type Added!');
    window.location.href = NavHome.aspx;
};
</script>

'NavHome.aspx' in the code above is not a variable name (it would be invalid variable name due to the dot) and it is not a javascript string. You absolutely MUST enclose it in quotes.

string message = ("<script type='text/javascript'> window.onload=function(){alert('food Type Added!'); window.location.href = 'NavHome.aspx';};</script>");

Hope this works for you.

commented: Provided perfect answer :) +2

PERFECT! Thank you so much :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.