Adding Text to a TextBox multiple times from code behind? - c#

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();

Related

Multi Line Text Box Displays HTML Elements When Going Back To Page

I have an asp.net web form which on one page has a multi line textbox. When the user types in this field and continues the details are displayed on a confirmation page and displays as the user entered
Example
This is the first line
This is the second line
But when I click my edit button (which directs me back to my page) my textbox displays as
This is the first line<br /><br />This is the second line
I want it to keep its styling but don't know how to do this. The details are stored in the session.
Code behind
protected void Step07SubmitButton_Click(object sender, EventArgs e)
{
Session["Step07OtherDetailsField"] = Step07OtherDetailsField.Text.Replace("\r\n", "<br />");
Response.Redirect("/Quotation/pg3.aspx");
}
I tried the following in my Page_Load
Step07OtherDetailsField.Text.Replace("<br />", "\r\n");
and also
Step07OtherDetailsField.Text.Replace("<br />", Environment.NewLine);
But for some reason when I debug it, it says that my .text is empty but the previous wording is actually still displayed in the field and the <br /> is also still displayed.
You don't need to replace anything. You just store value in session and display it to any textbox, it will maintain \r and \t.
If you want to display result in span then display result to textbox with multiline then apply css for no border,etc.

WebBrowser Control insert word in caret position

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.

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

How to make string bold in code behind

Is there a way to make a string bolder in code behind using C# .NET?
I tried:
string TypeofDay = "<span style='font-weight: bold'>Type Of Day</span> ";
txtbox.Text = TypeofDay + ": " + "Delivery Day"
I am concatenating TypeofDay(bold) and "Delivery Day" to display in a textbox.
You can't make some bits bold and some bits not bold, in an <asp:TextBox>.
You can't make text bolder by enclosing text value in tags. You must change attribute of a control that displays that text for example by setting its CSS class or changing code-behind property:
txbSendMessageBody.Font.Bold = true;
PS. I am concatenating few bold strings and some other strings to display in a textbox.
A HTML text box does not support this. ASP.NET (usually) generates HTML; if HTML does not support this, you cannot solve it from the server side.
A bit hacky alternative can be to use Unicode bold characters
http://qaz.wtf/u/convert.cgi?text=Type+Of+Day
txtbox.Text = "𝗧𝘆𝗽𝗲 𝗢𝗳 𝗗𝗮𝘆: 𝖣𝖾𝗅𝗂𝗏𝖾𝗋𝗒 𝖣𝖺𝗒"
You could layer a div on top of the textbox. Since the text formatting would change anyway once the user started typing, you can just hide the div once focus is given to the div, thus showing the text box. If nothing is entered and focus is lost, show the div again.
TextBox, no, however you could use a label.
myLabel.text = "<b>bold text</b> normal text <u>underlined text</u> <span style='font-size:Large; color:Red'>Big red text</span>";

New Line textbox

I have a comment box that users can fill and saving the text in a MySQL database:
When a users fills text in my textbox with a backslash (\n) it shows in the text box without the new line.
For example, the user enters:
Hi everyone!
Me
The Textbox displays:
Hi Everyone!Me
What am I doing wrong?
Use Environment.NewLine instead of "\n" (or at least "\r\n", since this is what Environment.NewLine contains on non-Unix platforms anyway).
Also, make sure your TextBox's MultiLine property is set to true.
myTextBox.MultiLine = true;
myTextBox.Text = "hi," + Environment.NewLine + "ALL";
Did you set the Multiline property?
textBox.Multiline = true;
try this : when extracting from database use this :
str_replace("\n",'<br />',$databasefield)
it`s working for me:)

Categories

Resources