<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>KaushaL.NET</title><link>http://weblogs.asp.net/kaushal/default.aspx</link><description>{ a .net developer&amp;#39;s blog; }</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>Start/Stop Window Service from ASP.NET page</title><link>http://weblogs.asp.net/kaushal/archive/2011/02/28/start-stop-window-service-from-asp-net-page.aspx</link><pubDate>Mon, 28 Feb 2011 16:59:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7714082</guid><dc:creator>kaushalparik27</dc:creator><slash:comments>14</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/kaushal/rsscomments.aspx?PostID=7714082</wfw:commentRss><comments>http://weblogs.asp.net/kaushal/archive/2011/02/28/start-stop-window-service-from-asp-net-page.aspx#comments</comments><description>&lt;p&gt;&lt;b&gt;&lt;i&gt;Update: The article has been selected as "Article of the day" on 30th April 2011 at official asp.net (&lt;a href="http://www.asp.net/Community/Articles/Default.aspx" target="_blank"&gt;http://www.asp.net&lt;/a&gt;) site&lt;/i&gt;&lt;/b&gt; &lt;br&gt;&lt;/p&gt;&lt;p&gt;Last week, I needed to complete one task on which I am going to blog about in this entry. The task is "Create a control panel like webpage to control (Start/Stop) Window Services which are part of my solution installed on computer where the main application is hosted". &lt;br&gt;&lt;br&gt;Here are the important points to accomplish:&lt;br&gt;[1] You need to add &lt;a href="http://msdn.microsoft.com/en-us/library/system.serviceprocess.aspx" target="_blank" mce_href="http://msdn.microsoft.com/en-us/library/system.serviceprocess.aspx"&gt;System.ServiceProcess&lt;/a&gt; reference in your application. This namespace holds &lt;a href="http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicecontroller.aspx" target="_blank" mce_href="http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicecontroller.aspx"&gt;ServiceController Class&lt;/a&gt; to access the window service.&lt;br&gt;&lt;br&gt;[2] You need to check the status of the window services before you explicitly start or stop it.&lt;br&gt;&lt;br&gt;[3] By default, IIS application runs under ASP.NET account which doesn't have access rights permission to window service. So, Very Important part of the solution is: &lt;a href="http://support.microsoft.com/kb/306158" target="_blank" mce_href="http://support.microsoft.com/kb/306158"&gt;Impersonation&lt;/a&gt;. You need to impersonate the application/part of the code with the User Credentials which is having proper rights and permission to access the window service. &lt;br&gt;&lt;br&gt;If you try to access window service it will generate "access denied" &lt;a href="http://www.dotnetmonster.com/Uwe/Forum.aspx/asp-net/23826/ASPX-ACCESS-denied-when-start-stop-windows-services" target="_blank" mce_href="http://www.dotnetmonster.com/Uwe/Forum.aspx/asp-net/23826/ASPX-ACCESS-denied-when-start-stop-windows-services"&gt;error&lt;/a&gt;.&lt;br&gt;&lt;br&gt;The alternatives are: You can either impersonate whole application by adding Identity tag in web.cofig as:&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;identity impersonate="true" userName="" password=""/&amp;gt;&lt;br&gt;&lt;br&gt;This tag will be under System.Web section. the "userName" and "password" will be the credentials of the user which is having rights to access the window service. But, this would not be a wise and good solution; because you may not impersonate whole website like this just to have access window service (which is going to be a small part of code).&lt;br&gt;&lt;br&gt;Second alternative is: Only impersonate part of code where you need to access the window service to start or stop it. I opted this one. But, to be fair; I am really unaware of the code part for impersonation. So, I just googled it and injected the code in my solution in a separate class file named as "Impersonate" with required static methods. In Impersonate class; impersonateValidUser() is the method to impersonate a part of code and undoImpersonation() is the method to undo the impersonation. Below is one example:&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;img src="http://weblogs.asp.net/blogs/kaushal/BlogFiles/2-28-2011%2010-33-14%20PM.png" title="Start/Stop Window Service from ASP.NET page" alt="Start/Stop Window Service from ASP.NET page" mce_src="http://weblogs.asp.net/blogs/kaushal/BlogFiles/2-28-2011%2010-33-14%20PM.png" height="226" width="700"&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;You need to provide domain name (which is "." if you are working on your home computer), username and password of appropriate user to impersonate.&lt;br&gt;&lt;br&gt;[4] Here, it is very important to note that: You need to have to store the Access Credentials (username and password) which you are going to user for impersonation; to some secured and encrypted format. I have used Machinekey Encryption to store the value encrypted value inside database.&lt;br&gt;&lt;br&gt;[5] So now; The real part is to start or stop a window service. You are almost done; because ServiceController class has simple Start() and Stop() methods to start or stop a window service. A ServiceController class has parametrized constructor that takes name of the service as parameter.&lt;br&gt;&lt;br&gt;Code to Start the window service:&lt;/p&gt;&lt;p&gt;&lt;img src="http://weblogs.asp.net/blogs/kaushal/BlogFiles/3-1-2011%201-10-44%20AM.png" title="Start/Stop Window Service from ASP.NET page" alt="Start/Stop Window Service from ASP.NET page" mce_src="http://weblogs.asp.net/blogs/kaushal/BlogFiles/3-1-2011%201-10-44%20AM.png"&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;Code to Stop the window service:&lt;/p&gt;&lt;p&gt;&lt;img src="http://weblogs.asp.net/blogs/kaushal/BlogFiles/3-1-2011%201-12-24%20AM.png" title="Start/Stop Window Service from ASP.NET page" alt="Start/Stop Window Service from ASP.NET page" mce_src="http://weblogs.asp.net/blogs/kaushal/BlogFiles/3-1-2011%201-12-24%20AM.png"&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;Isn't that too easy! ServiceController made it easy :) I have attached a working example with this post &lt;a href="http://weblogs.asp.net/blogs/kaushal/BlogFiles/WindowServiceHandler.zip" mce_href="http://weblogs.asp.net/blogs/kaushal/BlogFiles/WindowServiceHandler.zip"&gt;here&lt;/a&gt; to start/stop "SQLBrowser" service where you need to provide proper credentials who have permission to access to window service. &lt;/p&gt;&lt;p&gt;&lt;img src="http://weblogs.asp.net/blogs/kaushal/BlogFiles/2-28-2011%2010-25-53%20PM.png" title="Start/Stop Window Service from ASP.NET page" alt="Start/Stop Window Service from ASP.NET page" mce_src="http://weblogs.asp.net/blogs/kaushal/BlogFiles/2-28-2011%2010-25-53%20PM.png"&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;hope it would helps./. &lt;br&gt;&lt;/p&gt;&lt;p mce_keep="true"&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7714082" width="1" height="1"&gt;</description><enclosure url="http://weblogs.asp.net/blogs/kaushal/BlogFiles/WindowServiceHandler.zip" length="4225" type="application/x-zip-compressed" /><category domain="http://weblogs.asp.net/kaushal/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/kaushal/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/kaushal/archive/tags/kaushalparik27/default.aspx">kaushalparik27</category><category domain="http://weblogs.asp.net/kaushal/archive/tags/kaushalparik/default.aspx">kaushalparik</category><category domain="http://weblogs.asp.net/kaushal/archive/tags/kaushal.net/default.aspx">kaushal.net</category><category domain="http://weblogs.asp.net/kaushal/archive/tags/System.ServiceProcess/default.aspx">System.ServiceProcess</category><category domain="http://weblogs.asp.net/kaushal/archive/tags/ServiceController/default.aspx">ServiceController</category><category domain="http://weblogs.asp.net/kaushal/archive/tags/Window+Services/default.aspx">Window Services</category><category domain="http://weblogs.asp.net/kaushal/archive/tags/start_2F00_stop+window+service+from+asp.net/default.aspx">start/stop window service from asp.net</category></item><item><title>Linq To SQL: Behaviour for table field which is NotNull and having Default value or binding</title><link>http://weblogs.asp.net/kaushal/archive/2011/02/26/linq-to-sql-behaviour-for-table-field-which-is-notnull-and-having-default-value-binding.aspx</link><pubDate>Sat, 26 Feb 2011 17:35:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7713023</guid><dc:creator>kaushalparik27</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/kaushal/rsscomments.aspx?PostID=7713023</wfw:commentRss><comments>http://weblogs.asp.net/kaushal/archive/2011/02/26/linq-to-sql-behaviour-for-table-field-which-is-notnull-and-having-default-value-binding.aspx#comments</comments><description>&lt;p&gt;I found this something interesting while wandering over community which I would like to share. The post is whole about: DBML is not considering the table field's "Default value or Binding" setting which is a NotNull. I mean the field which can not be null but having default value set needs to be set IsDbGenerated = true in DBML file explicitly.&lt;br&gt;&lt;br&gt;Consider this situation: There is a simple tblEmployee table with below structure:&lt;/p&gt;&lt;p&gt;&lt;img src="http://weblogs.asp.net/blogs/kaushal/BlogFiles/2-26-2011%2010-35-51%20PM.png" title="tblEmployee Image" alt="tblEmployee Image" mce_src="http://weblogs.asp.net/blogs/kaushal/BlogFiles/2-26-2011%2010-35-51%20PM.png" height="334" width="651"&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;The fields are simple. EmployeeID is a Primary Key with Identity Specification = True with Identity Seed = 1 to autogenerate numeric value for this field. EmployeeName and their EmailAddress to store in rest of 2 fields. And the last one is "DateAdded" with DateTime datatype which doesn't allow NULL but having Default Value/Binding with "GetDate()". That means if we don't pass any value to this field then SQL will insert current date in "DateAdded" field.&lt;br&gt;&lt;br&gt;So, I start with a new website, add a DBML file and dropped the said table to generate LINQ To SQL context class. Finally, I write a simple code snippet to insert data into the tblEmployee table; BUT, I am not passing any value to "DateAdded" field. Because I am considering SQL Server's "Default Value or Binding (GetDate())" setting to this field and understand that SQL will insert current date to this field.&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; using (TestDatabaseDataContext context = new TestDatabaseDataContext())&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; tblEmployee tblEmpObjet = new tblEmployee();&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; tblEmpObjet.EmployeeName = "KaushaL";&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; tblEmpObjet.EmployeeEmailAddress = "kaushal@emaildomain.com";&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; context.tblEmployees.InsertOnSubmit(tblEmpObjet);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; context.SubmitChanges();&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;&lt;br&gt;Here comes the twist when application give me below error: &lt;/p&gt;&lt;p&gt;&lt;img src="http://weblogs.asp.net/blogs/kaushal/BlogFiles/2-26-2011%2010-45-13%20PM.png" title="Error Image" alt="Error Image" mce_src="http://weblogs.asp.net/blogs/kaushal/BlogFiles/2-26-2011%2010-45-13%20PM.png" height="269" width="703"&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;This is something not expecting! From the error it clearly depicts that LINQ is passing NULL value to "DateAdded" Field while according to my understanding it should respect Sql Server's "Default value or Binding" setting for this field. A bit googling and I found very interesting related to this problem.&lt;br&gt;&lt;br&gt;When we set Primary Key to any field with "Identity Specification" Property set to true; DBML set one important property "IsDbGenerated=true" for this field.&lt;/p&gt;&lt;p&gt;&lt;img src="http://weblogs.asp.net/blogs/kaushal/BlogFiles/2-26-2011%2010-49-34%20PM.png" title="IsDbGenerated=True Setting in Dbml.Designer.cs file" alt="IsDbGenerated=True Setting in Dbml.Designer.cs file" mce_src="http://weblogs.asp.net/blogs/kaushal/BlogFiles/2-26-2011%2010-49-34%20PM.png" height="219" width="424"&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;BUT, when we set "Default Value or Biding" property for some field; we need to explicitly tell the DBML/LINQ to let it know that this field is having default binding at DB side that needs to be respected if I don't pass any value. So, the solution is: You need to explicitly set "IsDbGenerated=true" for such field to tell the LINQ that the field is having default value or binding at Sql Server side so, please don't worry if i don't pass any value for it.&lt;br&gt;&lt;br&gt;You can select the field and set this property from property window in DBML Designer file or write the property in DBML.Designer.cs file directly.&lt;/p&gt;&lt;p&gt;&lt;img src="http://weblogs.asp.net/blogs/kaushal/BlogFiles/2-26-2011%2010-50-35%20PM.png" title="Field with Default Value or Binding needs to be set IsDbGenerated = true in DBML file" alt="Field with Default Value or Binding needs to be set IsDbGenerated = true in DBML file" mce_src="http://weblogs.asp.net/blogs/kaushal/BlogFiles/2-26-2011%2010-50-35%20PM.png" height="266" width="511"&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;I have attached a working example with required table script with this post &lt;a href="http://weblogs.asp.net/blogs/kaushal/BlogFiles/DBMLTest.zip" mce_href="http://weblogs.asp.net/blogs/kaushal/BlogFiles/DBMLTest.zip"&gt;here&lt;/a&gt;. I hope this would be helpful for someone hunting for the same. Happy Discovery!&lt;/p&gt;&lt;p mce_keep="true"&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7713023" width="1" height="1"&gt;</description><enclosure url="http://weblogs.asp.net/blogs/kaushal/BlogFiles/DBMLTest.zip" length="135453" type="application/x-zip-compressed" /><category domain="http://weblogs.asp.net/kaushal/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/kaushal/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/kaushal/archive/tags/IsDbGenerated/default.aspx">IsDbGenerated</category><category domain="http://weblogs.asp.net/kaushal/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://weblogs.asp.net/kaushal/archive/tags/Auto+Generated+Value/default.aspx">Auto Generated Value</category><category domain="http://weblogs.asp.net/kaushal/archive/tags/DBML/default.aspx">DBML</category><category domain="http://weblogs.asp.net/kaushal/archive/tags/Linq+To+SQL/default.aspx">Linq To SQL</category></item><item><title>Executing server validators first before OnClientClick Javascript confirm/alert</title><link>http://weblogs.asp.net/kaushal/archive/2011/02/26/executing-server-validators-first-before-onclientclick-javascript-confirm-alert.aspx</link><pubDate>Sat, 26 Feb 2011 13:23:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7712980</guid><dc:creator>kaushalparik27</dc:creator><slash:comments>5</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/kaushal/rsscomments.aspx?PostID=7712980</wfw:commentRss><comments>http://weblogs.asp.net/kaushal/archive/2011/02/26/executing-server-validators-first-before-onclientclick-javascript-confirm-alert.aspx#comments</comments><description>I got to answer a simple question over community forums. &lt;br&gt;&lt;br&gt;Consider this: Suppose you are developing a webpage with few input controls and a submit button. You have placed some server validator controls like RequiredFieldValidator to validate the inputs entered by the user. Once user fill-in all the details and try to submit the page via button click you want to alert/confirm the submission like "Are you sure to modify above details?". You will consider to use javascript on click of the button.&lt;br&gt;&lt;br&gt;Everything seems simple and you are almost done. BUT, when you run the page; you will see that Javascript alert/confirm box is executing first before server validators try to validate the input controls! Well, this is expected behaviour. BUT, this is not you want. Then? &lt;br&gt;&lt;br&gt;The simple answer is: Call Page_ClientValidate() in javascript where you are alerting the submission. Below is the javascript example:&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;script type="text/javascript" language="javascript"&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; function ValidateAllValidationGroups() {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (Page_ClientValidate()) {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return confirm("Are you sure to modify above details?");&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/script&amp;gt;&lt;br&gt;&lt;br&gt;Page_ClientValidate() function tests for all server validators and return bool value depends on whether the page meets defined validation criteria or not. In above example, confirm alert will only popup up if Page_ClientValidate() returns true (if all validations satisfy). You can also specify ValidationGroup inside this function as Page_ClientValidate('ValidationGroup1') to only validate a specific group of validation in your page.&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; function ValidateSpecificValidationGroup() {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (Page_ClientValidate('ValidationGroup1')) {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return confirm("Are you sure to modify above details?");&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;&lt;br&gt;I have attached a sample example with this post &lt;a href="http://weblogs.asp.net/blogs/kaushal/BlogFiles/Validation.zip" mce_href="http://weblogs.asp.net/blogs/kaushal/BlogFiles/Validation.zip"&gt;here&lt;/a&gt; demonstrating both above cases. Hope it helps./.&lt;br&gt;&lt;p mce_keep="true"&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7712980" width="1" height="1"&gt;</description><enclosure url="http://weblogs.asp.net/blogs/kaushal/BlogFiles/Validation.zip" length="2303" type="application/x-zip-compressed" /><category domain="http://weblogs.asp.net/kaushal/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/kaushal/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/kaushal/archive/tags/RequiredFieldValidator/default.aspx">RequiredFieldValidator</category><category domain="http://weblogs.asp.net/kaushal/archive/tags/OnClientClick/default.aspx">OnClientClick</category><category domain="http://weblogs.asp.net/kaushal/archive/tags/Page_5F00_ClientValidate_28002900_/default.aspx">Page_ClientValidate()</category></item><item><title>User is trying to leave! Set at-least confirm alert on browser(tab) close event!!</title><link>http://weblogs.asp.net/kaushal/archive/2011/02/25/user-is-trying-to-leave-trap-and-set-confirm-alert-on-browser-tab-close-event.aspx</link><pubDate>Fri, 25 Feb 2011 14:21:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7712574</guid><dc:creator>kaushalparik27</dc:creator><slash:comments>7</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/kaushal/rsscomments.aspx?PostID=7712574</wfw:commentRss><comments>http://weblogs.asp.net/kaushal/archive/2011/02/25/user-is-trying-to-leave-trap-and-set-confirm-alert-on-browser-tab-close-event.aspx#comments</comments><description>&lt;p&gt;This is something that might be annoying or irritating for end user. Obviously, It's impossible to prevent end user from closing the/any browser. Just think of this if it becomes possible!!!. That will be a horrible web world where everytime you will be attacked by sites and they will not allow to close your browser until you confirm your shopping cart and do the payment. LOL:) You need to open the task manager and might have to kill the running browser exe processes.&lt;br&gt;&lt;br&gt;Anyways; Jokes apart, but I have one situation where I need to alert/confirm from the user in any anyway when they try to close the browser or change the url. &lt;br&gt;&lt;br&gt;Think of this: You are creating a single page intranet asp.net application where your employee can enter/select their TDS/Investment Declarations and you wish to at-least ALERT/CONFIRM them if they are attempting to:&lt;br&gt;[1] Close the Browser&lt;br&gt;[2] Close the Browser Tab&lt;br&gt;[3] Attempt to go some other site by Changing the url&lt;br&gt;without completing/freezing their declaration.&lt;br&gt;&lt;br&gt;So, Finally requirement is clear. I need to alert/confirm the user what he is going to do on above bulleted events. I am going to use window.onbeforeunload event to set the javascript confirm alert box to appear.&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;script language="JavaScript" type="text/javascript"&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; window.onbeforeunload = confirmExit;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; function confirmExit() {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return "You are about to exit the system before freezing your declaration! If you leave now and never return to freeze your declaration; then they will not go into effect and you may lose tax deduction, Are you sure you want to leave now?";&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/script&amp;gt;&lt;br&gt;&lt;br&gt;See! you are halfway done!. So, every time browser unloads the page, above confirm alert causes to appear on front of user like below:&lt;br&gt;&lt;br&gt;&lt;img src="http://weblogs.asp.net/blogs/kaushal/BlogFiles/confirm-alert.png" title="User is trying to leave! Set confirm alert on browser(tab) close event!!" alt="User is trying to leave! Set confirm alert on browser(tab) close event!!" mce_src="http://weblogs.asp.net/blogs/kaushal/BlogFiles/confirm-alert.png" height="165" width="800"&gt;&lt;br&gt;&lt;br&gt;By saying here "every time browser unloads the page"; I mean to say that whenever page loads or postback happens the browser onbeforeunload event will be executed. So, event a button submit or a link submit which causes page to postback would tend to execute the browser onbeforeunload event to fire!&lt;br&gt;&lt;br&gt;So, now the hurdle is how can we prevent the alert "Not to show when page is being postback" via any button/link submit? Answer is JQuery :)&lt;br&gt;&lt;br&gt;Idea is, you just need to set the script reference src to jQuery library and Set the window.onbeforeunload event to null when any input/link causes a page to postback.&lt;br&gt;&lt;br&gt;Below will be the complete code:&lt;br&gt;&lt;br&gt;&amp;lt;head runat="server"&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;title&amp;gt;&amp;lt;/title&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;script src="jquery.min.js" type="text/javascript"&amp;gt;&amp;lt;/script&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;script language="JavaScript" type="text/javascript"&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; window.onbeforeunload = confirmExit;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; function confirmExit() {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return "You are about to exit the system before freezing your declaration! If you leave now and never return to freeze your declaration; then they will not go into effect and you may lose tax deduction, Are you sure you want to leave now?";&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; $(function() {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; $("a").click(function() {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; window.onbeforeunload = null;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; });&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; $("input").click(function() {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; window.onbeforeunload = null;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; });&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; });&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/script&amp;gt;&lt;br&gt;&lt;br&gt;&amp;lt;/head&amp;gt;&lt;br&gt;&amp;lt;body&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;form id="form1" runat="server"&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;div&amp;gt;&amp;lt;/div&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/form&amp;gt;&lt;br&gt;&amp;lt;/body&amp;gt;&lt;br&gt;&amp;lt;/html&amp;gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;So, By this post I have tried to set the confirm alert if user try to close the browser/tab or try leave the site by changing the url. I have attached a working example with this post &lt;a href="http://weblogs.asp.net/blogs/kaushal/BlogFiles/CloseConfirm1.1.zip" mce_href="http://weblogs.asp.net/blogs/kaushal/BlogFiles/CloseConfirm1.1.zip"&gt;here&lt;/a&gt;. I hope someone might find it helpful.&lt;/p&gt;&lt;p&gt;&lt;u&gt;&lt;b&gt;EDIT [26-February-2011]&lt;/b&gt;&lt;/u&gt;: I would like to edit this post here. I missed out to write that any control event that may cause a postback needs to be checked and set 
window.onbeforeunload = null; like I have set for Button Click or 
Hyperlink Click.&lt;/p&gt;&lt;p&gt;For Example, IF you have a&amp;nbsp; Select/DropDownList control which is causing a postback then you need to set window.onbeforeunload = null; on click of "select" as below:&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; $("select").click(function() {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; window.onbeforeunload = null;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; });&lt;/p&gt;&lt;p mce_keep="true"&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7712574" width="1" height="1"&gt;</description><enclosure url="http://weblogs.asp.net/blogs/kaushal/BlogFiles/CloseConfirm1.1.zip" length="30515" type="application/x-zip-compressed" /><category domain="http://weblogs.asp.net/kaushal/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/kaushal/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/kaushal/archive/tags/window.onbeforeunload/default.aspx">window.onbeforeunload</category><category domain="http://weblogs.asp.net/kaushal/archive/tags/kaushalparik27/default.aspx">kaushalparik27</category><category domain="http://weblogs.asp.net/kaushal/archive/tags/kaushalparik/default.aspx">kaushalparik</category><category domain="http://weblogs.asp.net/kaushal/archive/tags/browser+close+event/default.aspx">browser close event</category><category domain="http://weblogs.asp.net/kaushal/archive/tags/kaushal.net/default.aspx">kaushal.net</category><category domain="http://weblogs.asp.net/kaushal/archive/tags/confirm+alert+on+browser+close+event/default.aspx">confirm alert on browser close event</category></item><item><title>weblogs.asp.net! I am here now!</title><link>http://weblogs.asp.net/kaushal/archive/2011/02/12/weblogs-asp-net-i-am-here-now.aspx</link><pubDate>Sat, 12 Feb 2011 16:39:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7704298</guid><dc:creator>kaushalparik27</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/kaushal/rsscomments.aspx?PostID=7704298</wfw:commentRss><comments>http://weblogs.asp.net/kaushal/archive/2011/02/12/weblogs-asp-net-i-am-here-now.aspx#comments</comments><description>&lt;p style="line-height: 20px;"&gt;Hello all webloggers!!&lt;/p&gt;

&lt;p style="line-height: 20px;"&gt;Finally after much wait I got my blog space approved here. I really want to thank moderators (specially &lt;a href="http://forums.asp.net/members/tmorton.aspx" target="_blank" mce_href="http://forums.asp.net/members/tmorton.aspx"&gt;Terry&lt;/a&gt; for mail follow up) helping me out creating my weblog here. I; usually; blog about things and situation that I come across while development or something on which I succeeded to have some study/reading. Till now, I was maintaining my blog &lt;a href="http://dotnetslackers.com/community/blogs/kaushalparik/" target="_blank" mce_href="http://dotnetslackers.com/community/blogs/kaushalparik/"&gt;here&lt;/a&gt; (which I am still going to maintain in future as well!). Wishing for the best and thanks all future readers! &lt;/p&gt;

&lt;p style="line-height: 20px;"&gt;&lt;img src="http://weblogs.asp.net/blogs/kaushal/wlEmoticon-smile_017F9894.png" style="border-style: none;" class="wlEmoticon wlEmoticon-smile" alt="Smile" mce_src="http://weblogs.asp.net/blogs/kaushal/wlEmoticon-smile_017F9894.png"&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7704298" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/kaushal/archive/tags/general/default.aspx">general</category><category domain="http://weblogs.asp.net/kaushal/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/kaushal/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://weblogs.asp.net/kaushal/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/kaushal/archive/tags/SQL+Server/default.aspx">SQL Server</category></item></channel></rss>