Showing posts with label TextBox. Show all posts
Showing posts with label TextBox. Show all posts

Monday, 8 October 2012

We often get requirement to allow only numeric decimal value in textbox for validation purpose, And also we have to specify number of decimal places. We can use ASP.Net RegularExpressionValidator control for this. In this control we can specify regex expression for validation.

The regex expression is "^[+]?[0-9]\d{0,5}(\.\d{1,4})?%?$" .
This regex allow only positive numeric value of maximum six digit and maximum four digit decimal value. The maximum allowed value by this regex is "999999.9999". You can change this accroding to your requirement.

Here is example.

Thursday, 4 October 2012

For validation purpose in your project at some places you put text boxes that accept only integer value i.e. 1, 20 , 50, 200 etc... To put validation on that text boxes and allow only integer value you can use ASP.NET Regular Expression Validator Control. In this control we make one regex expression and do validation for that text box.
The regex expression is "^\d+$" .

Here is example.

Saturday, 25 August 2012

You can make textbox disabled. Means do not allow enter or change text into textbox.
You can do this with the help of "Enabled" property. You need to set Enabled=false to make text box disabled.
By default text box is enabled.

Here is example for this.
In this example we have one text box and we make that textbox disabled.
You can set this property from aspx side or from Coding side.

ASPX Code:
<asp:TextBox runat="server" ID="txtProductTitle" Text="Keyboard" Enabled="false" ></asp:TextBox>

Output : 




This is very useful articles for Beginning .Net .

This type of C# Tips is very useful in day to day programming life.

Note : Give Us your valuable feedback in comments. Give your suggestions in this article so we can update our articles accordingly that.



Friday, 24 August 2012

There are situation in multi line textbox where you want to wrapping text in textbox and sometimes you not want to wrap text , that text type as it is.
We can achieve this using "Wrap" property. By Default it is true.

In textbox Wrapping means if you entered text more than width of the textbox it will automatically set cursor into next line. In unwrapping if you entered text more than width of the textbox there is one horizontal scrollbar appear below the textbox and your line is continued.

Here is example for this.
In this example you can see that we take two text boxes one is "Product Description" and Second is "Product Meta Description". And both this text boxes are multiline textbox.
In "Product Description" textbox we allow warp text in this textbox.
And in "Product Meta Description" textbox we do not allow wraping in text.

You can see in output that in "Product Description" textbox new line created without pressing enter or return key. In "Product Meta Description" textbox scrollbar is appear when entered more data.

ASPX Code :
        <table>
            <tr>
                <td>
                    Product Description :
                </td>
                <td>
                    <asp:TextBox runat="server" ID="txtProductDescription" TextMode="MultiLine" Wrap="true"
                        Height="120px" Width="250px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Product Meta Description :
                </td>
                <td>
                    <asp:TextBox runat="server" ID="txtProductMetaDescription" TextMode="MultiLine" Wrap="false"
                        Height="120px" Width="250px"></asp:TextBox>
                </td>
            </tr>
        </table>

Output : 
(To view original image , click on image)

Here is example for set Maximum characters length of multi line textbox. Click Here...
 
For Beginning .Net articles. Click Here...
 
Here is very useful .Net Tips.


Note : Give Us your valuable feedback in comments. Give your suggestions in this article so we can update our articles accordingly that.


Thursday, 23 August 2012

 Click Here to Download TextBoxMaximumLengthApplication.zip

There is always a need of application to set maximum length allowed in textbox.
In this article we demonstrate both single line textbox and multi line textbox max length validation. 
There is a different way to set maximum characters allowed for single line textbox and multiline textbox.
You can set "MaxLength" property for single line textbox.

 For multiline textbox "MaxLength" does not supported.

So for Multi Line textbox we need some tricky approch. We can use javascript , JQuery or server side checking. Here we are going to use .Net Validator controls. We are using "RegularExpressionValidator" control.

Here is example for this.

In this example we are taking two textbox one is for ProdutTitle which is singleline textbox and other is for ProductDescription which is MultiLine textbox. For ProductTitle we are using MaxLength property to set maximum allowed characters. Which is 20 characters.
For ProductDescription we are using "RegularExpressionValidator" control to set maximum allowed characters. Which is 50 characters.
We are using Regex expression for validation.
The "RegularExpressionValidator" control display error message after typing in textbox focus or cursor moved out from textbox.
You can also make server side validation check for regular expression validation using "Page.Validate()" Method and "Page.IsValid" property.

In output you can see that if more than maximum allowed characters type in textbox it will give error message. You can also make you customize message.


ASPX Code:
        <table>
            <tr>
                <td>
                    Product Title :
                </td>
                <td>
                    <asp:TextBox runat="server" ID="txtProductTitle" MaxLength="20" ></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Product Description :
                </td>
                <td>
                    <asp:TextBox runat="server" ID="txtProductDescription"  TextMode="MultiLine"></asp:TextBox>
                    <asp:RegularExpressionValidator runat="server" ID="REVProductDescription" ControlToValidate="txtProductDescription"
                        ValidationExpression="^[\s\S]{0,50}$" style="color:Red;font-weight:bold" ErrorMessage="Please enter a maximum of 50 characters"
                        Display="Dynamic"></asp:RegularExpressionValidator>
                </td>
            </tr>
        </table>

Output : 
(To view original image , click on image)

For Beginning .Net articles. Click Here...
 
Here is very useful .Net Tips.


Note : Give Us your valuable feedback in comments. Give your suggestions in this article so we can update our articles accordingly that.



Saturday, 18 August 2012

As a .Net Beginners you must know how to make textbox readonly.

You can make textbox read only. Means do not allow type text in textbox and also is there is any text predefined in that you are not able to change that text.
You can do this with the help of "ReadOnly" property. You can set ReadOnly=true to make textbox read only.
By default text box is not readonly.

Here is example for this.
In this example we have one text box and we make that textbox readonly.
You can set this property from aspx side or from Coding side.
In this example we are showing from both side

ASPX Code:
<asp:TextBox runat="server" ID="txtProductName" Text="Keyboard"  ></asp:TextBox>

C# Examples :
txtProductName.ReadOnly = true;

VB.net Examples :
txtProductName.ReadOnly = True

Output : 


You can use Either aspx side or Coding side approach.

This is very useful articles for Beginning .Net .

This type of C# Tips is very useful in day to day programming life.

Note : Give Us your valuable feedback in comments. Give your suggestions in this article so we can update our articles accordingly that.


Saturday, 21 July 2012

As a .Net Beginners you must know how to set textbox width.
You can set "width" property of TextBox control to set width.
You can set "width" property in Pixel or Percentage wise.

Here is Example for this.
In this example we are setting different width in pixel as well as percentage.

ASPX Code :
<asp:TextBox runat="server" ID="txt1"></asp:TextBox>
<asp:TextBox runat="server" ID="TextBox1" Width="10px"></asp:TextBox>
<asp:TextBox runat="server" ID="TextBox2" Width="20px"></asp:TextBox>
<asp:TextBox runat="server" ID="TextBox3" Width="200px"></asp:TextBox>
<asp:TextBox runat="server" ID="TextBox4"  Width="20%"></asp:TextBox>
<asp:TextBox runat="server" ID="TextBox5" Width="100%"></asp:TextBox>

Output :
(To view original image , click on image)

Here in output we made slightly changes we display textbox with it's actual html tag, so you can easily differentiate it's width.


Note : Give Us your valuable feedback in comments. Give your suggestions in this article so we can update our articles accordingly that.



Saturday, 14 July 2012

There is a situation where you need to focus on text box after page is loaded.
This is the best user friendly concept.

You can do this with two ways.
First use .Focus() method of TextBox Control.
Second use Page.SetFocus method.

Here is example for this.

C# Example :
 //Use Focus method of textbox
 txtProductName.Focus();

 //Use SetFocus method of Page
 Page.SetFocus(txtProductName);

VB.net Example :
''Use Focus method of textbox
txtProductName.Focus()

''Use SetFocus method of Page
Page.SetFocus(txtProductName)

 You can use anyone method from above. Do not use both method at a time.

This article is very useful for .Net Beginners.


Note : Give Us your valuable feedback in comments. Give your suggestions in this article so we can update our articles accordingly that.




Saturday, 7 July 2012

You can submit a page when textbox value changed.
For that you need to take asp textbox control and set it's AutoPostBack property to true and handle it's ontextchanged event.

This article is very useful for .Net Beginners.

Here is example of this.
In this example we set autopostback=true and handle textbox change event. And on server side we handle change event and set textbox value to label control.

C# ASPX Code : 
<asp:TextBox runat="server" ID="txtProductName" AutoPostBack="true" ontextchanged="txtProductName_TextChanged"></asp:TextBox>
<br />
<asp:Label runat="server" ID="lblProductName"></asp:Label>

C# Example :
    protected void txtProductName_TextChanged(object sender, EventArgs e)
    {
        lblProductName.Text = txtProductName.Text;
    }

VB.Net ASPX Code :
<asp:TextBox runat="server" AutoPostBack="true" ID="txtProductName"></asp:TextBox>
<br />
<asp:Label runat="server" ID="lblProductName"></asp:Label>

VB.net Example :
    Protected Sub txtProductName_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles txtProductName.TextChanged
        lblProductName.Text = txtProductName.Text
    End Sub

In Vb.Net we are using handle keyword to handle event.So that we do not set ontextchanged property.
You can also set ontextchanged property as we do in C# Example. In this case you need to remove that Handle from server side code. Otherwise textchanged event call two times.

Output : 


Note : Give Us your valuable feedback in comments. Give your suggestions in this article so we can update our articles accordingly that.


You need this password textbox where you would like you insert username and password from website users. With the help of this password textbox inputed password character is not see on textbox , it will display astrik "*" or other special characters.
This is very useful article for .Net Beginners.

Here is example for this.
In this example we take two textbox one is for username and second is for password. Username password is normal textbox and password textbox is special password type textbox. There is a "TextMode"  property of textbox , you canse this property as "password" like this TextMode="Password" , by doing this your textbox is converted to password type textbox.

ASPX Code : 
        <table>
            <tr>
                <td>
                    User Name :
                </td>
                <td>
                    <asp:TextBox runat="server" ID="txtUserName"></asp:TextBox></td>
            </tr>
            <tr>
                <td>
                    Password :</td>
                <td>
                    <asp:TextBox runat="server" ID="txtPassword" TextMode="Password"></asp:TextBox></td>
            </tr>
        </table>

Output : 

You can see in the output image, In password textbox you did not see password in plain text , this will display in special characters.

Note : Give Us your valuable feedback in comments. Give your suggestions in this article so we can update our articles accordingly that.


Wednesday, 27 June 2012

You can set and get textbox value programmatically using C# and VB.Net. Every .Net Beginners needs to know this.
You can use "Text"  property of textbox to set and get value of textbox.

Here is example of this.
This example is in both C# .Net Programming and VB.Net Programming.
In this example we have one textbox it's id is "txtProductName".

ASPX Code : 
        <asp:TextBox runat="server" ID="txtProductName"></asp:TextBox>
        <br />
        <asp:Label runat="server" ID="lblProductName"></asp:Label>


C# Example :
        //Set textbox value
        txtProductName.Text = "ASP.net book";

        //Get textbox value and display in label
        lblProductName.Text = "Product Name : " + txtProductName.Text.Trim();

VB.net Example :
        ''Set textbox value
        txtProductName.Text = "ASP.net book"

        ''Get textbox value and display in label
        lblProductName.Text = "Product Name : " + txtProductName.Text.Trim()

Output : 



You can use Text.Trim() method to remove extra whitespace from left and rights side of text.

Note : Give Us your valuable feedback in comments. Give your suggestions in this article so we can update our articles accordingly that.