Getting textarea content after setting it with innerHtml - c#

I'm working on something in asp.net that requires allowing the user to change the contents of information within a text area. What happens is that I set the text area contents by getting information within an external .txt, .html, or .rtf file and then set it into the text area using innerHtml.
As for retrieving the data I'm also using innerHtml to do that in the .cs file behind the page. The problem is that when I try doing this, I get back what was previously set even if I had replaced every line of text. For example, if I had originally set "This is the initial text" first then replaced it all with "New text" after, innerHtml will just give me back "This is the initial text".
Is there another way to get the new text or a way to get innerHtml to do what I want?
Edit: Forgot to include code.
On the aspx side of the code, I just have a simple text area,
<textarea id="TextArea1" cols="80" rows="10" runat="server"></textarea>
and on the aspx.cs side,
protected void Page_Load(object sender, EventArgs e)
{
ces = new ContentEditorService.ContentEditorService();
strRtfDir = Server.MapPath("Testfile.rtf");
string strContents = ces.loadEditorContents(strRtfDir);
TextArea1.InnerText = strContents;
}
where ces loads a separate .cs file that does the loading and saving of the text.
As for getting the content, I'm using
protected void Button1_Click(object sender, EventArgs e)
{
string strTxtArea = TextArea1.InnerHtml;
System.Diagnostics.Debug.WriteLine(strTxtArea);
//ces.saveEditorContents(strContents, strRtfDir);
}
which gets the contents and prints them out in the debug window of Visual Studio for now just to see if I managed to get the changed text.

Change your code to this:
if (!IsPostBack)
{
ces = new ContentEditorService.ContentEditorService();
strRtfDir = Server.MapPath("Testfile.rtf");
string strContents = ces.loadEditorContents(strRtfDir);
TextArea1.InnerText = strContents;
}
Because Page_Load will always be excuted even you clicked the Button1.

You can use jquery to get the value inside the text area
var text = $('#yourTextAreaId').html();
or
var text = $('#yourTextAreaId').val();

Related

Prevent WebBrowser Control from automatically adding <A> Tag when contentEditable is set

I use WebBrowser control and have text inside like:
some text https://www.example.com some text.
This is my code the way I added the text and enabled the web browser to edit contents:
public partial class Form1 : Form
{
private HTMLBody _body;
public Form1()
{
InitializeComponent();
webBrowser1.Navigate("about:blank");
}
private void webBrowser1_DocumentCompleted(object sender,
WebBrowserDocumentCompletedEventArgs e)
{
_body = ((HTMLBody)((HTMLDocument)webBrowser1.Document.DomDocument).body);
_body.contentEditable = true.ToString();
_body.innerHTML = "some text https://www.example.com some text";
}
}
If I run it and changed part of link (for type something else instead of 'example.com' and lost focus) then it automatically adds tag <a> around my link. You can see it in innerHTML property. But it's wrong for me.
Is there a way to avoid to do this behavior?
Thanks a lot!
You can turn off auto-dettecting url in document. To do so, in DocumentCompleted evnet after you set the new content for body, add this line of code:
webBrowser1.Document.ExecCommand("AutoUrlDetect", false, false);
Also you can make the content editable without adding reference to mshtml.dll. To do so you can simply use:
this.webBrowser1.DocumentText = #"<HTML><BODY contentEditable=""true""></BODY></HTML>";

Converting HTML Code in TextBox to display Correctly (after input through a keyboard made in XAML)

I've created a Keyboard for my WPF application in XAML with the functionality of it done in the code behind C#. As of now, any key clicked (or touched), will display the respective key in a TextBox (as expected. However, when handling the HTML Code, the HTML Code gets displayed.
For reference, the button click:
private void btnQUOTATION_Click(object sender, RoutedEventArgs e)
{
HandleKeyboard(""");
}
The HandleKeyboard method:
private bool current spot = false;
private void HandleKeyboard(string key)
{
//Ternary operator
string valueSoFar = currentSpot ? txtBoxTest.Text : "";
string newValue = valueSoFar + key.ToString();
txtBoxTest.Text = newValue;
currentSpot = true;
}
Everything is fine and dandy with the other keys presented, except for quotation marks and apostrophes.
What I'm expecting in the TextBox txtBoxTest when the button for quotation marks is clicked (") is quote marks to show in the text box as quotation marks...instead of the HTML code it is showing now.
Since HandleKeyboard() cannot accept (""") or ("'") ... Is there a way to do this?
You could look to use the Encode and Decode HTML. You will need to use System.Web namespace.
e.g.
string key= "This is a "";
string deCodedKey = System.Web.HttpUtility.HtmlDecode(key);
If you put a breakpoint over the DeCodedKey line you should see it change back to ""

How to set focus at the end of textbox while typing?

I have a textbox with a live search function. It is working all good except one problem. If I type any characters on it, it just loses its focus. If I set textbox.Focus(), the cursor goes at the beginning of the textbox.
I have tried most of solutions on the internet. Please check my codes below.
asp:TextBox ID="searchCompany" runat="server" Text="" CssClass="searchCompany" AutoPostBack="true" Width="190px" OnTextChanged="searchCompany_TextChanged"></asp:TextBox>
In page_Load
protected void Page_Load(object sender, EventArgs e)
{
//ScriptManager1.RegisterAsyncPostBackControl(Menu1);
menuDisplay();
searchCompany.Attributes.Add("onkeyup", "setTimeout('__doPostBack(\\'" + searchCompany.UniqueID + "\\',\\'\\')', 0);");
//searchCompany.Attributes.Add("onfocus", "javascript:setSelectionRange('" + "','')");
//searchCompany.Focus();
}
and I have tried javascript as below
<script type="text/javascript">
function setSelectionRange() {
var inputField = document.getElementById('searchCompany');
if (inputField != null && inputField.value.length > 0) {
if (inputField.createTextRange) {
var FieldRange = inputField.createTextRange();
FieldRange.moveStart('character',inputField.value.length);
FieldRange.collapse();
FieldRange.select();
}
}
}
</script>
I have tried to put codes on a method "searchCompany_TextChanged" which is calling if user type any characters on a textbox everytime however it is not working as well.
I saw other solutions with using Textbox.Select() but System.Windows.Control is not working in asp.net i guess.
Any idea??
There's a very simple trick that's worked for me. Basically, set the text value of the of input to itself to its own text value, and that will move the cursor to the end of the text. Then just focus it. This code uses jQuery to demonstrate that, but you should be able to do that in straight JS as well.
HTML
<input type="text" id="focusText"></input>
<button id="focusButton">Set Focus</button>
JavaScript
$("#focusButton").click(function() {
var text = $("#focusText").val();
$("#focusText").val(text).focus();
})
Here's a non jQuery example of the JavaScript, HTML should be the same:
document.getElementById("focusButton").onclick = function() {
var inputElement = document.getElementById("focusText");
var text = inputElement.value;
inputElement.value = text;
inputElement.focus();
}
Here's a fiddle demonstrating the non-jQuery version of the code: http://jsfiddle.net/C3gCa/

How to set dynamic parameter values to custom control from web page

I have one image button in the custom control like below.
public string SearchTableName = string.Empty;
public string SearchColumnName = string.Empty;
public string SiteURL = string.Empty;
ImageButton _imgbtn;
protected override void OnInit(EventArgs e)
{
_imgbtn = new ImageButton();
_imgbtn.ImageUrl = ImageURL;
_imgbtn.OnClientClick = "ShowSearchBox('" + SiteURL +"/_layouts/CustomSearch/SearchPage/Searchpage.aspx?table_name=" + SearchTableName + " &column_name=" + SearchColumnName + "')";
}
On Clicking of the image button I want to migrate to the another window which is a popup. For this I written a javascript function. I am setting the SearchTableName and SearchColumnName in the web page in which we are consuming this custom control like below. Before consuming I registered this control in web page with register tag.
<ncc:SearchControl runat="server" ID="txtSearchControl" /> In code behind file of this webpage I am using following code to set the values.
protected void Page_Load(object sender, EventArgs e)
{
txtSearchControl.ImageURL = "_layouts/Images/settingsicon.gif";
txtSearchControl.SearchTableName = "Employees";
txtSearchControl.SearchColumnName = "LastName";
txtSearchControl.SiteURL = "http://Sp2010:8787";
}
Now coming to the problem, when I click the image button the SearchTableName and SearchColumnName values are not coming. I think I am calling OnClientClick function, thats why the values are not being set. But how to set the values for the custom control based on the values setting in the webpage. If I use the Click function will it serve my purpose? If so, how to call that javascript function from this click event.
Finally got solution. I am initializing the values in the page init method in the custom control. Thats why the values i am setting in the visual webpart page are not being captured. Now I changed the initializing the values in CreateChildControl method. Now it works perfectly. Thank you.

How do I add a reference to a generated TextBox in the control tree?

I have an input field that is appended to the HTML in a div.
I need to have this input field bound to the ctrlmenuItems in the system but I don't know how.
I have this function which is the one calling the object:
protected void btnSave_Click(object sender, EventArgs e){
int Id = int.Parse(Request.QueryString["edit"]);
DA.page itmToEdit = DA.page.StaticGetById(Id);
itmToEdit.title = ctrltitle.Text;
itmToEdit.menuItems = ctrlmenuItems.Text;
itmToEdit.content = ctrlcontent.Text;
itmToEdit.Update();
UpdateGUI();
pnlEditArea.Visible = false;
}
I tried using:
protected global::System.Web.UI.WebControls.TextBox ctrlmenuItems;
which just creates a null reference.
I tried calling this onInit():
TextBox ctrlmenuItems = new TextBox();
ctrlmenuItems.ID = "ctrlmenuItems";
ctrlmenuItems.Text = "Enter your name";
this.form1.Controls.Add(ctrlmenuItems);
with no luck.
The appending is done like this:
litMenu.Text += string.Format(#"<div>
<a class='button' href='?del={1}'>Slet</a>
<input name='ctrlmenuItems' type='text' id='ctrlmenuItems' value'{0}' />
</div>",
itm.menuItems,
itm.id);
I need to be able to click "save" and it passes on the value from the textField to dataAccess
but I can't figure out how to bind this "virtual" textbox correctly.
input tags with a name attribute will be stored in the Request.Form array when the page posts back.
Whether you add the TextBox control manually, or simply insert the corresponding HTML (which I would not recommend, building HTML via string concatenation makes it very easy to yield invalid HTML), you can retrieve the value when your button is clicked using the following:
string value = Request.Form["ctrlmenuItems"];

Categories

Resources