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";
Related
I have a asp.net page. On one particular page i can add comments and when i add these comments they get saved to a db. When i want new lines the "\n" tagg is automaticly displayed when i look at it in my backend code.
So now when i get back a response i want to get my comments back but i want them to have breaking spaces on those particular places where the "\n" is displayed.
I tried this in my backend code(cs):
Text = text.replace("\n", "<br/>")
the result i get:
my test<br/> test text new line<br/>
My <br> tags get displayed as text.
To clarify i basicly wan to achieve this:
Why are my "<br />" tags getting converted to "<br />"?
But from the backend code in C#.
Ive been googling and haven't been able to find a answer to this.
If you are using ASP.NET WebForms and you are putting the text in a Label control, this is normal behaviour. The Label control html-encodes its text before rendering it.
The Literal control does not use html-encoding, so if you change your Label to a Literal, it should work.
If you are using ASP.NET MVC, try using #Html.Raw(Model.Text).
You need to create an HtmlString with your content before printing it. So your code would be like
var foo = new HtmlString(text.replace("\n", "<br/>"));
and you would print foo.
I have a textarea which is dynamicly changed in c# by doing this:
TextBox1.Text=(String)Application["chat"];
When the text is to big for the textarea, I would like the scroll to always be on the bottom. I have tried to do this in JS(jquery) with the change event.
$("#TextBox1").change(function () {
alert("alert");
// $('#TextBox1').scrollTop($('#TextBox1')[0].scrollHeight);
});
This is the code I use in Javascript. The problem is that the event does not occur, even if the text in the textarea is changed, which I have proven by debuging it with the alert which does not execute.
var textarea = document.getElementById('textarea_id');
textarea.scrollTop = textarea.scrollHeight;
Regarding this issue of your event not firing: If this is ASP.NET webforms, it's unlikely that the client-side id of your textbox (input) is TextBox1.
View source on the generated page and check what the ID renders as and update you JS accordingly.
You sould use this code to scroll down the textarea to the bottom:
$("#TextBox1").scrollTop($("#TextBox1").height());
If you would like to scroll down, when it's content changes, use this:
$("#TextBox1").change(function(){
$(this).scrollTop($(this).height());
});
I have embedded the TinyMCE in WebBrowser control and using it as a HTML WYSIWYG editor in my WPF app.
On the html page containing TinyMCE, I have:
function getContent() {
return tinyMCE.get('elm1').getContent();
}
I am calling this function on the WPF Button click as follows to obtain the content of TinyMCE in C#:
string editHtml = this.webBrowser1.InvokeScript("getContent").ToString();
However I want to set the content of the TinyMCE from the file before it loads, just to show text previously entered by the user, so that he/she can continue to modify the older content, instead writing the new content. How can I set the content of the TinyMCE through C#.
Thank you.
If you are saving the data in database then on edit, select the data from table, and assign the text to your text box..
txbItemText.Text = ListofItems[0].description;
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..
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.