Showing posts with label default. Show all posts
Showing posts with label default. Show all posts

Saturday, March 31, 2012

Text Box

I have a asp.net textbox on my page with a default value in it. I would like
the box to be cleared when some one clicks on the box.
How do I go about doing this?you need to add some client-side javascript like this:

onfocus=" if ( this.value == 'some default' ) this.value=''; " onblur="
if ( this.value == '' ) this.value = 'some default' ; "

Lou Civitella wrote:

Quote:

Originally Posted by

I have a asp.net textbox on my page with a default value in it. I would like
the box to be cleared when some one clicks on the box.
How do I go about doing this?

Text Box to change on focus... Possible?

Hi i was just wondering if it was possible to change a text boxes default value onFocus... I have a search bar and rather then put next to it search product here I wanted to have written in the text box as default Enter Product Code.. and then when the box is clicked on it clears this text away and has a blank textbox before they begin to type

Thanks in advance for any help given

Hey,

Yes, there are two ways to do it. There is a TextboxWatermarkExtender (in the AJAXControlToolkit), which renders the text and manages that automatically for you, or you can use manual javascript, simply by catching the focus and when the textbox value is equal to the watermark value, clear it. I don't have an example on-hand, but you should be able to find one.


Create a test ASPX and add following:


<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" Text="Enter Here"></asp:TextBox> <br/>
</div>
</form>
<script language="javascript" type="text/javascript">
function ClearValue()
{
document.getElementById("<%=TextBox1.ClientID %>").value = "";
}
</script>


In Code Behind add following:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
TextBox1.Attributes.Add("OnFocus", "javascript:ClearValue();")
End Sub

Hope this will serve the purpose.


Code for the text box:

<input id="txtBox1" type="text" value="Some Default Value" onfocus="ClearTxt();" />

Javascript function to clear default value:

<script type="text/javascript">
function ClearTxt() {
document.getElementById("txtBox1").value = ""
}
</script>


you could use a javascript method like teh one shown here

http://www.urban75.org/tech/q024.html


Thanks alot guys for all the help


try this

tst.Attributes.Add("onFocus","ctrl = document.getElementById('tst');ctrl.value = '';")

replace both occurrences of tst with the name of the textbox


Hi sorry i know this is an old post but I was wondering, I managed to get this working the only little thing is if I click in the box it deletes the text (what I was looking for) but if I dont type anything and click away from the box it's still blank.. I was wondering if there was a way to refill the text box with the default text again

Thanks


Hey,

Attach to the client onblur event, that if the textbox's text length is zero, put the text back in the textbox.


I know I am a strong anti-Javascript developer, but using javascript to set properties of a form control is not a very good idea, especially if you consider accessibility issues. Rather, why not to attach labels to the text box, and/or use tool-tip feature for displaying user information. This is both, dynamic, on focus, as well as Accessible to challanged users.

Again, it is kind of personal opinion, but then you have to think about the accessibility issues at some point of time!

Monday, March 26, 2012

text length in pixels

Hi,

I am trying to resolve the following scenario:
We have a text box of fixed width that by default should display only 1 line
of text without a scrollbar indicator.
If there are multiple lines of text then we need to to indicate that by
displaying a button alongside the textbox that the user can click on to
expand the height of the text box. The height would be, say, 4 lines of
text. If there are more than 4 lines of text then we need to display a
vertical scrollbar.
To be able to achieve this, it seems, we need to be able to compare the
length of the text with that of the textbox. If we can somehow get the
length in pixels that would solve part of the problem?

Any ideas would be greatly appreciated...

Thanks, VishalInstead of trying to accomplish this by pixels (the hard way), try this:

A textbox that does not have any styling applied to it will use a font where
different characters are different widths (capital W is wider than lower
case l). So, pick a number of characters that you consider to be one full
line of text. You can programmatically check the number of characters in a
textbox and when you reach your one line limit, you can change the rows
property of the textbox to 4. The vertical scroll bar is built in, so you
don't have to worry about enabling it when needed.

"Vishal Gupta" <vishalg_99@.hotmail.com> wrote in message
news:eqX6Zmr3DHA.2112@.TK2MSFTNGP10.phx.gbl...
> Hi,
> I am trying to resolve the following scenario:
> We have a text box of fixed width that by default should display only 1
line
> of text without a scrollbar indicator.
> If there are multiple lines of text then we need to to indicate that by
> displaying a button alongside the textbox that the user can click on to
> expand the height of the text box. The height would be, say, 4 lines of
> text. If there are more than 4 lines of text then we need to display a
> vertical scrollbar.
> To be able to achieve this, it seems, we need to be able to compare the
> length of the text with that of the textbox. If we can somehow get the
> length in pixels that would solve part of the problem?
> Any ideas would be greatly appreciated...
> Thanks, Vishal
Check out the .NET Framework Class Library for:

Graphics.MeasureString Method (String, Font)

"Scott M." <s-mar@.BADSPAMsnet.net> wrote in message
news:%233OjMrr3DHA.3656@.TK2MSFTNGP11.phx.gbl...
> Instead of trying to accomplish this by pixels (the hard way), try this:
> A textbox that does not have any styling applied to it will use a font
where
> different characters are different widths (capital W is wider than lower
> case l). So, pick a number of characters that you consider to be one full
> line of text. You can programmatically check the number of characters in
a
> textbox and when you reach your one line limit, you can change the rows
> property of the textbox to 4. The vertical scroll bar is built in, so you
> don't have to worry about enabling it when needed.
>
> "Vishal Gupta" <vishalg_99@.hotmail.com> wrote in message
> news:eqX6Zmr3DHA.2112@.TK2MSFTNGP10.phx.gbl...
> > Hi,
> > I am trying to resolve the following scenario:
> > We have a text box of fixed width that by default should display only 1
> line
> > of text without a scrollbar indicator.
> > If there are multiple lines of text then we need to to indicate that by
> > displaying a button alongside the textbox that the user can click on to
> > expand the height of the text box. The height would be, say, 4 lines of
> > text. If there are more than 4 lines of text then we need to display a
> > vertical scrollbar.
> > To be able to achieve this, it seems, we need to be able to compare the
> > length of the text with that of the textbox. If we can somehow get the
> > length in pixels that would solve part of the problem?
> > Any ideas would be greatly appreciated...
> > Thanks, Vishal

Thursday, March 22, 2012

TextAlignment

How to align the text in textbox to the right. The default is left aligned.

Thanx

You can do that in the Style attribute (style="text-align: right;") , for example:

<asp:TextBoxID="TextBox1"runat="server"Text="test"style="text-align: right;" />

Or specify text-align:right; in a CSS class and use that cssClass on you Textbox

textarea (multi line) how to set default value ?

Hi,
I cannot figure out how to set the default value for a <textarea> control in my page:

Here is my current code for this control:

<textarea name="contMessage" cols="35" rows="5" id="contMessage" runat="server" tooltip="enter your message here" /></textarea
how do I add a default value for this multi-line text field?


<textarea name="contMessage" cols="35" rows="5" id="contMessage" runat="server" tooltip="enter your message here" />
THIS IS THE TEXT APEARING IN THE TEXTAREA
</textarea>

put the text in between the textarea tags like so...

<textarea name="contMessage" cols="35" rows="5" id="contMessage" runat="server" tooltip="enter your message here" />Place your default text here!!!</textarea


Thanks for the replies.

That does not work for me ? It just puts the text I have between the tags, next to the textarea field, not within it ?

I have found the following to accomplish what I'm after, but am a little confused still:

<asp:textbox Text="This text does show WITHIN the text box" textmode="MultiLine" columns="35" rows="5" id="contMessage" runat="server" tooltip="enter your message here" />Text here shows NEXT to the text box</asp:textbox

Is this just me ? Is there something going on in ther st of my form to have this effect?
you are confusing 2 things - textbox and htmltextarea - try this


<asp:TextBox id="Text1"
Text="This will show up in textbox"
TextMode="MultiLine"
Columns="50"
Rows="5"
runat="server"/>

In addition....
I'd like to bind some data collected from the querystring to the default content of the textbox I've tried:

<asp:textbox Text='<%# Response.Write(Request.QueryString("page")) %>' textmode="MultiLine" columns="35" rows="5" id="contMessage" runat="server" tooltip="enter your message here" /></asp:textbox>

but this produces an error:

Compiler Error Message: BC30518: Overload resolution failed because no accessible 'ToString' can be called with these arguments:
Remove the response.write.
thanks for that, but it still doesn't apear to work without the response.write :


<asp:textbox Text='<%#(Request.QueryString("page"))%>' TextMode="MultiLine" Columns="35" Rows="5" ID="contMessage" runat="server" ToolTip="enter your message here"></asp:textbox>

this does not display anything for me. the querystring does have the appropriate string, I have a basic text response.write on another part of the page that works fine, but not within the textbox.

and just to go back to the textarea option (I'm not sure of the difference yet, but I can google that), is it just me who can't get a default value in the textarea by adding text in between the tags ? I just get that text next to the text box, not within it ?

any further help appreciated.
Im not sure how to do it that way ( still lookin around) but you can do it this way..
add the text assignment to the page load...

Sub Page_Load(Source As Object, E As EventArgs)
text1.text=request.querystring("page")
End Sub

aslo here are some links i use..
http://samples.gotdotnet.com/quickstart/aspplus/doc/webcontrolsref.aspx

text is a .Net WEBcontrol
textarea is a HTMLcontrol

I also refer to this
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/cpref_start.asp

most of the controls you will use are under System.Web.UI.controls on left panel

HTH