Apostrophes in XML and Databound Controls - asp.net

In my XML, it's possible for an apostrophe to appear in a node's value:
<Root>
<Sections>
<SectionA>
<Heading>Title</Heading>
<Description>This is section 'A'</Description>
</SectionA>
</Sections>
</Root>
If I have controls bound to this XML:
<asp:FormView ID="myFormView" runat="server" DataSourceID="myXmlDataSource">
<ItemTemplate>
<div>
HTML Element:
<input type="text" value='<%# XPath("text()") %>' />
</div>
<div>
Server Control:
<asp:TextBox id="myTextBox" runat="server" value='<%# XPath("text()") %>' />
</div>
</ItemTemplate>
</asp:FormView>
<asp:XmlDataSource ID="myXmlDataSource" runat="server" XPath="Root/Sections/SectionA" />
I've noticed that the text is correctly displayed in the asp:TextBox but not in the INPUT element. I'm assuming that it's because server controls correctly escape the apostrophe. To work around this, I tried changing the Description node in the XML to the following:
<Description>This is section 'A'</Description>
Again, this displayed correctly in the asp:TextBox, but not in the INPUT element.
My next attempt was to wrap the node's value in a CDATA:
<Description><![CDATA[This is section 'A']]></Description>
Finally it was displaying correctly in the INPUT element, but now the asp:TextBox displayed the two "& # 3 9 ;". I've even tried "& a p o s ;" but the result is the same.
What is the best approach for the use of apostrophes in XML where the value can be displayed in either a server control or HTML element?

Here you have single quotes surrounding the value argument for the html element:
<input type="text" value='<%# XPath("text()") %>' />
This renders:
value='This is section 'A''
Instead use double quotes:
<input type="text" value="<%# XPath("text()") %>" />
Which renders:
<input type="text" value="This is section 'A'" />

Use &apos; instead. It is one of the 5 permitted entities in XML.
So your code would look like:
<Description>This is section &apos;A&apos;</Description>

Related

Why does asp:Textbox sometimes add "text" to the CSS class in the rendered html?

This markup:
<asp:TextBox ID="txtAddress" CssClass="s175" runat="server" MaxLength="30" placeholder="Street"></asp:TextBox>
is rendered as:
<input name="ctl00$LeftColumnContent$txtAddress" type="text" maxlength="30" id="LeftColumnContent_txtAddress" class="s175 text" placeholder="Street">
But on another project, this markup: (exactly the same)
<asp:TextBox ID="txtAddress" CssClass="s175" runat="server" MaxLength="30" placeholder="Street"></asp:TextBox>
causes this to happen:
<input name="ctl00$ContentPlaceHolder1$txtAddress" type="text" maxlength="30" id="ContentPlaceHolder1_txtAddress" class="s175" placeholder="Street">
Why is the "text" class not getting applied? It's class="s175" vs class="s175 text".
You can apply skins to controls in asp.net, http://msdn.microsoft.com/library/ykzx33wh.aspx , if there is a default skin in this particular project it might explain why the class is being added. Check for a .skin file in the project.
It's getting added from javascript. I think it's this:
jquery.uniform.js
function doInput(elem){
$el = $(elem);
$el.addClass($el.attr("type"));
storeElement(elem);
}

Show "<hello world>" as Text in ASP.NET RadioButton

I want a RadioButton to display text like "<hello world>" (note I want the "<" and ">" characters to be displayed, not to be interpreted as HTML tag characters).
Tried something like this :
<asp:RadioButton ID="RadioButton1" runat="server" Text="<hello world>" AutoPostBack="True" />
but this displays no text on the browser, and renders on client side as something like :
<span class="radiobutton"><input id="ctl00_mainCopy_wizard_RadioButton1" type="radio"
... />
<label for="ctl00_mainCopy_wizard_RadioButton1"><hello world></label></span>
Any ideas ?
Try double escaping it.
&lt;hello world&gt;

How do I retrieve HTML POST data for manipulation in ASP.NET WebForms?

I have the following html:
<html>
<body>
<form runat="server">
Name: <input type="text" name="name" />
<br />
<input type="submit" name="submit" />
</form>
</body>
</html>
How do I retrieve the value in the "name" textbox posted back to the webserver to manipulate in ASP.NET WebForms?
(I know about the ASP.NET built-in controls and the possibilities with them, but I am looking for a "clean" solution without the use of built-in ASP.NET controls)
If you can't, or don't want to use asp.net textboxes, then you can retrieve the name of a regular html textbox like this:
string nameTextPosted = Request.Form["name"];
Just note that textboxes created in this manner will not automatically persist their values across postbacks like asp.net textboxes will.
Simplest solution would be to turn it into a server-side component and access it by it's name. e.g.
<asp:TextBox Id="Name" runat="server"></asp:TextBox>
...
string name = Name.Text;
Unless you have other reasons not to use a component, you'd only be making things much more difficult on your part for no justification.
ASP.net includes Html server controls for backward compatibility for just someone like you fond of html. make your html tags server controls by adding the runat="server" and id properties and you are able to access them inside your server side code with their id.
<form runat="server">
Name: <input type="text" name="name" id="name" runat="server" />
<br />
<input type="submit" name="submit" id="name1" runat="server" />
</form>
Now after this you can control their behavior:
name.Value="Hellow World !"
You have to add id and runat="server" in each control. like this :
<input type="text" name="name" id="name" runat="server" />
Its better to use asp:TextBox like this :
<asp:TextBox ID="name" runat="server"></asp:TextBox>

How is my page creating two different types of checkbox?

Got a really weird thing happening on my page. I have some checkbox controls and some of them are rendering like this..
<span confirmmodified="true" class="aspNetDisabled"><input type="checkbox" disabled="disabled" name="ctl00$FormContents$TrackerDetails$OutOfScopeUSQS" id="ctl00_FormContents_TrackerDetails_OutOfScopeUSQS"><label for="ctl00_FormContents_TrackerDetails_OutOfScopeUSQS">Out of scope USQS</label></span>
But some of them are rendering like this...
<input type="checkbox" name="ctl00$FormContents$TrackerDetails$OutOfScopeSQA" id="ctl00_FormContents_TrackerDetails_OutOfScopeSQA"><label for="ctl00_FormContents_TrackerDetails_OutOfScopeSQA">Out of scope USQS</label>
See, no span? Cant work out what is going on I have looked at the page and control and doesnt seem like anything different is happening...
UPDATE: [this is the markup]
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="AuditStateTracker.ascx.cs"
Inherits="Dashboard.controls.AuditStateTracker" %>
<div class="grid_12 alpha">
<h2><asp:Literal runat="server" Text="<%$ Resources:LocalisedText, TrackerMigration%>" /></h2>
</div>
<div class="grid_1">
<asp:CheckBox ID="OutOfScopeUSQS" runat="server" Text="<%$ Resources:LocalisedText, OutOfScopeUSQS %>" />
</div>
<div class="grid_1" runat="server" ID="divOutOfScopeSQA">
<asp:CheckBox ID="OutOfScopeSQA" runat="server" Text="<%$ Resources:LocalisedText, OutOfScopeUSQS %>" />
</div>
<div class="grid_1">
<asp:CheckBox ID="OutOfScopeRS" runat="server" Text="<%$ Resources:LocalisedText, OutOfScopeRS %>" />
</div>
<div class="grid_1" runat="server" ID="divRingFencedSQA">
<asp:CheckBox ID="RingFencedSQA" runat="server" Text="<%$ Resources:LocalisedText, RingFencedSQA %>" />
</div>
<div class="grid_1">
<asp:CheckBox ID="RingFencedRS" runat="server" Text="<%$ Resources:LocalisedText, RingFencedRS %>" />
</div>
<div class="grid_1" runat="server" ID="divBusinessCase">
<asp:CheckBox ID="BusinessCase" runat="server" Text="<%$ Resources:LocalisedText, BusinessCase %>" />
</div>
<div class="grid_1" runat="server" ID="divDelisted" visible="false">
<asp:CheckBox ID="Delisted" runat="server" Text="<%$ Resources:LocalisedText, Delisted %>" />
</div>
The first checkbox is disabled, the second one isn't; that's how asp.net renders disabled checkboxes.
It's throwing that span on there so asp.net can decorate it with the css class aspNetDisabled. This would allow you to (optionally) style disabled controls differently.
Incidentally, this disabled class is actually configurable.
In earlier versions of ASP.NET Microsoft used to render the disabled = "disabled"attribute for your controls. However, since the HTML 4.01 specification, the disabled attribute is not valid anymore for each type of web control. It is still valid for input, but not anymore for span.
So what they've done is add a style class (in CSS) which is set to controls when they are disabled. This is also the case with any controls in a diabled control. By default, this style class is called `aspNetDisabled'.
You can actually use a different name for this class, but not for individual controls. The name of this style class can only be set as a static property on the root class WebControl, for instance at application start in your global.asax.cs.
protected void Application_Start(object sender, EventArgs e)
{
WebControl.DisabledCssClass = "disabled";
}
More annoying is that for checkboxes, ASP.NET renders a span around you checkbox with the defined style class on it.
<span disabled="disabled">
<input id="ctl00_UsecaseContent_ctl01_ctl01_bCVbox" type="checkbox" name="ctl00$UsecaseContent$ctl01$ctl01$bCVbox" checked="checked">
</span>
When you are using e.g. Bootstrap as your UI framework, and on top of that a checkbox specific framework, such as https://github.com/flatlogic/awesome-bootstrap-checkbox, this additional span can actually break the structure of your HTML checkbox control hierarchy.

How to store value of data binding expression into variable

I need to access the value of a bound item several times in a template. Right now my ListView template looks like this:
<asp:ListView ID="ListView1" runat="server" ItemPlaceholderID="plc"><br/>
<ItemTemplate><br/>
<input type="radio" class="myrating<%# DataBinder.Eval(Container.DataItem, "Day")%>" value="3" /><br/>
<input type="radio" class="myrating<%# DataBinder.Eval(Container.DataItem, "Day")%>" value="4" /><br/>
</ItemTemplate><br/>
<LayoutTemplate><br/>
<div id="plc" runat="server"><br/>
</div><br/>
</LayoutTemplate><br/>
<EmptyDataTemplate><br/>
No data</EmptyDataTemplate><br/>
</asp:ListView><br/>
Under certain conditions I may have dozens of radio button so repeatedly calling <%# DataBinder.Eval(Container.DataItem, "Day")%> seems to be inefficient.
I would like to assign the value of that expression to a variable and then use this variable instead so my template would look something like this
<ItemTemplate><br />
<%String ClassName = "myrating" + <%# DataBinder.Eval(Container.DataItem, "Day")%><br />
<input type="radio" class="<%=ClassName %>" value="3" /><br />
<input type="radio" class="<%="ClassName" value="4" /><br />
</ItemTemplate><br />
This example doesn't compile but I hope you are getting the idea.
You can give your page a public variable MyRating.
Now you can assign the variable in the expression binding Syntax:
<ItemTemplate>
<%# MyRating = "myrating" + <%# Eval(Container.DataItem, "Day")%>
//Use the variable inside the binding(!) block
<%#MyRating
</ItemTemplate>
I usually bind to lists of view-objects. That way I can access view properties directly.
<ItemTemplate>
<%# MyType = (MyType)Container.DataItem
<%# MyRating.Average %>
<%# MyRating.Count %>
</ItemTemplate>
Hope this helps :-)
You can use OnItemDataBount event and work with DataItem as with variable there.
I personally consider setting HTML element values in the OnItemDataBound event to be messier than in the ASP code. I also didn't like setting the variable value using a data-binding expression since it seems to cause the value to be output in the HTML.
Here's a similar way to do it based on the other answers:
1. Create a protected field in the code-behind to use as your variable.
protected string className;
2. Use a data-binding expression to assign to the variable.
<asp:Literal runat="server" Visible="false" Text="<%# className = "myrating" + DataBinder.Eval(Container.DataItem, "Day") %>" />
Make sure to do this inside the hidden server-side tag so that the result of the expression does not appear in the resulting HTML.
3. Use the variable inside data-binding expressions in the ASP code.
<ItemTemplate><br />
<input type="radio" class="<%# className %>" value="3" /><br />
<input type="radio" class="<%# className %>" value="4" /><br />
</ItemTemplate><br />
Make sure to use data-binding expressions to access the variable. Other expression types only seem to see the default value of the variable.
I think the main disadvantage of this approach is the use of the field, which would ideally be scoped to the ItemTemplate element.
You can define public/protected variables in the code-behind and assign those in data binding expressions.
If you don't have access to the code-behind (e.g. when you are modifying the markup of a compiled application), you can declare variables in a script block set to run on server, e.g.:
<script runat="server">
YourNamespace.Rating current;
</script>
Then in your control's binding templates:
<HeaderTemplate>
<%# (current = (YourNamespace.Rating)Eval("Day")) == null ? "" : "" %>
</HeaderTemplate>
Put it in the HeaderTemplate if you only want it to get executed once per databinding.
The == null ? "" : ""-part, is to prevent any generated cruft html (e.g. the ToString value of the assigned value), from being rendered in the browser.

Resources