From right to left in textBox (asp.net)? - c#

How do you achieve this. and i set some text inside the textbox. i want it to be lower in opacity and disappear whenever the user clicks on the textbox to begin typing (like in iphone interface). How do i achieve this?

You can use ajax water mark control and jquery autoclear plugin

In html5 a new attribute introduced placeholder, you can make use of this attribute
In aspx markup your TextBox is
<asp:TextBox runat="server" ID="TextBox1"></asp:TextBox>
Then in Page_Load set the attribute.
TextBox1.Attributes.Add("placeholder", "you faded text like in iphone");
Edit 1:
You are right, for right to left text you need to add another dir attribute on TextBox
<asp:TextBox runat="server" ID="TextBox1" dir="rtl"></asp:TextBox>

For the watermark there is the HTML5 placeholder attribute or you could also use a jquery watermark plugin if your site is not yet HTML5. For right to left you may take a look at the following article.

Do it using java script. See the code below:
set a default text for your textbox.
for clearing the textbox call the following function on textbox onblur event
function ClearSearchText() {
var searchUserName = document.getElementById('<%=txtUserName.ClientID%>');
if (searchUserName.value = searchUserName.defaultValue)
{
searchUserName.value = "";
}
return false;
}
for setting call the following function on textbox onFocus event
function SetSearchText() {
var searchUserName = document.getElementById('<%=txtBoxID.ClientID%>');
if (searchUserName.value == "")
{
searchUserName.value = searchUserName.defaultValue;
}
}
return false;
}
do the search in button postback after checking the default text and also set the textBoxID.Text="your default text".
hope this will help..

Related

create label and put this control in href tag based on certain conditions

Can any one tell me how to create a label and put it in href tag using c# code.
this is because, I need to use a single asp label control for both as label and link label.
So I want it to be programatically set based on certain parameter values. and this is ausercontro as well. Note: I cannot change the logic because this is client's requirement
regards,
Sivajih S.
Use asp:HyperLink control !
In your aspx ,
<asp:HyperLink ID="yourHyperLink" runat="server" >
</asp:HyperLink>
In your cs ,
if(....yourCondition...)
{
yourHyperLink.Text = "YourText";
yourHyperLink.NavigateUrl = "YourURL";
}

Textbox Onclick = clear value , on exit from textbox set the default value?

I have this in the
<asp:TextBox ID="svv" OnClick="this.value=''" runat="server">Hello...</asp:TextBox>
OnClick="this.value=''" // On mouse click in textbox it will deleted the text.
How can I set something like
Unclick"this.defautlvalue"; // something like this.
So, when I click the control it will clear the value, if I exit from the control (for example, clicking another textbox) it will return the default value of the textbox.
Thanks
Specifically with C# .NET WebForms you have a few options.
You can go completely front-end with jquery by doing something like this:
$('selector').blur(function() {
// Make sure you do some validation so it doesn't clear everytime
$('selector').val('My Default Text');
});
Or, if you are using the AJAX Control Toolkit, you can simply use The textbox Watermark Control, which will do exactly what you are talking about just by setting a few properties.
You can also go straight javascript like #m.edmondson explain in his answer.
I think you're looking for onBlur:
<input type="text" id="fname" onblur="upperCase()">
This will call upperCase() when the user leaves the box.
You can attach to the client-side onblur event which is called when the focus of your control changes.
Also worth storing the default value in an attribute on the input so you can refer to it in blur event. Believe in ASP.NET web forms you've got to add that attribute in the code-behind though.

How to enter the text in textarea using mshtml

I am facing a problem I have to fill a TextArea programatically. I used the following code.
System.Windows.Forms.HtmlElement reviewText = myDoc.GetElementById("review-text");
reviewText.InnerText = Program.mainForm.richTextBox1.Text.Trim();
This works fine and it sets the text in TextArea control. The problem that I am facing is that this text appears light gary. When the user clicks over this text it disappears. It happens only on first click.
So I tried the following code to first click this box and then set the text.
reviewText.InvokeMember("click");
And then tried to set the text in the TextArea but got the same behaviour.
I dug into the source of the page and found some script associated with this TextArea.
Source of TextArea
<textarea onkeyup="javascript:yelp.ui.widget.countChars(this.form.comment,5000);" onkeydown="javascript:yelp.ui.widget.countChars(this.form.comment,5000);" name="comment" id="review-text" class="form400" rows="8" cols="40" style="height: 86px;"></textarea>
Script associated with TextArea
<script type="text/javascript">
var bizName = "Geary Club";
if ($('review-text'))
{
new yelp.ui.widget.DefaultValueTextField($('review-text'), 'Please write here');
}
Will somebody suggest to me how to insert the text into this TextArea control?
Is this an ASP.NET web app? If it is, the System.Windows.Forms.HtmlElement is a problem. What I think is going on is that you need to somehow disable the new yelp.ui.widget.DefaultValueTextField JavaScript code when you're pre-filling the TextArea. Are you able to use an ASP.NET TextBox control with the multi-line properties set? If so, I'd use that and change your top code to just set the .Text property of the Textbox control.
Either way, in the Javascript (if this is jQuery), I'd do this:
if ($('review-text') && $('review-text').val() != "")
{
new yelp.ui.widget.DefaultValueTextField($('review-text'), 'Please write here');
}
That way the default value is only set if there is no text.
I changed the HTML of text area.And remove the class="form400 default_valued_text_field" attribute from its html.
myDoc = this.webBrowser1.Document;
myDoc.GetElementById("review-text").OuterHtml = "<textarea id=\"review-text\" onkeyup=\"javascript:ui.widget.countChars(this.form.comment,5000);\" onkeydown=\"javascript:ui.widget.countChars(this.form.comment,5000);\" name=\"comment\" rows=\"8\" cols=\"40\" style=\"height: 86px;\"></textarea>";
After that i set the inner text of this element
myDoc.GetElementById("review-text").InnerText = "Testing";

Removing default text from text box

I got this Text box with default value as "First Name" ..Now when I click inside this text box to enter name , this value "First Name" keeps on displaying. What I want is to the text box to clear as soon as I click inside it. What property do I need to set in mt textbox tag ?
[Edit]
ok anything from Telerik that I can use to do that ?
There is not out of the box functionality in TextBox that will accomplish this, but the ASP.Net Ajax Toolkit has a Watermark Extender that will do everything you want.
I have used both, but now personally use a jQuery Watermark Plugin
Either will work just fine, choose based on your needs.
According to the Telerik docs you just have to set the EmptyMessage property on their TextBox control. Demo Page Here
In the code behind, on Page Load you can add the following code to achieve this
TextBox1.Attributes.Add("onClick", "javascript:if(this.value=='First Name'){this.value='';}");
You can use the method suggested by #Josh. If you do not want to use Ajax Toolkit controls or JQuery you could write it on your own using Javascript. Write a function which gets called when the foucs is received by the textbox control. I thik the function is called onfocus or just focus in Javascript.
Hi I just wrote this small function which will achieve your desired result
function clearInputBox(x,prefil){
if(x.value == prefil){
x.value = '';
}
}
Your input box looks like this
<input type='text' value='First Name' onfocus="clearInputBox(this,'First Name')" />
May be this will help you
Taking Shobans advice one step farther, you could add something like this to your Page subclass
protected override void OnInitComplete(EventArgs e)
{
string jsString = "javascript:if(this.value=='" + TextBox1.Text + "'){this.value='';}";
TextBox1.Attributes.Add("onFocus", jsString);
base.OnInitComplete(e);
}
What this will do is, it will always consider that default string is the one this controll contains at esign time (the initial one in your .aspx file), so you wont have to manually change it in codebehind every time you change your .aspx. Remember, that OnIinitComplete fires before any viewstate or postback data has been applied, but after the controlls on your page have been set to their default values.
P.S. As anishMarokey pointed, use onFocus vs onClick, since fields can gain focus without clicks via Tab key.

Text inside a text box that must be cleared when clicked

I am using ASP.NET.
I am not sure what you will call this but I would like text to be displayed in my text box called txtName. So when the form load the text box will have faded text that will say "Required". Then when the user click inside the text box i want the user to place a value inside the text box.
Is this possible in ASP.NET? If so, how can this be done????
If you can use the MS AjaxControlToolkit, there is a watermark extender that will do precisely that.
MS Ajax TextBoxWatermark Extender
If not, you can probably find a jQuery plugin or roll your own javascript. Using server-side events will probably not do what you want.
onClick is a client side event. onFocus may be better. You can use
txtName.Attributes.Add("onFocus", "this.value = '';this.style.color = 'black';")
Put it in the Page's PreRender event.
Here's a great jQuery plug-in:
http://www.mudaimemo.com/p/formnotifier/
Try something like this:
<asp:TextBox ID="txtName" runat="server"
onclick="if (this.value == 'Required') {this.value=''}"
Width="275px">Required</asp:TextBox>
If using the Ajax Toolkit is an option or makes sense there is a tool in the tool box called textbox watermark (see http://www.asp.net/AJAX/AjaxControlToolkit/Samples/TextBoxWatermark/TextBoxWatermark.aspx)

Categories