show popup box before signout - c#

i have a doubt on how to show a popup???`
if (machineID.Count != 0)
{
checkMachineGrpState(machineID);
}
else
{
FormsAuthentication.SignOut();
Session.Abandon();
Response.Redirect("~/Default.aspx");
}
Ok now what im am doing in else statement is signing out the user and sending him back to the log out page....
I need to how him some pop up message that he is being signed out i cant figure out how to do that...
i tried messagebox but it wont work with servver and client side..
I want to use AJAX but dont know how...
any suggestions.... thanks

There are a couple of different ways you can go about this. Here's a simple example.
Your Default.aspx page will need to display the message to the user when they've logged out, so you might want a way to distinguish when you want to show the message. You could add a query string param to your redirect, like:
Response.Redirect("~/Default.aspx?ShowLogout=true");
Now on your Default.aspx page, you have a number of options. You could simply show a hidden control on the page, or write out some Javascript to show an alert box:
if (!String.IsNullOrEmpty(Request.QueryString["ShowLogout"]))
ClientScript.RegisterStartupScript(this.GetType(), "LogoutMsg", "<script>alert('You have been logged out.');</script>");
This will simply write out a script tag that runs when the user views the page. From here, you can make it more elegant by showing the user a better dialog box. For example, you could use jQuery to create a nice looking dialog box, and call the Javascript function to show it rather than calling alert in my example.

Related

Prevent resending form without page.redirect

maybe some of you know the problem, that a form gets resubmitted if pressing refreshing the browser by pressing F5.
I am looking for a way to prevent this, and all I have found is the solution to do a redirect on the same page after submit
Response.Redirect(Request.RawUrl);
This DOES work, but my problem is, that I need to inject javascript into a placeholder after postback, and so this information is lost because of the Redirect.
PlaceHolder p1 = this.NamingContainer.Parent.FindControl("phPostScript") as PlaceHolder;
GenerateJsTag(script,p1);
Response.Redirect(Request.RawUrl);
Is there a way I can prevent a resubmit without Redirect?
Thanks
Is there a way I can prevent a resubmit without Redirect?
Even if there is , you should stick to that approach which is redirect after submit.
Regarding JS after post back , I believe that you want to see those JS output even after refresh after redirect.
What you can do is to redirect to Request.RawUrl+"#someValue" and then in the pageLoad (not within IsPostBack) you can inject again those scripts , knowing that you already showed that to the user via that hashkey ( could be cookie also , or query string , doesn't really matter) - or to show a message like ("Message was already accepted"...) or something like that.
Try adding following JavaScript code to your aspx page.
Also, you should not be trying to disable F5 since there are many users out there who may need to use F5 during browsing with your website.
//bind keydown event to method that disables F5
$(document).on("keydown", PreventRefresh);
function PreventRefresh(e) {
var code = e.which || e.keyCode;
if (code === 116) {
e.preventDefault();
}
}

Get alert content when alert is shown

I have an HTML page with this script:
<Script>
function Run()
{
alert("The Content Here");
return true;
}
</script>
<button onclick="Run();">I Want It Now</button>
Lets say that I opened this page with firefox or Chrome. And lets say that I clicked on the button "I Want It Now" and the page shows me the alert:
The Content Here
How can I insert the alert content into a string in my VB.NET project? I know that I can use the alert window handle to get the label handle and then extract (grab) the text of the alert, but I don't think that this is the best way to do it. Is there another way to pass (or get) information from the page (not from webbrowser control or by using webClient.DownloadString) into my VB.NET (or C#) project?
i don't this it will easy to use js without the webbrowser control by the way if you changed your mind and want to use a web browser control , you can do it like this :)
WebBrowser1.Document.GetElementById("NAME").InvokeMember("click");

Prevent or rollback the postback on page_load

First of all, I can't use Async plugins like Jquery, Ajax, UpdatePanel, etc.
I have an ASP.NET 4 WEBFORM, In the master page I verify if the Session is lost, in that case a show a modal to get the username/password and log-in the user again.
But I've two problems, the first one is that for example if I press a button and I lost the session, the webpage show me the modal but the code behind of the clickevent still execute it and i want to prevent it (I dont want to verify the state of the session in every event handler of the APP).
The second one is that after Im success on log-in the user, i want to finish or continue the event that the user tries before he lost the session.
I other words my problem is that I need to prevent the postback in the middle of it, and then do it again. :/
Something like this:
Page_Load(...)
{
If(Session["User"] == null)
{
PostBack.Cancel(); //Like de e.PreventDefault() of Javascript.
return;
}
.
.
}
AND
LoginOnSessionLost()
{
...
//After Login Success
Postback.Continue();
}
Any suggestions? Thanks

asp.net: how do i prevent users from posting the same data multiple times

i have a little asp.net web application.
it is a front end to a sql-server-2008 database.
after they fill out all the data, they will press submit and the data will be sent to the database.
after this point if the user refreshes the page, the data is posted again!!! how do i disable this from happening?
Send a Location header back to the user that redirects the browser to another page: refresh will then reload that other page rather than resubmit the old form.
This is caused by the last request being a POST request to the page, what do you need to do is a redirect so the last request becomes a GET.
After you have handled the post data you can just do a redirect to the same page with:
Response.Redirect("~/ThePage.aspx");
This will prevent you from presenting a message to the user straight from the code behind, if you want to present a success message using this method you will need to add a querystring or something similar:
Response.Redirect("~/ThePage.aspx?result=success");
And then check on the page bind if the querystring to present a success message is set, such a check could look something like this:
if (Request.QueryString["result"] != null && Request.QueryString["result"].ToString().ToLower() == "success")
{
//Show success message
}
Another solution which probably is superior but might require some more work is to wrap the form in a updatepanel, you can read more about it here: http://ajax.net-tutorials.com/controls/updatepanel-control/
An updatepanel will make the form submit with AJAX instead of full postback.
You need to follow the Post/Redirect/Get pattern which is explained on WikiPedia and alluded to by Femi. In your code after you've done your processing do a Response.Redirect to the desired page.
See this article about the PRG pattern: http://en.wikipedia.org/wiki/Post/Redirect/Get
In short, after the user POSTs (submits) data to your server, you issue a Response.Redirect to have the users browser GET a page. This way, if the user presses the reload button, it is the GET request that is repeated.

Message box in asp.net website

using (DataServer server = new DataServer())
{
server.ExecuteNonQuery(CommandType.Text, updateuser, param);
}
MessageBox.Show("User succesfully updated");
I have a update button when The fields are updated I show a message box, but the problem is the message box is behind the browser. I have to minimize the browser to close the message box, at the same time I am able to navigate in the website without closing the message box, I want to blur out the remaing area in the website when the message box is displayed. How can I do this in my website.
In this case you would either want to throw up an alert with JavaScript, or use a dialog of some sort.
To throw up an alert, you can do this:
Page.ClientScript.RegisterStartupScript(GetType(), "UserDialogScript", "alert(\"User successfully updated\");", true);
As for using a dialog, I would suggest the ModalPopupExtender or the jQuery UI Dialog.
ModalPopupExtender:
http://www.asp.net/ajaxlibrary/AjaxControlToolkitSampleSite/modalpopup/modalpopup.aspx
jQuery UI Dialog:
http://jqueryui.com/demos/dialog/
Don't do it this way. Instead, emit client side JavaScript to pop up an alert dialog, or add a div that a user can click to make it go away. You can also use a more proper UX convention of adding a message div that simply states "record updated" or similar, in a very specific color, so the user knows what to look for.
The idea of popping up a message box sounds tempting, but stick this out on a server and hit it from a browser on a separate box and you will quickly find out why this is an extremely bad idea.

Categories