Documente Academic
Documente Profesional
Documente Cultură
global.asax file contains the code for application level and session level events.
Also known as application file.
It is an optional file, which means we may include or exclude this file from web
application.
The location for file is ROOT folder.
global.asax Events:
1. Application_Start : This event fired when application first time starts. That means
when application is started in IIS server.
2. Application_End : Fired when Application is stopped, that is when IIS stops , IIS
restarts, Server crashed, Or manually application stopped.
5. Session_End : Session remain alive until user closes the browser or session time
out occurs. At the end of session Session_End event occurs.
--------------------------------------------------------------------
1. Using a simple approach that is using try –catch block. This is a performance
issue, we cannot put try-catch block everywhere.
2. web.config: In web.config file we can set the Error mode and Custom Error
Page address, so that whenever a error occurs, it redirects the Control to
Custom Error Page. In custom error page we can write code to display the
ERROR by using “Server.GetLastError” method or redirect control flow to Home
Page.
3. global.asax: We can write code here to handle the Error on Application level.
We can write code in Application_Error event to handle the Error. We can use
Server.GetLastError or redirect the control flow to Home page or any desired
page by using Response.Redirect.
Error handling code also includes code to create an entry in LogFile, which
includes the details of the Error.
http://www.c-
sharpcorner.com/UploadFile/akukreja/CustomErrorHandlingASPNET11142005235614PM/CustomErrorHandlingA
SPNET.aspx
--------------------------------------------------------------------
1. Required Filed validator : Attached with controls to make sure user should
enter some value.
<asp:RequiredFieldValidator ID="rfv_txt1" runat="server" ErrorMessage="Required!" ControlToValidate="txt1"/>
3. Range validator: This is used to assigned range to a text box, like integer,
dates, string etc.
5. Custom Validator: We can create a custom validator and can call a java
script function to add our own validation logic.
<script type="text/javascript">
function validatenumber(oSrc,args)
{ args.IsValid = (args.Value % 6 == 0) }
</script>
Note 3: We can also set Style of the Error message, like back color , font, bold.
http://www.beansoftware.com/ASP.NET-Tutorials/Validation-In-ASP.NET.aspx
--------------------------------------------------------------------
What are Application variables and Session variables?
Application variables: are used to store the variable values on application level,
which means we can create such variables which are available to all the pages in
application and remains alive until the application remains alive.
For example we want to set a counter for Logged in user. For this we can create
a application variable say UserCount and can update the value of counter in
Session_Start and Session_End events in global.asax.
// to set the data
Application( ' UserCount ' ) = 'something';
Session variables: are used to store the variable values on Session level, which
means the Session variables values are available only for that session.
Suppose we want to save the User Id for each logged n user. In this case we can
create Session(“UserID”) variable and can set the value for each logged in user
in Login page code.
So the UserID value will be different for each user because Session variables are
Session specific.
--------------------------------------------------------------------
How to add the Java Script code block through c#.net/vb.net code?
RegisterStartupScript insert the java script block in the end of the page, whereas
RegisterClientScriptBlock insert the java script block in the top of the page.
So if you want to insert a java script code which runs immediately after the
page loads in client side use StartUpScript. If you put the code on the top of the
page which runs immediately when page loads and your code is on the top of
the page, this code will unable to Search the Controls in the page, because this
piece of java code is being inserted and executed immediately before rendering
the other controls which are below the java script code.
If you want are inserting code which fires for some events (e.g. OnClick(),
OnChange()) you can used either method, but recommended is
ClientScriptBlock().
// Define the JavaScript function for the specified control.
string focusScript = "<script language='javascript'>" +
"document.getElementById('" + ctrl.ClientID +
"').focus();</script>";
if ( !Page.IsClientScriptBlockRegistered("mytest") )
Page.RegisterClientScriptBlock("myrest",
"<script>alert('Hello');</script>
Note: the name is StartUp that means we want to run the code immediately in
startup, so use RegisterStartupScript and it will goes to bottom. And to just
insert code any where you can use RegisterClientScriptBlock.
--------------------------------------------------------------------