Click here to Skip to main content
15,885,032 members
Articles / Web Development / ASP.NET / ASP.NET4.0
Tip/Trick

Display Selected Value for TextBox Mode=Range

Rate me:
Please Sign up or sign in to vote.
4.78/5 (4 votes)
2 Dec 2013Public Domain2 min read 27.9K   179   5   5
Display selected value for the TextBox when TextMode="Range" without server postback.

Introduction

ASP.NET has introduced a new property for the TextBox called TextMode="Range". Even though we use the TextBox, ASP.NET provides tremendous look and feel like track bar. This tip is going to explain how to display selected value on ASP.NET page without any postback or Ajax.

Background

As explained a little in the introduction, Textbox's TextMode property has many values. Out of them there is one value which I am going to explain - Range. We are going to make use of this property for real time scenarios.

Code Walkthrough

  • Simply create a New project and add an ASPX page.
  • After this, drag and drop the TextBox control from the toolbar.
  • Modify the TextMode property to "Range".
  • Run the project.

The interesting thing was that TextBox was converted to like TrackBar shown below:

When I tried to select some value, the selected value appeared to be on ToolTip field. (See the below image.)

But this was only when your mouse is used to change value otherwise it was simply blank. Then I realized that the missing part is the display the current value which is selected by this control. To get the selected value and display on the page something like below, we have two options (Both are explained below).

  1. In order to display selected value, we can have another label and at the server side TextChanged event sets the label to selected value. This requires the server side code execution which is time consuming and overhead as an extra web request by browser to server.
  2. Another option would be on client side. To achieve the display, I have created a sample application which uses JavaScript to display selected value. By this means, all display is handled by the client browser and there is no server post needed. You can easily change the display style if you want to make it consistent, even you can place the display anywhere as per your requirement.

Now modify the added web page with “TextMode=Range”.

ASP.NET
<asp:TextBox ID="TextBox1" 
runat="server" TextMode="Range" />

Then, I added a JavaScript event “OnClick” to this textbox so handle the client side click. Next I added another control to display the selected value. You can use any other control.

ASP.NET
<input id="Text1" type="text" 
readonly="true"/>

After the UI work, I wrote the JavaScript code block and some methods. One of them was hooked to OnClick event of TextBox. Something like below:

JavaScript
<script type="text/javascript">
        function onValueChanged() {
            document.getElementById("Text1").value = 
            		document.getElementById("TextBox1").value;
        }

        function onLoad() {
            document.getElementById("Text1").value = 
            		document.getElementById("TextBox1").value;
        }
        window.onload = onLoad;

</script>
Note:
getElementById method in JavaScript identifies the control by the Id of assigned to control. If you are using Master pages, then this may not be a valid Id and you may get an error at JavaScript. Find the proper Id of control using any debugging tool and assign it.

After this, I just added the OnClick event handler for the TextBox.

ASP.NET
<asp:TextBox ID="TextBox1" runat="server" OnClick="onValueChanged();" TextMode="Range" /> 

That’s it. All done!!!

Download the code from the link at the top of this tip.

Run your application to view the effect.

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication


Written By
Software Developer (Senior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionIt is not working in my Win 7 Pin
HamidYaseen2-Dec-13 9:13
professionalHamidYaseen2-Dec-13 9:13 
AnswerRe: It is not working in my Win 7 Pin
JV99992-Dec-13 23:37
professionalJV99992-Dec-13 23:37 
GeneralRe: It is not working in my Win 7 Pin
HamidYaseen3-Dec-13 8:48
professionalHamidYaseen3-Dec-13 8:48 
GeneralRe: It is not working in my Win 7 Pin
Jitendra_Shri10-Dec-13 23:23
Jitendra_Shri10-Dec-13 23:23 
AnswerRe: It is not working in my Win 7 Pin
Richard Deeming3-Dec-13 6:51
mveRichard Deeming3-Dec-13 6:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.