WebBrowser Control insert word in caret position - c#

I have a WPF WebBrowser control where I need to insert some text (e.g. the selected item coming from a listbox). This way, if the editor contains "When " and the listbox contains "I, You, He" and I click on "You", the editor must contain "When you".
The code I'm using with the ActiveElement is working right now, but I'm finding some weird issues explained below.
HTMLDocument doc;
doc = webBrowser.Document as HTMLDocument;
doc.activeElement.innerHTML += " " + selectedItemFromListboxAsString + " ";
Then, the webbrowser gets appended the selectedItemFromListboxAsString properly, but when I continue typing after that, the new keystrokes go to the next line. In the sourcecode I can see that a </P> tag has been put after the selectedItemFromListboxAsString. No matter if I remove the </P> programmatically, it will insert the new typed content in a new line.
Is there any other way to achieve the same result instead of playing around with the activeElement?
Thanks in advance.

Related

Adding Text to a TextBox multiple times from code behind?

I'm still stuck... I'm using ASP.NET Web Forms.
I have a TextBox:
<p><asp:TextBox runat="server" TextMode="Multiline" ReadOnly="true" ID="txtBoxAlgo" rows="50" width="878px"></asp:TextBox></p>
I can only add text to this textbox Once from the code behind:
txtBoxAlgo.Text = ("My first line of text" + Environment.NewLine);
Now my issue is if I try to add text again to the textbox nothing happens. For example:
txtBoxAlgo.Text = ("First try! It works!");
txtBoxAlgo.Text = ("Nothing, not working :(");
Now I'm moving over a simple program I have made for a C# console and C# Windows forms. I'm experimenting with ASP.NET Web Forms.
I used Console.WriteLine(""); throughout my console application. The only way I can do something similar for webforms is make Labels and TextBoxes+Multilines for each instance I wanto output text to the screen, which at this point is not worth doing all that effort for each instance of Console.WriteLine.
I tried Response.Write, but first off this shows in the top left corner of the webpage and its all unformatted.
I have ran out of ideas and I can't find anything while searching. Can you add or Append text to a textbox? Can we use Response.Write in some type of way to format everything so a user can read it?
Use += instead of = when appending extra text to your Textbox
txtBoxAlgo.Text = "My first line of text" + Environment.NewLine;
txtBoxAlgo.Text += "First try! It works!";
txtBoxAlgo.Text += "Should work.";
Every time you use = when assigning a value to your Textbox, you are just replacing the previous value which is why only the last string is visible.
Or use StringBuilder:
StringBuilder sb = new StringBuilder();
sb.AppendLine("First Line");
sb.AppendLine("Second Line");
txtBoxAlgo.Text = sb.ToString();

How to avoid HTMLElement.InnerHtml property to change relative path url of HTML elements

In winforms, I have something similar to a HTML editor where a textbox control is used to write Html code and a browser control to display a preview.
I am trying to set an InnerHtml property of a HTMLElement with something like this:
htmlElement.InnerHtml = txtCode.Text;
The problem is when assigning a string like:
"<a href='/foo/bar.aspx'>Click Here</a>"
htmlElement.InnerHtml returns:
"Click Here"
The HTML code of the InnerHtml property is saved in a file and the file is used to render content in a website which renders and invalid link.
Is there any way to avoid this behavior of the InnerHtml property, without saving the text directly from the textbox?
My only idea is to delete the text node child of the <a> element, and then append a newly-created text node with your text. This might work around whatever process is interfering with your assignment of InnerHtml.
As a workaround you can try to put script block in your txtCode:
<script>document.write("<a href='/foo/bar.aspx'>Click Here</a>")</script>

Add html text to webcontrol Button in ASP codebehind file

So I am looking for a way to add HTML text to a programmatically created button, or some other control that would accept HTML text and have a on_click_event to capture.
I have a table that I am building dynamically in the codebehind file based upon steps (attendance to a lesson, and the taking of a quiz) that are recorded as complete or incomplete in the database. If a step was not done, I entered a button with a simple X
If the step was done, I just added HTML month and day to the cell.
Attendance to the lesson is a step that can be repeated, so I wanted to make the lesson button look like the styling of a completed quiz, but still capture a 2nd+ attendance point with a click_event.
I am stumped in making a dynamically created button work with my HTML styling. My text style on an ASP.net webcontrol button in a codebehind file is being interpreted as literal text on that button.
Here is the code that fills the cells, either creates the buttons, or adds the text
if(lesson_completed == true)
{
DateTime date = Convert.ToDateTime(lesson);
Button markNewDateComplete = new Button();//System.Web.UI.WebControls.Button Class
markNewDateComplete.Text = "<font style='font-size:50%;color:silver;'>" + date.ToString("MMMM") + "</font><br><font style='color:light gray'>" + date.ToString("dd") + "<font>";
markNewDateComplete.CssClass = "mybtn";
markNewDateComplete.CommandName = lesson_detail_id.ToString();
markNewDateComplete.Click += new EventHandler(this.NewDateLesson_Click);
tc2x.Controls.Add(markNewDateComplete);
}
else
{
DateTime date = Convert.ToDateTime(quiz);
tc2x.Text = "<font style='font-size:50%;color:silver;'>" + date.ToString("MMMM") + "</font><br><font style='color:light gray'>" + date.ToString("dd") + "<font>";
}
Here is what the output looks like on-screen
There are many pages in SO that give insight how to do this in the regular ASPX page, but none that I could find to do this programmatically in a ASPX.cs codebehind file.
In programming there is always another way to do something; so I am looking for a way to add HTML text to a programmatically created button, or some other small control that would accept HTML text and have a on_click_event that I could use as a method back to SQL.
Example of what I want, but this is not for the codebehind file.Font awesome inside asp button

Get HTML text from page after scrolling

I am trying to capture HTML text from a page, in the case of this website more info is loaded dynamically as you scroll. So it is easy enough to capture the first page of text, but have do I get the next. I have the auto scroll part worked out fine like this:
string Java = "javascript:window.scroll(0,";//Builds up logic so screen will scroll by one page each time
string JavaScroll = Java + Scroll + ");";
webBrowser1.Navigate(JavaScroll);
Scroll += 3050;
String Morefriends = webBrowser1.DocumentText;
The problem is, even when I scroll, the Morefriends HTML text still remains the same as the first page, like it is not updating to reflect the HTML text from the new page that was just scrolled to.

Text property on selenium web element is empty even though I can inspect it with Visual Studio

Without posting pages of C# code and markup, has anyone got a reason why this code
var link = _driver.FindElement(By.Id(field + "Field"));
var id = link.GetAttribute("id");
var text = link.Text;
given this markup
<a id="ForenameField" href="/MyUrl/MyFolder/MyId">3 errors</a>
Assigns an empty string to the text variable, but if I put a breakpoint on the second or third line and inspect the link variable, I can see the inner text of the element against the Text property on the inspector, it reads "3 errors", but the value of text is an empty string. It is not hidden, I can see the text if I add a watch or use quickview, any ideas?
Ok, it's my bad. Using jquery to toggle class on the div that contains the html in the question, meant that although users see the div appearing, the class that hides the div is still in the tag. A bit like this
<div class="hideThis showThis"><!-- my elements /--></div>
This makes it so that Selenium is right not give me a text value. It is strange however that the Visual studio debugger thinks that there should be a value. Visual Studio seems to go with what I can see, but Selenium is more pedantic about the hideThis class being there.
I go with the idea that if you can't see it you can't interact with it, so it is worth looking up the html graph from the element you expect to have a value to see if any class is present which would hide your element.
Feel free to recommend that I delete this rather obvious wisdom.
I know this was posted over a year ago, but I had this exact problem too and came across this thread. I was able to solve it by just waiting for the DOM to load--some elements aren't visible until the DOM is updated. So just putting Thread.Sleep(6000) or whatever after navigating to the page got it to work for me.

Categories

Resources