ASP.net – How to make text box as Read Only

There are two ways of doing it without using javascript:

1) In .aspx side in the declaration of TextBox use the property ReadOnly=“true” and the user wont be able to alter the text box text unless specified from the code behind .cs side.

2)in .cs side where in we can write txtUserName.ReadOnly = true; we can write it  in page load or in button click event or to show default text in text box can supply the text from code behind like this  txtUserName.Text = “Dilip”; where  txtUserName is the ID of the TextBox.

Reference : Dilip Kumar Jena ( https://mstechexplore.wordpress.com )

DOT NET – How to make a button as default button and set Focus on a particular text box

In the initial days when i was new to development side and in some site i used to login straight away when i enter username and password and hit Enter key on my keyboard.

I used to think how is this happening but never had any answer but now i am going to show how thats possible.

In this post I will be talking about how  to make any button Default button and set Focus on a particular text box in any page thats displayed.

Ex: Make a test site and go to default.aspx

Step 1:

in <body> tag write the following code

<body>

<form id=”frmExample” runat=”server” defaultbutton=”btnLogin” defaultfocus=”txtUserName”>

<table><tr><td width=”50%”>

Welcome to the Example Web site from dilip

</td>

<td width=”50%”>

<table>

<tr><td><asp:TextBox ID=”txtUserName” runat=”server” Text=”UserName” /></td><td><asp:TextBox ID=”txtPassword” runat=”server” Text=”Password”/></td></tr>

<tr><td><asp:Button ID=”btnLogin” runat=”server” Text=”Login” onclick=”btnLogin_Click”/></td><td><asp:Button ID=”btnSignIn” runat=”server” Text=”Signup”/></td></tr>

</table>

</td>

</tr>

</table>

</form>

</body>

Note: Here the beauty is that i have login and singnup button and i made defaultbutton as login button, and defaultfocus on username text box so whenever this page is viewed from any web browser focus will be on username text box and default button will be login button so when u hit enter it will invoke login button so simple.

 Step 2: see  carefully i have a function onclick=”btnLogin_Click” now press F7 key and go to .cs side of the same page or right click on the mouse and select view code .

and write the following code in .cs side

protected void btnLogin_Click(object sender, EventArgs e)

{

Response.Write(“Default Button was clicked and u are now logged In”);

Response.End();

}

Note: Here another concept when ever you enter key this method will be invoked and at the same time there wont be any other you can see that means response is closed just to make you feel that you really logged in but actually u are in same page.

the output is some this like this :

First Time when visited:

you can see username text box is in focus.

Then when enter key is pressed:

Reference : Dilip Kumar Jena ( https://mstechexplore.wordpress.com )

CSS NAVIGATION BAR

Many a time when there is a need to go for a navigation bar we simply remember javascript and write a functionality based on mouse onhouver property, we achieve the functionality but sometimes when javascript is disabled on clients computer then what the function never fires but in CSS we have functionality called hover will always run independent of javascript and writing is also very simple here is example how we can achieve it.

Step 1: Create an html file copy and paste the code below.

xmlns=”http://www.w3.org/1999/xhtml”&gt;

<head>

<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ />

<title>Strip Menu Model</title>

<style type=”text/css”>

.stripboxmenu {padding: 0; width: 100%; /* width of the menu strip, 100% will extend to the full resolution of the screen */

border-top: 5px solid #000033; /*specify the color strip width,color */

background: transparent;

}

.stripboxmenu ul {margin:0;

margin-left: 80px; /*space between first menu item and left edge*/

padding: 0; /*Spacing between the strip and menu boxes*/

list-style: none;

}

.stripboxmenu li {display: inline;

margin: 2 2px 2 2;

padding: 0;

text-transform:uppercase;

}

.stripboxmenu a {

float: left;

display: block;

font: bold 12px Arial; /* Specify the font attributes like font style, boldness */

color: black; /* specify the color of the font */

text-decoration: none; /* remove the default underline of the anchor tag */

margin: 2 2px 2 2; /*Margin between each menu item, you can increase the spacing between the menus by adjusting the attributes*/

padding: 7px 15px 7px 15px; /*with this we can increase the width of the menu item (blue background)*/

background-color: lightblue; /*menu color*/

border-bottom: 9px solid white;

}

.stripboxmenu a:hover{

background-color: #f98a10; /*Mouseover color*/

padding-top: 20px; /*increase the height of the menu on mouseover using padding attribute */

border-bottom-color: #f98a10;

color: white;

}

/*Specify the style for current tab*/

.stripboxmenu .current a{

background-color: #000000;

padding-top: 12px;

border-bottom-color: #000000;

color: white;

}

</style>

</head>

<body>

<div>

<ul>

<li><a href=”#” title=”home”>Home</a></li>

<li><a href=”#” title=”what we do”>What We Do</a></li>

<li><a href=”#” title=”services”>Services</a></li>

<li><a href=”#” title=”clients”>Clients</a></li>

<li><a href=”#” title=”contact us”>Contact Us</a></li>

</ul>

</div>

<br style=”clear: both;” />

</body>

</html>

Step 2: now run the file in any browser it will look like this

As the html is rendered as li and anchor link inside it we can have many li inside another li to have sub sub menus.

Reference : Dilip Kumar Jena ( https://mstechexplore.wordpress.com )

ASP – Application Object

An application on the Web may be a group of ASP files. The ASP files work together to perform some purpose. The Application object in ASP is used to tie these files together.

The Application object is used to store and access variables from any page, just like the Session object. The difference is that ALL users share one Application object, while with Sessions there is one Session object for EACH user.

The Application object should hold information that will be used by many pages in the application (like database connection information). This means that you can access the information from any page. It also means that you can change the information in one place and the changes will automatically be reflected on all pages.

 

Store and Retrieve Application Variables

Application variables can be accessed and changed by any page in the application.

You can create Application variables in “Global.asa” like this:

<script language=”vbscript” runat=”server”>

 

Sub Application_OnStart

application(“vartime”)=””

application(“users”)=1

End Sub

 

</script>

In the example above we have created two Application variables: “vartime” and “users”.

You can access the value of an Application variable like this:

There are

<%

Response.Write(Application(“users”))

%>

active connections.

 

Loop Through the Contents Collection

The Contents collection contains all application variables. You can loop through the Contents collection, to see what’s stored in it:

<%

dim i

For Each i in Application.Contents

Response.Write(i & ”
“)

Next

%>

If you do not know the number of items in the Contents collection, you can use the Count property:

<%

dim i

dim j

j=Application.Contents.Count

For i=1 to j

Response.Write(Application.Contents(i) & ”
“)

Next

%>

 

Loop Through the StaticObjects Collection

You can loop through the StaticObjects collection, to see the values of all objects stored in the Application object:

<%

dim i

For Each i in Application.StaticObjects

Response.Write(i & ”
“)

Next

%>

 

Lock and Unlock

You can lock an application with the “Lock” method. When an application is locked, the users cannot change the Application variables (other than the one currently accessing it). You can unlock an application with the “Unlock” method. This method removes the lock from the Application variable:

<%

Application.Lock

‘do some application object operations

Application.Unlock

%>

The #include directive is used to create functions, headers, footers, or elements that will be reused on multiple pages.

 

The #include Directive

You can insert the content of one ASP file into another ASP file before the server executes it, with the #include directive. The #include directive is used to create functions, headers, footers, or elements that will be reused on multiple pages.

 

How to Use the #include Directive

Here is a file called “mypage.asp”:

<html>

<body>

<h3>Words of Wisdom:</h3>

<p><!–#include file=”wisdom.inc”–></p>

<h3>The time is:</h3>

<p><!–#include file=”time.inc”–></p>

</body>

</html>

Here is the “wisdom.inc” file:

“One should never increase, beyond what is necessary,

the number of entities required to explain anything.”

Here is the “time.inc” file:

<%

Response.Write(Time)

%>

If you look at the source code in a browser, it will look something like this:

<html>

<body>

<h3>Words of Wisdom:</h3>

<p>”One should never increase, beyond what is necessary,

the number of entities required to explain anything.”</p>

<h3>The time is:</h3>

<p>11:33:42 AM</p>

</body>

</html>

Syntax for Including Files

To include a file in an ASP page, place the #include directive inside comment tags:

<!–#include virtual=”somefilename”–>

or

<!–#include file ="somefilename“–>

The Virtual Keyword

Use the virtual keyword to indicate a path beginning with a virtual directory.

If a file named “header.inc” resides in a virtual directory named /html, the following line would insert the contents of “header.inc”:

<!– #include virtual =”/html/header.inc” –>

The File Keyword

Use the file keyword to indicate a relative path. A relative path begins with the directory that contains the including file.

If you have a file in the html directory, and the file “header.inc” resides in html\headers, the following line would insert “header.inc” in your file:

<!– #include file =”headers\header.inc” –>

Note that the path to the included file (headers\header.inc) is relative to the including file. If the file containing this #include statement is not in the html directory, the statement will not work.

You can also use the file keyword with the syntax (..\) to include a file from a higher-level directory.

 

Tips and Notes

In the sections above we have used the file extension “.inc” for included files. Notice that if a user tries to browse an INC file directly, its content will be displayed. If your included file contains confidential information or information you do not want any users to see, it is better to use an ASP extension. The source code in an ASP file will not be visible after the interpretation. An included file can also include other files, and one ASP file can include the same file more than once.

Important: Included files are processed and inserted before the scripts are executed.

The following script will not work because ASP executes the #include directive before it assigns a value to the variable:

<%

fname=”header.inc”

%>

<!–#include file=”<%=fname%>”–>

You cannot open or close a script delimiter in an INC file. This script will not work:

<%

For i = 1 To n

<!–#include file=”count.inc”–>

Next

%>

But this script will work:

<% For i = 1 to n %>

<!–#include file=”count.inc” –>

<% Next %>

The Global.asa file is an optional file that can contain declarations of objects, variables, and methods that can be accessed by every page in an ASP application.

Reference : Dilip Kumar Jena ( https://mstechexplore.wordpress.com )

ASP – Lifetime of Variables

A variable declared inside a procedure is created and destroyed every time the procedure is executed. No scripts outside the procedure can access or change the variable.

To declare variables accessible to more than one ASP file, declare them as session variables or application variables.

Session Variables

Session variables are used to store information about ONE single user, and are available to all pages in one application. Typically information stored in session variables are name, id, and preferences.

Application Variables

Application variables are also available to all pages in one application. Application variables are used to store information about ALL users in a specific application.

Procedures

The ASP source code can contain procedures and functions:

<html>

<head>

<%

sub vbproc(num1,num2)

response.write(num1*num2)

end sub

%>

</head>

<body>

Result:

</body>

</html>

Insert the <%@ language=”language” %> line above the tag to write procedures or functions in another scripting language than default:

<%@ language=”javascript” %>

<html>

<head>

<%

function jsproc(num1,num2)

{

Response.Write(num1*num2)

}

%>

</head>

<body>

Result:

</body>

</html>

Reference : Dilip Kumar Jena ( https://mstechexplore.wordpress.com )