I am doing online Exam application using asp.net in this i have to disable the titlebar so that the user has no option to exit with in the time period.So please help with this one
Its not good practice to force user to stay on the page if they don't wish to, but you can have some work around if you want to confirm the close event before they leave the browser tab
function internalHandler(e) {
return "Please don't leave the page you can be fail in exams!";
}
if (window.addEventListener) {
window.addEventListener('beforeunload', internalHandler, true);
} else if (window.attachEvent) {
window.attachEvent('onbeforeunload', internalHandler);
}
If you prevent user to close it any way you don't have control over ALT + F4 or closes it from Task Manager
you can do it using javascript like this
var message = "You have not completed exam. Are you sure you want to leave?";
window.onbeforeunload = function(event) {
var e = e || window.event;
if (e) {
e.returnValue = message;
}
return message;
};
and you can unload it when user finish the exam
window.onbeforeunload = null;
or you can create your own browser application using c# windows forms. where you can set this custom option without having close button. You load your web application form in windows forms application easily.
onbeforeunload & onunload will help you out. You can't disable but you can show user an alert.
var showMsgTimer;
window.onbeforeunload = function(evt) {
var message = 'Don't Discard';
showMsgTimer = window.setTimeout(showMessage, 500);
evt = evt || window.evt;
evt.returnValue = message;
return message;
}
window.onunload = function () {
clearTimeout(showMsgTimer);
}
function showMessage() {
alert("You're Right!");
}
If this is not the one you expect. Then please try https://eureka.ykyuen.info/2011/02/22/jquery-javascript-capture-the-browser-or-tab-closed-event/
Related
I am working on basic ASP.net website and i want to execute server side function when user try to go away from page. For this i am using onbeforeunload event of window. I have check box on my page and when user checked this check box, i am executing sverside "checkedchange event". Issue is whenever user click on this check box my web method is also get called, which should not get called as only postback is happen, user is not leaving my page. can any one suggest me to avoid web method call when postback happen.
I wnat to execute web method only in following scenarios:
1) When user closes the browser.
2) On click of “Find more matches” button, when user landed on search results page with no school listed.
3) when user changes the url from browser's address bar
Code on aspx page:
function GetMessage() {
var urlstring = document.URL;
{
PageMethods.Message( document.URL);
}
}
</script>
Code on aspx.cs page
[System.Web.Services.WebMethod]
public static void Message()
{
string x="a";
}
This link should hold the answer you are looking for: How to capture the browser window close event?
I think the following code from that link is what you are looking for.
var inFormOrLink;
$('a').live('click', function() { inFormOrLink = true; });
$('form').bind('submit', function() { inFormOrLink = true; });
$(window).bind('beforeunload', function(eventObject) {
var returnValue = undefined;
if (! inFormOrLink) {
//TODO: Execute some code before unload here
//returnValue = "Message to display before the user leaves the page.";
}
eventObject.returnValue = returnValue;
return returnValue;
});
i m opening an Webpage(Clock.aspx) from window.showModalDialog of javascript.In the Clock.aspx i have a button and i want that when the user click on that button the Clock.aspx page will be closed. I dont want to use onClientClick() method of javascript as some server side database insertion is going on and after the insertion i want to close this page.
The Code behind of the button is as follows:-
protected void btnStop_Click(object sender, EventArgs e)
{
_nonProduction = new NonProduction();
if (Session["LastNonProdTimeID"] == null)
{
}
else
{
int NonProdTimeEntryID = Convert.ToInt32(Session["LastNonProdTimeID"]);
//Updating the TimeSpent
isTimeSpentUpdated = _nonProduction.UpdateTimeSpentInDB(NonProdTimeEntryID);
if (isTimeSpentUpdated == true)
{
string timespent = Convert.ToString(_nonProduction.GetTimeSpent(NonProdTimeEntryID));
string msg = "Total time consumed in " +HiddenTaskname.Value.ToString()+": " + timespent.ToString() + " Minutes";
ClientScript.RegisterStartupScript(typeof(Page), "closePage", "<script type='text/JavaScript'>window.close();</script>");
//ShowPopUpMsg(msg);
}
else
{
}
}
}
Here when i m clicking on the Button one more (Clock.aspx)pop up is appearing and window is not closing. Please help me that how i close the ShowModalDialog from server side code. I m using Script manager in my page also.
Thanks in Advance.
I had added <base target="_self">to the head section of the clock.aspx page and then it works fine for me.
For mine, I have a function in normal javascript that closes the page in the aspx.
In the code behind, if the update is successful, it calls that function.
// this function is to be called by the popup windows to refresh the opener using specific office code, and close self
function allDoneOffice(office)
{
var opener = self.opener;
if (opener.doRefresh) opener.doRefreshWithOfficeCode(office);
window.open('','_self',''); // IE warning hack
self.close();
}
// update the record
bool b = report.SaveModifiedToDB();
if (b)
{
// don't close the page if nothing was updated
ClientScript.RegisterStartupScript(this.GetType(), "load", "<script type=\"text/javascript\">\n" +
"allDoneOffice('" + report.OfficeCode + "');" + "<" + "/script>");
}
else
{
lblResults.Text += " Unable to save modified report to the database.";
}
Use this code below should work in IE
Response.Write("<script language='javascript'> { self.close() }</script>");
Introduction of problem:
I have two forms Home.cs and Login.cs. I have ToolStripMenuItems in Home.cs, Admin will log-in from the Login.cs form. On form_load (Home.cs) event I had made two menu items disabled.
addToolStripMenuItem.Enabled = false;
editToolStripMenuItem.Enabled = false;
After successful login I want to enable those menu items in Home.cs. But is not able to figure out how to do that in C#. I thought I can do something like this:
private Home hm = null;
and then in authentication event I can do....
hm.addToolStripMenuItem.Enabled = true;
Problem:
But this is not working, and this is not the right way to handle this situation. Plz help......
You could simply call the Login.cs from the Load event of the Home.cs form/class , just like that:
public void Home_Load(...params...)
{
Login log = new Login();
if(log.ShowDialog() == DialogResult.Ok)
{
// enable the menu here
}
else
{
// let the menu disabled or exit the application here
}
}
Hello
Those who have used WatiN likely also used DialogHandlers.
Well can someone teach me how can i assign a DialogHandler that will handle any Alert Box window.alert(), of a specific IE instance under WatiN control .
The DialogHandler only has to click in the OK button for very alert dialog box, in that case i think we need an AlertDialogHandler that basically only has to click the OK button.
AlertDialogHandler.OKButton.Click()
I've search the web and found a few examples.. But they work for a small period of time or the time you specify, i need one that will work forever, until i choose to stop it by clicking a button.
This as been bugging my head for hours, any help is appreciated. Thanks.
Note: Sometimes the alert dialog window has two buttons. Thats why i really need to click the OK button, not just Close the dialog window.
Create class:
public class OKDialogHandler : BaseDialogHandler
{
public override bool HandleDialog(Window window)
{
var button = GetOKButton(window);
if (button != null)
{
button.Click();
return true;
}
else
{
return false;
}
}
public override bool CanHandleDialog(Window window)
{
return GetOKButton(window) != null;
}
private WinButton GetOKButton(Window window)
{
var windowButton = new WindowsEnumerator().GetChildWindows(window.Hwnd, w => w.ClassName == "Button" && new WinButton(w.Hwnd).Title == "OK").FirstOrDefault();
if (windowButton == null)
return null;
else
return new WinButton(windowButton.Hwnd);
}
}
After creating instance of IE, attach dialog handler to it:
ie.AddDialogHandler(new OKDialogHandler());
This dialog handler will handle all windows, that contains a button with "OK" caption, by clicking on that button.
I'm a newbie programmer writting a program in MonoDevelop in C# and have a porblem with my gtk MessageDialogs.
The close button on the window boarders of my GTK Message dialogues require a double click to actually close them. The close button on the dialogue its self works fine. Could someone please tell me how I can fix this below is the code:
if (fchDestination.CurrentFolder == fchTarget.CurrentFolder) {
MessageDialog msdSame = new MessageDialog(this, DialogFlags.Modal, MessageType.Error, ButtonsType.Close, "Destination directory cannot be the same as the target directory");
msdSame.Title="Error";
if ((ResponseType) msdSame.Run() == ResponseType.Close) {
msdSame.Destroy();
}
return;
}
if (fchTarget.CurrentFolder.StartsWith(fchDestination.CurrentFolder)) {
MessageDialog msdContains = new MessageDialog(this, DialogFlags.Modal, MessageType.Error, ButtonsType.Close, "error");
msdContains.Title="Error";
if ((ResponseType) msdContains.Run() == ResponseType.Close) {
msdContains.Destroy();
}
return;
}
The response value given when you click on a dialog's "close window" button is not CLOSE, but DELETE_EVENT. That's why the destroy method is never called and the dialog lingers. The second time you close it (out of the context of the run method), the dialog is destroyed normally.
In short, you also need to check for ResponseType.DeleteEvent.
Update:
In code:
MessageDialog msdSame = ...
...
ResponseType response = (ResponseType) msdSame.Run();
if (response == ResponseType.Close || response == ResponseType.DeleteEvent) {
msdSame.Destroy();
}
Or, as ptomato mentions, you don't need to check the response, considering the user only has one choice: "close".
MessageDialog msdSame = ...
...
msdSame.Run();
msdSame.Destroy();
May be both the conditions get satisfied and hence you get two msgbox and it appears you've to give a double click to close it
A sample class could be:
using System;
using Gtk;
namespace Visitors.Clases.MessageBox
{
public static class MessageBox
{
public static Gtk.ResponseType Show(Gtk.Window window, Gtk.DialogFlags dialogflags, MessageType msgType,ButtonsType btnType,string Message,String caption)
{
MessageDialog md = new MessageDialog (window,dialogflags,msgType,btnType, Message);
md.Title = caption;
ResponseType tp = (Gtk.ResponseType)md.Run();
md.Destroy();
return tp;
}
}
}
The class in use:
ResponseType result = MessageBox.Show(this,DialogFlags.Modal,MessageType.Error,ButtonsType.Ok,Error,"ERROR");
if (result == Gtk.ResponseType.Yes)
{
MessageBox.Show (this, DialogFlags.Modal, MessageType.Other,ButtonsType.Ok, "YES", "EJEMPLO");
}
else
{
MessageBox.Show (this, DialogFlags.Modal, MessageType.Other,ButtonsType.Ok, "NO", "EJEMPLO");
}