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.
Related
I have a WebForm ASP label and button. I am setting the label's value on page load. For example, the label text on page load is 2 items selected. This comes from the database. Then if the user changes the selection then it counts the selected values by jQuery and sets the text as 5 items selected.
When I click on the submit button to save changes, again it resets to 2 items selected. I didn't use an update panel. I don't know what is going on here. Can anyone please explain this scenario?
$("#lblCount").text($('#grdProducts').find('input#chkSelect:checked').length + ' Complementary Products added');
C# on page load:
lblCount.Text = ComplementaryproductCount.ToString() + " Complementary Products added";
I do not understand why the label text is changed on button click. I couldn't find anything while debugging too.
Thanks
When you set lblCount.Text in your code, that value is set into the ViewState of the page... that means when your page is posted back to the server (to handle an event, etc) ASP.Net knows what lblCount.Text was originally and can re-render the HTML with the same value.
As part of that post-back to the server, the browser will send back that ViewState along with any input control values (things like textboxes, dropdowns, hidden field).
What it does NOT do is post-back any changes you might have made to the elements on the page via things like jQuery (other than input controls I mentioned above).
The result is that although you've changed the element on the screen, the server knows absolutely nothing about that change, and it will re-send the original HTML for the label back to the browser.
Your only option is to do something as suggested by #John in his comment... you need to store the fact the element has changed in an input, and then use that.
For instance...
<asp:Label runat="server" id="lblCount" />
<asp:HiddenField runat="server" id="hdnCount" />
function updateCount(newCount) {
$("#<%=lblCount.ClientID%>").text("Count: " + newCount.toString());
$("#<%=hdnCount.ClientID%>").val(newCount.toString());
}
Then in your code-behind you can have...
if (!Page.IsPostBack)
{
var count = 1;
lblCount.Text = String.Format("Count: {0}", count);
hdnCount.Value = count.ToString();
}
else
{
lblCount.Text = String.Format("Count: {0}", hdnCount.Value);
}
I am working on asp.net mvc. I am trying to save form-data to the database. I have the form with one textarea where user can type the text or he can paste the text from somewhere. Here i need to save the text of textarea into database. and then i need to show the textarea content what he saved,
#using (Html.BeginForm("myaction", "mycontrolller", FormMethod.Post,
new { id = "addform" }))
{
<textarea id="note" name="note"></textarea>
<input type="submit"/>
}
Now i am able to get the data that user entered into my post action from view, But the text doesn't contain any line breaks or carriage returns though he pasted multiple paragraph text. While i am in debugging and watch the text content in Text Visualizer, it shows me with exact format. But when i save it into the database. It save it as single line of text and so shows me as a single line of text, when i displayed.
I have tried the following code,
obj.Notes = obj.Notes
.Replace(Environment.NewLine, "<br />")
.Replace("\r\n", "<br />");
But it doesn't resolve my problem. It still displays single line of continuous text though it have multi-paragraph text.
Yes I've got solution to my answer after a small trial, just displayed the content in <pre> tag instead of <p>
Replaced from:
<p>Data from Database</p>
to
<pre>Data from Database</pre>
I have a FreeTextBox control in my aspx page.
First, the client types the text into the FreeTextBox and click button Save to write the text into Database (sql server 2008).
Then, in Edit function, the text is loaded from Database into FreeTextBox again for Editing.
After Editing, client clicks OK to write the new text into Database but the new text isn't allowed be null or empty.
Take a look at my simple code:
public void btn_OK()
{
if(string.IsNullOrEmpty(FTB.Text))
Labelerror.Text="The text cannot be null or empty.";
else
{
...write new text into Database...
}
}
Building: when I clear the old text and click OK, the program passes the if statement and does the codes inside else statement (write new text into Database).
Try to debug, I clear the old text and let the FreeTextBox empty but the FTB.Text= "<p class=\"MsoNormal\"><br></p>"
Where is the <p class=\"MsoNormal\"><br></p>from???
As recommended, I use HttpUtility.HtmlDecode to decode that html tag:
string text= HttpUtility.HtmlDecode(FTB.Text);
if(string.IsNullOrEmpty(text))
But nothing change: text= "<p class=\"MsoNormal\"><br></p>"
Help!!! How can I remove that html tag to check if the FreeTextbox is null or empty.
I have three RadListBox controls on a page. When a user drags an item from the first listbox to the second listbox, I need the third one to automatically generate a textbox.
I've figured out how to add a regular HTML textbox with JavaScript. However, I'm not able to access this in the code behind page because adding a runat=server field causes an error. Here's the client JavaScript code I'm using to insert a textbox:
//Add textbox to 3rd listbox
var listbox = $find("<%= ListBoxThree.ClientID %>");
listbox.trackChanges();
var textboxItem = new Telerik.Web.UI.RadListBoxItem();
textboxItem.set_text(item.get_text()); //sets the ID
textboxItem.set_clientTemplate(" <input id=\"#= Text #\" type=\"text\" /> ");
textboxItem.bindTemplate();
textboxItem.set_value
listbox.get_items().add(textboxItem);
listbox.commitChanges();
If I try adding runat=server I get the following error:
Parser Error
Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.
Line 70: textboxItem.set_clientTemplate(" <input id=\"#= Text #\" type=\"text\" runat=\"server\" /> ");
Parser Error Message: The server tag is not well formed.
When the user hits the Save button though, the item's Text and Value fields do not get updated with the text they've entered.
protected void SaveButton_Click(object sender, EventArgs e)
{
foreach (RadListBoxItem item in ListBoxThree.Items)
{
string value = item.Value;
string text = item.Text;
}
}
If someone could point me in the right direction I'd really appreciate it!
I am working on designing a user profile web page in asp.net using c#.
I first load the values of text boxes from database and put them in the text box:
txt_Name.Text = "somestring";
The user can then change the text in the text box to modify their profile.
However when I read txt_Name.Text it shows me the "original" value instead of what the user entered.
More clearly:
First I set the value of a text box to something:
txt_Name.Text = "somestring";
Then the user changes the value of the text box to something else in the gui
Then I read the value of text box:
Response.Write(txt_Name.Text);
In 3 the value is the one from 1 instead of the one from 2
It sounds like you aren't checking the Page.IsPostBack property (http://msdn.microsoft.com/en-us/library/system.web.ui.page.ispostback.aspx) when you are setting the initial textbox value, so it is always being set no matter how the page is invoked.
private void Page_Load()
{
if (!IsPostBack)
{
txt_Name.Text = "somestring";
}
}
Its all in the page life cycle have a look at this page
http://msdn.microsoft.com/en-us/library/ms178472.aspx