Click here to Skip to main content
15,911,360 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have an alert redirect issue. When the user click on submit the alert message displays. If the user clicks on Ok the page will reload and the user can enter another one. If the user clicks Cancel the page should redirect using the redirect code I have in place. How can I get the redirect to work on the alert message with the redirect code I have?

C#
if (Page.IsValid)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "Alert", "if(confirm('You Have Successfully Submitted the Cohort! Do you have Additional Cohorts Please Click Yes to Enter another Cohorts? If you are finished entering Cohorts, Please click No.') == false){ window.location();}else{window.location.href='Gradrate.aspx';}", true);
            }
            else
            {
                SqlConnection con2 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
                con2.Open();
                SqlCommand level = new SqlCommand("select accessLevel, INST_ID from Table99 where INST_ID = @INST_ID AND accessLevel = @accessLevel", con2);
                level.Parameters.Add(new SqlParameter("INST_ID", TextBoxINST_ID.Text));
                level.Parameters.Add(new SqlParameter("accessLevel", TextBoxaccessLevel.Text));

                SqlDataReader reader = level.ExecuteReader();
                DataTable dt1 = new DataTable();
                dt1.Load(reader);

                foreach (DataRow dr1 in dt1.Rows)
                {
                    int returnedLevel = Convert.ToInt32(dr1[0].ToString());
                    int accessLevel = Convert.ToInt32(dr1[1].ToString());
                    Session["accessLevel"] = accessLevel;

                    if (returnedLevel == 1)
                    {
                        Response.Redirect("FormAPublic.aspx");
                    }
                    else if (returnedLevel == 2)
                    {
                        Response.Redirect("FormCPrivateNon.aspx");
                    }
                    else if (returnedLevel == 3)
                    {
                        Response.Redirect("FormDPrivateFor.aspx");
                    }
                    else if (returnedLevel == 11)
                    {
                        Response.Redirect("FormAPublicL.aspx");
                    }
                    else if (returnedLevel == 21)
                    {
                        Response.Redirect("FormCPrivateNonL.aspx");
                    }
                    else if (returnedLevel == 31)
                    {
                        Response.Redirect("FormDPrivateForL.aspx");
                    }
                    else if (returnedLevel == 7)
                    {
                        Response.Redirect("CEOPage.aspx");
                    }
                }
                con2.Close();
Posted
Updated 13-Jan-14 14:42pm
v2
Comments
njammy 13-Jan-14 20:02pm    
Please use the edit function and re-type your question as it is unclear.
What is the problem your having? Is the redirect not working from the javascript alert OK/Cancel button? Is that the issue? Please explain clearly. Your question is more a requirement not a issue.
Computer Wiz99 13-Jan-14 20:38pm    
Sorry about that. Updated question.
njammy 13-Jan-14 21:09pm    
Still unclear. Share the part of code which isn't working...or explain.
Computer Wiz99 13-Jan-14 22:17pm    
Ok. When the alert displays after the user clicks submit, the alert message has two buttons. An ok button and a cancel button. When the user clicks the cancel button it should redirect to a page. Right now it redirects to the Gradrate page. I have a code to redirect. How can I get my code to redirect when the user clicks on the cancel button in the alert message?

Quote:
C#
Page.ClientScript.RegisterStartupScript(this.GetType(), "Alert", "if(confirm('You Have Successfully Submitted the Cohort! Do you have Additional Cohorts Please Click Yes to Enter another Cohorts? If you are finished entering Cohorts, Please click No.') == false){ window.location();}else{window.location.href='Gradrate.aspx';}", true);
Currently it is showing issue in Console Tab of Developer Tool, when you click on "Cancel".
And that is because of the Code I have underlined.

You should do like...
C#
Page.ClientScript.RegisterStartupScript(this.GetType(), "Alert", "if(confirm('You Have Successfully Submitted the Cohort! Do you have Additional Cohorts Please Click Yes to Enter another Cohorts? If you are finished entering Cohorts, Please click No.') == false){ window.location.href='someOtherPage.aspx';}else{window.location.href='Gradrate.aspx';}", true);

So, use window.location.href and specify the page as you are doing in the "else" part for "OK" Button.
 
Share this answer
 
Comments
njammy 14-Jan-14 4:13am    
This looks like a good solution.
Thanks a lot onenomi... :)
Computer Wiz99 14-Jan-14 4:34am    
Tadit Dash, Thanks for the edited code I will use it but I think I am not being clear enough. What I want is for the alert message to run the code I have for redirect when the user clicks cancel. Instead of the alert having Gradrate.aspx in it, what can I put there to run the redirect code I have in place?
Which redirect code you are referring to? There is no other code inside that "if clause".
Computer Wiz99 26-Mar-14 14:18pm    
The redirect code I am talking about is the one with the different returned levels.
Oh. Well your code implies that the redirect if statements will be used only if page is not valid. You should move your page redirect code to another method like

public string GetRedirect()
{
string redirect = "";
SqlConnection con2 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
                con2.Open();
                SqlCommand level = new SqlCommand("select accessLevel, INST_ID from Table99 where INST_ID = @INST_ID AND accessLevel = @accessLevel", con2);
                level.Parameters.Add(new SqlParameter("INST_ID", TextBoxINST_ID.Text));
                level.Parameters.Add(new SqlParameter("accessLevel", TextBoxaccessLevel.Text));
 
                SqlDataReader reader = level.ExecuteReader();
                DataTable dt1 = new DataTable();
                dt1.Load(reader);
 
                foreach (DataRow dr1 in dt1.Rows)
                {
                    int returnedLevel = Convert.ToInt32(dr1[0].ToString());
                    int accessLevel = Convert.ToInt32(dr1[1].ToString());
                    Session["accessLevel"] = accessLevel;
 
                    if (returnedLevel == 1)
                    {
                        redirect = "FormAPublic.aspx";
                    }
                    else if (returnedLevel == 2)
                    {
                        redirect = "FormCPrivateNon.aspx";
                    }
                    else if (returnedLevel == 3)
                    {
                        redirect = "FormDPrivateFor.aspx";
                    }
                    else if (returnedLevel == 11)
                    {
                        redirect = "FormAPublicL.aspx";
                    }
                    else if (returnedLevel == 21)
                    {
                        redirect = "FormCPrivateNonL.aspx";
                    }
                    else if (returnedLevel == 31)
                    {
                        redirect = "FormDPrivateForL.aspx";
                    }
                    else if (returnedLevel == 7)
                    {
                        redirect = "CEOPage.aspx";
                    }
                }
                con2.Close();
return redirect; 
}


Then in your javascript code bit:
Page.ClientScript.RegisterStartupScript(this.GetType(), "Alert", "if(confirm('You Have Successfully Submitted the Cohort! Do you have Additional Cohorts Please Click Yes to Enter another Cohorts? If you are finished entering Cohorts, Please click No.') == false){ window.location.href=" + GetRedirect() + /*<-Sorry this should be in string concat format*/";}else{window.location.href='Gradrate.aspx';}", true);


By the way, your foraech loop doesn't need to be a loop. I'm assuming, you tell otherwise, that dt1.Rows.Count is 1?
 
Share this answer
 
v2

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