There seems to be a problem with the ASP textbox control when set to multiline and serving the page as xhtml. The project I am working on uses content negotiation to serve asp pages as application/xhtml+xml to browsers which support it. The problem is when asp textbox renders a textarea to the page, it explicitly prepends a newline to the text. Reflection of the textbox's render method looks like the following:
if (TextMode == TextBoxMode.MultiLine)
HttpUtility.HtmlEncode(Environment.NewLine + this.Text, (TextWriter) writer);
When firefox and opera are served this with xhtml content type, they interpret the newline as part of the text in the textarea and so I get extra newlines at the beginning of my text areas.
I could subclass textbox and override render, but that seems like a bit of overkill to correct something like this. Is there another way to correct this? And does anyone know why asp textbox does this anyway?
An alternative to subclassing is to use control adapters, or to write the <textarea by hand and get ASP.NET to generate the control name attribute for you.
I suspect ASP.NET WebForms does this simply because of a short-sight. The future is MVC anyway, so don't expect this to be changed any time soon. I suspect the original purpose of the newline is to give the textarea a "value" rather than nothing (thus making the textarea "successful" in HTML forms parlance).
This isn't the only odd behaviour you'll see in ASP.NET. The atrocious HTML formatting of <head runat="server"> is also on the list.
Related
I have a front end written in html that I am converting to asp, and many of the controls have names with "-" in them. This is causing crazy headaches, as there is no time to rename everything, and the ctrl-f and replace somehow breaks my css. Is there any way to access these controls in the code behind while they have the dashes? I have tried the code below.
//Can find when there is no dash in it, but that breaks the css after find/replace of full solution
HtmlGenericControl body = (HtmlGenericControl)this.Page.FindControl("page-list");
body.Attributes.Add("class", GlobalVariables.webAppSkin);
//I have also tried this, but logout stays null
WebControl logout = (WebControl)Page.FindControl("logout-link");
This is the html control:
<body id="page-list">
Sorry, that's not gonna happen.
You cannot have an element with an id containing "-", and still be a runat="server" ASP.NET control.
Microsoft's docs about the control's IDs states:
http://msdn.microsoft.com/en-us/library/system.web.ui.control.id.aspx
Only combinations of alphanumeric characters and the underscore character ( _ ) are valid values for this property. Including spaces or other invalid characters will cause an ASP.NET page parser error.
If you tried adding runat="server" to the body tag you showed: <body id="page-list">, it would give you the following line in aspx.designer.cs:
protected global::System.Web.UI.HtmlControls.HtmlGenericControl page-list;
Which is obviously throwing an exception on C# syntax.
<body id="page-list"> is not a HTML Control (i.e. an instance of (a subclass of) System.Web.UI.Control because it doesn't have the runat="server" attribute. If you were to add runat="server" then you would get a JIT compile-time error message informing you that "page-list" is not a valid identifier.
ASP.NET Web Forms 4.0 added the ClientIDMode attribute, however it doesn't allow you to set a completely custom ID attribute value. There's no easy solution for this without using <asp:Literal> or implementing your own control (or my preferred option: switching to ASP.NET MVC).
You can access controls in code behind with their ID if you write runat="server". Here is the example for your case
<body runat="server" id="testID">
In code behind you can access it like this:
body.Attributes.Add("class", value);
I have a bit of code in my markup like this :
<% Response.Write(((String)objRow["Post"]).Trim()); %>
The value in objRow["Post"] is HTML markup - similar to this:
<p><span style="color: #ff0000;">asdsada</span></p>
The content of which was created using tinymce. I now want to render the resultant html - basically a view function of a previously created save function.
At the moment, my markup literally spits out the HTML you see there onto my website - but what I really want is a red line of text saying asdsada.
Please help
it sounds like the value you are trying to display is already html encoded. try this:
<%= HttpUtility.HtmlDecode((string)objRow["Post"]) %>
My version of Asp.net is little bit higher, but this might help someone, who uses .net Core 2.0 MVC.
I am using the following approach for rendering html in my Asp.Net Core 2.0 MVC project (the html is created via Tiny MCE and saved in the database, after that it is rendered on the Razor View).
Please, try these two steps:
1) include System.Net in _ViewImports.cshtml:
#using System.Net
#addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
2) render the html using HtmlDecode in your View.cshtml:
#Html.Raw(#WebUtility.HtmlDecode(Model.MyHtmlData))
This works for me, hope it helps!
But without HtmlDecode, Html.Raw displayed MyHtmlData with html tags (didn't render html, just displayed it). I guess, that is because the data was HTML encoded already, as Zdravko Danev mentioned:
#Html.Raw(Model.MyHtmlData)
You can use literal control in asp.net to display the text and it will render it accordingly.
literal1.text = htmlcodeuwanttodisplay
Did you try something like
Server.HtmlEncode("<p><span style='color: #ff0000;'>asdsada</span></p>");
I have an ASP.net project that uses many asp:TextBox controls. When a page is being viewed in print mode, we want to display the HTML rendered for the TextBoxes different. Perhaps as a string literal. Is there a way to override / hook into the way TextBox is rendered on all pages?
jquery is not recommended as javascript could be disabled.
I would like to see a couple different methods this could be handled.
Do it on client side. Via #media print { } style declaration with a different style declared for input[type="text"] elements.
I have tried to use the standard AJAX HTMLeditor from here (http://www.asp.net/ajaxlibrary/act.ashx) and I have try to work with the FCKEditor (from http://ckeditor.com/)
But both don't do everything. I call the AJAX standard control A and the FCKeditor F.
With the A editor it is impossible to get your HTML text in the HTML content. You can only get it in the Design content. (this next code doesn't do the job: string htmlContentStr = Editor1.Content).
With F it is possible to get it in the HTML content (it does this by default), but to get your changes back in HTML is impossible. (this next code doesn't do the job: string htmlContentStr = FCKeditor1.Value).
So what I need is a HTML editor that is possible to put HTML text in HTML content, a user can make changes in the designcontent and after the changes 're make it must be possible to get the HTMLcontent and put it away in a string or database.
Is this possible or do I need a commercial one to get this feature?
If my question isn't clear, please let me know.
Thnx
I've used XStandard quite easily and it let me manipulate the HTML. I didn't bother using it as a control, but just read and wrote (escaped) the HTML where needed into the asp output.
When I render a page in ASP.NET, the following happens
</head>
<NOSCRIPT>
<meta http-equiv="REFRESH" content="0;URL=/Default.aspx?id=84&epslanguage=en-GB&jse=0" />
</NOSCRIPT>
<title>Page title goes here.</title>
<body>
My masterpage looks like this:
<title>Page title goes here.</title>
</head>
<body>
So what I'm asking is, where the heck has this refresh meta tag come from, why has it put it between my head tag and body tag, and why has my page title jumped outside of the head?!
When viewing the page's generated source in firebug, it shows the title tag and this new meta tag within the head tag, but viewing the source in any browser, it looks like the above. When using wget to scrape the page, it also comes out incorrectly as displayed above.
Any ideas why browsers may be interpreting this in different ways, and more importantly where this new meta tag has come from?
Thanks! Karl.
Edit:
Hi!
Thanks for your replies guys, very informative!
I've discovered that the problem is this line of code:
Page.Header.Controls.Add(ctrl);
Putting the mysterious meta tag in using this line puts it outside the head tag. When commenting this out, the title tag drops back into the right place, and all is well!
Any further thoughts?
Thanks!
Karl.
On the matter of why browsers will be interpreting it differently there are two answers. Firstly the firebug output as you say is generated source. That means its gone through a certain amount of processing already and clearly firefox is doing some magic to say "Well, its a meta and a title tag, they should be in the header so I'll put them there."
The other browsers you are comparing their raw source it sounds like which is before the browser has tried to make sense of it. I suspect you'd get the same if you viewed the raw source in firefox (ctrl-u).
I'd have expected all browsers to do much the same thing as you have described firefox as doing but if not then that's not really somethign to be concerned about. When invalid HTML like this is received the browsers have no real rules of what to do. This means that browsers are welcome to do whatever they want from trying to guess what you meant to just ignoring it entirely.
As for what is causing it, the epslanguage query paramter is from episerver - I don't know if that was in the request url or not so it may be that it is just being persisted or it may be episerver trying to redirect to a page with an explicit language instead of just assuming the default. Unfortunately I'm not familiar with episerver so I can't say any more specific to that.
It is of course definitely the case that there is something on your server side that is causing this to happen.
Do you get that for all pages out of interest or just one specific one or just in one specific circumstance?
Quite often it's a case of an element not being properly closed. Most browsers will try to adjust the markup so that it makes sense, but in most cases the markup will be incorrectly parsed.
You should probably share more of your master page (and the web form using it)!
Maybe your HEAD-tag doesn't have runat="server"?