how to refresh the current window using c# in asp.net - c#

if (TextBox1.Text == row["UserName"].ToString()
&& TextBox2.Text == row["Password"].ToString())
{
//here i have to reload the page
Button1.Attributes.Add("onclick", "alert('Login SucessFull')");
break;
}

Maybe this is more what you want? It will show an alert and then redirect you to another page.
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "LoggedInScript", "alert('Login SucessFull'); window.location = 'MyLoggedInPage.aspx';", true);

When you say you need to "reload the page" do you mean redirect to another page becuase the user is now logged in? If you you use:
Response.Redirect("MyLoggedInPage.aspx")

Related

ScriptManager.RegisterStartupScript doesn't work on page redirection

I've got this block of code, which works fine:
if (ddlAuditComplete.SelectedItem.Value == "3")
{
vState = 3;
BindAccordions(Convert.ToInt32(Session["PlanID"]), Convert.ToInt32(Session["AuditID"]));
if (vExit == 2)
{
ScriptManager.RegisterStartupScript(btnAuditComplete, typeof(Button), "Data Entry", "alert('At least one plan has not been closed properly')", true);
return;
}
}
Now, what I need to do is throw that popup and then redirect them to another page.
I tried this and it won't work:
if (ddlAuditComplete.SelectedItem.Value == "3")
{
vState = 3;
BindAccordions(Convert.ToInt32(Session["PlanID"]), Convert.ToInt32(Session["AuditID"]));
if (vExit == 2)
{
ScriptManager.RegisterStartupScript(btnAuditComplete, typeof(Button), "Data Entry", "alert('At least one plan has not been closed properly')", true);
String SUrl = "frmAuditSearch.aspx";
Server.Transfer(SUrl, true);
return;
}
}
The above code will throw the user to the other page, but the popup never shows.
Can anyone tell me how to do both? Ideally, the popup will show up, once the user clicks OK it will re-direct them to the other page. But I'll also take it if the user gets re-directed first and then the popup shows up.
Try this first shows message and then redirect to next page.
ScriptManager.RegisterStartupScript(btnAuditComplete, typeof(Button), "Data Entry",
"alert('At least one plan has not been closed properly');window.location ='"+ SUrl +"';", true);
And remove:
Server.Transfer(SUrl, true);
return;

Stay on the current page after refresh using iFrame asp.net

I am using 'iFrame' on the website, and right now, whenever I directed to the another page (says Register Page) and I click F5 (Refresh button on the browser), it will go to the Default.aspx and not Register Page. I notice that while I directed to the Register Page or another Page, the url stays on: www.something.com/Default.aspx, but it should be www.something.com/Register.aspx. I think the cause for it cannot go back to the current page is because of the URL linked to the Default.aspx.
ID for Main Frame is MainFrame and here is the code:
if (Session["AFFM_RoleCode"] != null && Session["AFFM_RoleCode"].ToString() == "0")
{
// If the user is not logged in
if (Session["IsAgent"] != null && Session["IsAgent"].ToString() == "1")
{
mainFrame.Attributes["src"] = "BlankPage.aspx";
}
else
{
mainFrame.Attributes["src"] = "Main.aspx";
}
}
How do I can solve this problem?
Your answer much appreciated!
Thanks
This is a quick patch you can do to this, add a session to your register page when it load, such as
session["Load"] = "register";
and when the page is refreshed, if you find register in the load session, redirect it to the register
if (session["load"] == "register"){
session["load"] = "";
//you redirect code
}

Countdown label on asp.net redirect page

After a user clicks the log out button I have it take them to a redirection page which displays a message and says " Logging out and Redirecting in ? seconds." I am using
Response.AddHeader("REFRESH", "3;URL=Login.aspx");
is there a way to display how many seconds are left until they are redirected in label?
In your redirect page you need to use JavaScript to handle it.
This sample may help you: http://javascriptsource.com/navigation/countdown-redirect.html
Following up on my comment, you could accomplish your solution via the following code:
<script type="text/javascript">
var timeLeft = 3;
function decrementCounter() {
if (timeLeft > 0) {
document.all('countDown').innerHTML = "Redirecting in " + timeLeft + "...";
timeLeft--;
setTimeout("decrementCounter()", 1000);
}
else {
window.location = "http://www.google.com/"
}
}
</script>
<body>
<form>
<label id="countDown">3</label>
<input type="button" value="Display alert box in 3 seconds" onclick="decrementCounter()" />
</form>
</body>
For example ,
When you Click the Logout button , You can create a count down javascript by dynamic
protected void OnLogout(object sender, EventArgs e)
{
string url = "~/Login.aspx";
string msg = "Logging out and Redirecting in ? ";
StringBuilder js = new StringBuilder("<script language=\"javascript\">")
.Append("var ts = 3; setInterval(\"redirect()\",1000);")
.Append("function redirect(){ if(ts == 0){")
.Append("window.location.href=\"" + url + "\"; }else{")
.Append("document.body.innerHTML = \"msg \" + (ts--)+\"seconds\";}}")
.Append("</script>");
Response.Write(js.ToString());
}
You'll need to use a client-side technology, I.e. JavaScript. Using server-side will require calls to the server which is not needed for something simple like this.
Because you are using Microsoft aspx, you can create a label on the client side and implement an AJAX by giving your code-behind the Id of the label and changing its value base on the time left.

How to display an alert box on a non clickable link button

I am having HyperLink on my webfrom in such a way that if proper user login and clicks on the link will navigate him to corresponding page. If not i make that as a non clickable field by using this code
if(isAdmin)
{ // check for admin user
Link1.NavigateUrl = "Reports.aspx";
Link1.NavigateUr2 = "Delete.aspx";
}
else
{
Link1.NavigateUrl = "javascript:return void;";
Link1.NavigateUr2 = "javascript:return void;";
}
In the else part i would like to display an alert box saying that you are not an authorized user.
I also write another code for making that link as non selectable as follows
LinkButton1.Attributes["OnClick"] = “return false;”;
So can any one tell how can i write alert message in both the cases
Link1.NavigateUrl = "javascript:alert('hello'); return false;";
OR
Link1.Attributes["onclick"] = "alert('hello'); return false";
In your else part add:
string prompt =
"<script type=\"text/javascript\">alert('You are not an authorized user'); </script>";
ClientScript.RegisterStartupScript(typeof (Page), "alert", prompt);
Regards

Prevent page refresh on pressing F5

I have Created form with Server Side control like button..and also have written event on that..
Now runtime after click on the button i have refreshed the page by pressing F5.Page_load is executing fine but button1_click() event also firing...So how can i stop this event execution in this scenario.Please suggest me
Short Answer: Not possible
Longer Answer: No web site can block the browser's functionality as that would be a serious security concern for the browser.
Welcome to web development, there is no way to stop the page from refreshing and reposting, this is entirely a browser behavior.
There are work arounds though; one simple thing that you can do is after processing your button click, you can redirect the browser back to the page so that a refresh will not include a repost of your button press. Obviously this might require some other state management system (rather than view-state), maybe you can store your state in the Session.
<script type="text/javascript" language="javascript">
function checkKeyCode(evt)// for F5 disable
{
var evt = (evt) ? evt : ((event) ? event : null);
var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
if (event.keyCode == 116)//disable F5
{
evt.keyCode = 0;
return false
}
if (event.keyCode == 123) {
evt.keyCode = 0;
return false
}
if (event.keyCode == 93) {
evt.keyCode = 0;
return false
}
if (event.altKey == true && event.keyCode == 115) //disable alt-F4
{
evt.keyCode = 0;
return false
}
//alert(event.keyCode);
}
document.onkeydown = checkKeyCode;
</script>
Post this script on your master page or where u want to block F5 button.
You could potentially use:
window.onbeforeunload = function ()
{
return false;
}
This will display a prompt asking the user if they want to navigate away and may not be desired. (Plus, I don't know the extent of support)
The poster is using ASP.NET WebForms. In MVC, using the Post-Redirect-Get pattern, this would not be a problem.
But this has been solved for Webforms since 2004 (and even before that - I wrote a simple ticket solution in 2001) with this article: http://msdn.microsoft.com/en-us/library/ms379557.aspx
Basically the author creates a unique ticket associated with each request, and removes the validity of the ticket on first postback. Any accidental (or malicious) subsequent postbacks are ignored. It's wrapped up in a base page so it should be easy to add to any Webforms solution.
Its not really page refresh that you want to block, its page re-POST.
As already said, it's not possible on the client side because the refresh will replay what the browser did on the last action.
But you can trap it on the server side because the server can hold some infos to control the request.
The easiest way is to put a sort of RequestID in a hidden field on Page Load, store it somewhere like in the Session. And control its value on next POST to valid the request then override it to avoid re-POST with the same RequestID.
Maybe there's a better way :)
Your 'problem' is similar to a user double-click horror behavior : ASP.Net double-click problem
Do you have to refresh the page on the click of the button? Could you rewire the button to make an ajax call to submit the data? If so, then you would not need to worry about the page re-submitting on refresh.
The only solution for the F5 refresh problem is Response.Redirect. Please see my post on this topic.
https://stackoverflow.com/questions/12596152/asp-net-f5-browser-refresh-duplicate-record/12596153#12596153
I got some code that doesn't allow you to press F5 and/or Ctrl + R:
<!DOCTYPE html>
<html>
<head>
<title>
Key code test!
</title>
<script type="text/javascript">
var x, ctrlKeyPressed = false;
function keyDown(e){
x = e.charCode || e.keyCode;
document.getElementById("key").innerHTML = x;
if(x == 17) {
ctrlKeyPressed = true;
}
if(ctrlKeyPressed && x == 82) {
e.preventDefault();
} else if(x == 116) {
e.preventDefault();
}
}
function keyUp(e) {
x = e.charCode || e.keyCode;
if(x == 17) {
ctrlKeyPressed = false;
}
}
window.onload = function () {
document.onkeydown = function (e) {
keyDown(e);
};
document.onkeyup = function (e) {
keyUp(e);
};
}
</script>
</head>
<body>
Press a key!
<br/>
Key code: <div style="display:inline;" id="key">Nothing</div>
</body>
</html>

Categories