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 )