Navigate to a new page and display an alert box - c#

I am developing an application by using ASP.Net WebForm. Once user click a button, application will navigate to a new page and prompt out a dialog box "Welcome to JackiesGame"
However, I able to navigate to new page but the alert dialog box does not display.
The following is my sample code
void cmdCancel_Click(object sender, EventArgs e)
{
HttpContext.Current.Response.Redirect(Globals.NavigateURL(TabId), true);
Page page2 = HttpContext.Current.CurrentHandler as Page;
ScriptManager.RegisterStartupScript(page2, page2.GetType(), "alertMessage", "alert('Insert Successfully')", true);
}

Add the following in page 2. On the page load it will register only for the first time the page loads the script.
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
var reg = Request["Welcome"]
if(reg != null && reg.ToString() == "yes"){
ScriptManager.RegisterStartupScript(this, this.GetType(), "alertMessage", "alert('Insert Successfully')", true);
}
}
}
All code after the redirect is getting ignored since it has to redirect to a new page. So the code never gets triggered.
EDIT
Added a example of how it can look further
void cmdCancel_Click(object sender, EventArgs e)
{
string myUrl = Globals.NavigateURL(TabId)+"?Welcome=yes";
HttpContext.Current.Response.Redirect(myUrl, true);
}

Related

asp.net Load Child page only from parent page

I have two pages MainPage.aspx and ChildPage.aspx. From main page when i click a button i redirect to ChildPage.
If i give the address of ChildPage directly on browser, i do not want to load it directly instead i want to redirect to MainPage.
the ChildPage must be loaded only if it is loaded from the MainPage.
How do I find from where the ChildPage.aspx is loaded. how to find the parent page of it or from where it is loaded.
can we try something in the below code
if (!IsPostBack)
{
if (finding_source)
{
Response.Redirect("MainPage.aspx");
}
}
You can use Request.UrlReferrer.AbsolutePath to see the previous page.
if (!IsPostBack)
{
if (Request.UrlReferrer != null && Request.UrlReferrer.AbsolutePath == "/MainPage")
{
//do what you want
}else{
Response.Redirect("~/MainPage.aspx");
}
}
TIP But be careful with using it with postbacks since it will change the value of Request.UrlReferrer to the current page during a postback.
Even though your question is not clear , here i am trying to give my solution.
MAINPAGE.ASPX BUtton Click
protected void lnkRegister_Click(object sender, EventArgs e)
{
Session["MainPage"] = "true";//Encrypt it if u wish
Response.Redirect("childpage.aspx");
}
CHILDPAGE.ASPX PAGE LOAD
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (!string.IsNullOrEmpty(Session["MainPage"] as string) && Session["MainPage"].Tostring()=="true")
{
//proceed
}
else
{
Response.Redirect("mainpage.aspx");
}
}
GLOBAL.ASAX
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
Session.RemoveAll();
Session.Clear();
Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
Response.Cache.SetValidUntilExpires(false);
Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();
}
it is little difficult to control browser shutdown or close...but you can workaround global asax file when your application shutsdown.

ASP .net Always ask for log in on load

I'm using ASP.net with C#, I have a form with master page and I need to show the log in everytime someone enters the page and logout when the information in the form is saved in database.
To save I use this code
<asp:Button ID="botonAcepto"
Text="Guardar"
runat="server"
ValidationGroup="validaARCO"
OnClick="btnUpload_Click" />
btnUpload_Click uses this code to redirect to logout
string scriptText = "alert('Datos guardados exitosamente.'); location.href='/folder/folder/logout.aspx';";
ScriptManager.RegisterStartupScript(this, this.GetType(), "alertMessage", scriptText, true);
But, when I try to force the log in page the button doesn't work.
I tried this 3 ways to force login on load. These page loads are not in the same document at the same time, they're the 3 set ups I tried on my Default.aspx.cs to force the login at load.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
HttpCookie cookie1 = new HttpCookie(FormsAuthentication.FormsCookieName, "");
cookie1.Expires = DateTime.Now.AddYears(-1);
Response.Cookies.Add(cookie1);
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FormsAuthentication.SignOut();
Response.Cookies[FormsAuthentication.FormsCookieName].Expires = DateTime.Now.AddYears(-1);
HttpContext.Current.User = new GenericPrincipal(new GenericIdentity(string.Empty), null);
MaxUsersManager.RemoveSessionCacheItem(Context);
Session.Clear();
Session.Abandon();
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FormsAuthentication.SignOut();
FormsAuthentication.RedirectToLoginPage();
}
}
The 3 examples make the button useless, and the form goes to the login page and doesn't save the data.
How can I solve this? Is there any other way to always force the user to log in to enter the form and log out when the data is saved?
Thanks.

How to prevent back button after LOGIN in asp.net?

I have 2 pages home.aspx and admin.aspx
After successfully logging into admin.aspx when i click back button of browser, it does redirect to home.aspx but that i don't want.
I am checking session variable persistence on home.aspx but for some reason its not working!!
Here's the code
home.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (Session["aname"] != null)//should work as session will not be null!
{
Response.Redirect("admin.aspx");
}
} //.....some code..after this
if (dt.Rows.Count != 0)
{
Session["aname"] = TextBox11.Text;
Response.Redirect("admin.aspx");
}
admin.aspx.cs code
protected void Page_Load(object sender, EventArgs e)
{
if (Session["aname"] == null)
{
Response.Redirect("home.aspx");
}
} //some code after this..
protected void logoutbutton_Click(object sender, EventArgs e)
{
Session["aname"] = null;
Session.Abandon();
Session.Clear();
Response.Redirect("home.aspx");
}
NOTE:(things working fine)
1.login working sucessfully
2.logout working sucessfully
3.back button is disabled once loggedout(not going on admin.aspx)
Issue:
When logged in i.e. on admin.aspx ,on clicking back button it redirects to home.aspx which i don't want. i expect it to remain on same admin.aspx
ok.. finally trying all your solutions..this code worked on adding in my masterpage (in head tags)
<script type = "text/javascript" >
function preventBack(){window.history.forward();}
setTimeout("preventBack()", 0);
window.onunload=function(){null};
</script>
full details on this page
You can push the Window History forward to prevent the back button. This has work for me in most cases. Include this JavaScript on your Admin.aspx page.
$(function() {
window.history.forward();
});

How to destroy query string after redirecting from one page to another page on page load event?

I am having 2 pages name as :
Abc.aspx
pqr.aspx
Now on page load of Abc.aspx i am doing this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (!string.IsNullOrEmpty(Request.QueryString["Alert"]))
{
if (Request.QueryString["Alert"] == "y")
{
//Here on redirection from Pqr.aspx i will display Javascript alert that your "Your data save"
}
}
else
{
//Dont do anything
}
}
}
Now from pqr.aspx page i am redirecting to Abc.aspx and passing query string on button click:
protected void Save_Click(object sender, EventArgs e)
{
//saving my data to database.
Response.Redirect("~/Abc.aspx?Alert=yes");
}
But what is happening is if anybody enters url like this in browser then still this alert is coming:
http://localhost:19078/Abc.aspx?Alert=yes
Then still this javascript alert box comes.
What i want is after redirecting from my Pqr.aspx page only this alert should come.
How to do this??
In Asp.net there is an object named Request.UrlReferrer.With this property you can get the previous page from which you come to the current page
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (!string.IsNullOrEmpty(Request.QueryString["Alert"]))
{
if (Request.QueryString["Alert"] == "y" && Request.UrlReferrer != null && Request.UrlReferrer.LocalPath == "/pqr.aspx") // if the root is same
{
//Here on redirection from Pqr.aspx i will display Javascript alert that your "Your data save"
}
else
{
//Dont do anything
}
}
}
}

How to refresh the page? (Click button close RadWindow)

I have 2 pages (Home and Сategory)
Load page Home on there's button. Click button run panel RadWindow (NavigateUrl: Сategory).
protected void ShowWindow()
{
string script = "function f(){$find(\"" + RadWindow_editor.ClientID + "\").show(); Sys.Application.remove_load(f);}Sys.Application.add_load(f);";
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "key", script, true);
}
protected void RadButtonEdit_Click(object sender, EventArgs e)
{
ShowWindow();
}
Load RadWindow NavigateUrl - Сategory on there's button. Click button close RadWindow.
protected void RadButtonEdit_Click(object sender, EventArgs e)
{
ClientScript.RegisterStartupScript(Page.GetType(), "mykey", "Close();", true);
}
function GetRadWindow() {
var oWindow = null;
if (window.radWindow)
oWindow = window.radWindow;
else if (window.frameElement.radWindow)
oWindow = window.frameElement.radWindow;
return oWindow;
}
function Close() {
var oWindow = GetRadWindow();
oWindow.argument = null;
oWindow.close();
return false;
}
How to refresh the page Home? (Click button close RadWindow)
Thank you!
Here's how: http://www.telerik.com/community/forums/aspnet-ajax/window/how-to-refresh-the-page-click-button-close-radwindow.aspx#2894238.
If your page does not refresh, then the problem is in the page. Having a has in the URL can cause this problem. Make sure you change the URL so you have a fresh GET request.
There are client-side events like OnClientBeforeClose and OnClientClose.
http://demos.telerik.com/aspnet-ajax/window/examples/clientsideevents/defaultcs.aspx
and check this link, it gives you an idea.
How to close the radwindow on serverside and refresh the parent page
You should also look at using a RadAjaxManager in the parent page to send the window events to the parent as shown in the the following demo: Grid and Window. It focuses on refreshing a grid, not a page in your instance, but the concept of sending events to the parent page remains the same.

Categories