Inconsistencies When Accessing DataBound Item Controls - asp.net

Essentially, my question is this: There are two ways that I have experienced setting values to databound controls. Either this way:
<asp:Label runat="server" id="MyLabel"><%#DataBinder.Eval(Container.DataItem, "MyValue")%></asp:Label>
Or this way:
<asp:Label runat="server" id="MyLabel" text=<%#DataBinder.Eval(Container.DataItem, "MyValue")%> />
When trying to access items in an event handler (outside of the method that this databinding is occuring) using the first method, MyLabel.Text is an empty string. However, using the second way, MyLabel.Text will equal "MyValue". Can anyone tell me why this happens?

The Text property of a Label doesn't map to the inner text in the control markup. The Label control can be used as a container for other controls - so you'd put child controls inside the tag.
The reason you're seeing the Text as empty when you bind using <%# ... %> is because the bound text is rendering as a child literal control in the MyLabel.Controls collection. In this case, you'd access the text as
var myText = ((ITextControl)MyLabel.Controls[0]).Text;
// instead of..
var myText = MyLabel.Text;
If you want to access the Text of the label - always use the Text property. If you want to nest controls in your label - put them between the markup tags.

Not sure about this but... It may be because in the second example, Text is a property of the Label control and you set it directly, whereas in the first example, you're not setting the Text property, you're just adding a child to the Label...
EDIT: A quick look with Reflector confirmed this: if the Label has some child content to it, it is that content that is rendered to html (but it's never set to the Text property). Otherwise, it is the content of the Text property that is rendered.

Related

Is there a difference between Image.GenerateEmptyAlternateText and just setting Image.AlternateText = "" in asp.net webforms

The title says it all, really.
I've found theImage.GenerateEmptyAlternateText property on the ASP.NET Image control and now I'm wondering if there's any difference in setting
<asp:Image GenerateEmptyAlternateText="True" /> and <asp:Image AlternateText="" />?
Turns out setting AlternateText="" doesn't work to set an empty alt text, if it set to an empty string, it gets omitted when the image HTML is rendered. To get alt="", one has to use the GenerateEmptyAlternateText property.
See below from "Accessibility in Visual Studio 2010 and ASP.NET 4"
ASP.NET controls that render images omit the alt attribute in the
rendered HTML if you simply assign an empty string to the
AlternateText property. For example, suppose you add the following
ASP.NET Image control to a page:
<asp:Image ImageUrl="PageDivider.gif" AlternateText="" Runat="Server" />
In this case, the following HTML is rendered:
<img src="PageDivider.gif" />
Notice that the alt attribute has disappeared. This is the default
behavior of all ASP.NET control attributes. When you do not assign an
attribute a value, it is not rendered. Unfortunately, in this case, we
really want to render an empty string as the alt attribute value.
In order to make sure that HTML rendered for an ASP.NET Image control
includes alt="", you must set the GenerateEmptyAlternateText property
to true.
From MSDN's ImageButton.GenerateEmptyAlternateText Property:
Gets or sets a value indicating whether the control generates an alternate-text attribute for an empty string value.
By default, the ImageButton control renders the AlternateText property using an alt attribute. When the AlternateText property is not set, the ImageButton control does not include the alt attribute to specify the alternate-text in the control rendering.
The XHTML document type definition requires the alt attribute on image controls. However, accessibility best practices recommend that image controls that do not convey information relevant to the context of the Web page should not specify an alt attribute. You can meet both XHTML and accessibility requirements by setting the AlternateText property to true.

How to set HtmlEditorExtender's content server-side

I'm using the AjaxControlToolkit's HtmlEditorExtender in my ASP.NET 4.0 web app:
<asp:TextBox ID="myTxt" runat="server" TextMode="MultiLine" Height="80px" Width="100%" />
<act:HtmlEditorExtender ID="heMyTxt" runat="server" TargetControlID="myTxt">
<Toolbar>
etc...
</Toolbar>
</act:HtmlEditorExtender>
When I set the content of the text box server-side like this:
myTxt.Text = htmlStringFromDatabase;
...the content in the textbox is the literal HTML markup (i.e. <b>Bold</b> shows up just like that, not like Bold). The formatting doesn't transfer, but the Extender does do its work on the textbox and set up its toolbar and buttons, etc. Is there a different way to set the content?
EDIT: turns out the HTML I get out of myTxt (the control that the extender is attached to) is encoded HTML. So now the question is how to stop the control from encoding its content. This problem is also presented in this question, but I'm not using LoadControl() or the designer to my page; I've written my markup manually.
Also, I don't know if this makes a difference, but I'm pulling the text out of the TextBox in the page's Page_Load handler.
Try to do like this,
myTxt.Text = HttpUtility.HtmlDecode(htmlStringFromDatabase);
I was able to solve this problem like this :
Literal lit = new Literal();
lit.Mode = LiteralMode.PassThrough;
lit.Text = HttpUtility.HtmlDecode(HTMLTExt);
TextBox1.Text = lit.Text; // The text box which HTMLEditorExtender is attached to

Asp.Net - User control with text between blocks

I want to create a usercontrol that behaves like the Label usercontrol or the HyperLink usercontrol.
What I mean - the Label usercontrol has the Text attribute, and the text can also be set with the following way:
<asp:Label runat="server" id="lblTest">Text Here</asp:Label>
If I wish to create a usercontrol that can set the text between blocks to the actual Text attribute of the control.
Do I need to use Templated UserControl? If no - what is the correct way?
Thanks all.
I would create a simple custom control with the text attribute exposed as a property :-)
http://msdn.microsoft.com/en-us/library/aa310915%28VS.71%29.aspx

Databound DIV in asp .net

Looking for a custom ASP .NET control were the InnerHTML of a DIV is connected to a databas to fetch the content to render the HTML content from the databas inside the DIV.
Is there one out there I have missed or anyone could tell if its possible to make my own DIV component to make it DataBound?
Thanks,
Stefan
You can't databind to a div, but you can databind to something like a Repeater, which is mainly good for showing rows of data (i.e. repeating the same markup for multiple data items).
If you just want to show one field from one row, you're probably better off with something like a literal:
<div>
<asp:Literal id="myLiteral" runat="server" />
</div>
And then in the code behind...
myLiteral.Text = "some string from the database or wherever";

What's the Literal control used for and what's the difference to the Label Control in asp.net?

What Literal control is used for in asp.net? and What is the difference between them and Label control?
The major difference is that the Label Control adds the span tag to the text (property) you set, allowing to apply a style to it:
<span>My Label text</span>
The Literal Control allows you to render any kind of content. You can use it to render scripts, hmtl and any other type of document content. It doesn't change the string you provide in the Text property.
Note: the Label control allows you to render straight HTML too, but it puts all your text in span tags as mentioned. So, for rendering large HTML portions a Literal control is the way to go.
P.S.: In HTML there is a <label> tag. If you use the AssociatedControlId property of the Label control, it will render as HTML <label> (thanks to Ray for pointing that out.)
For example:
<asp:Label runat="server" id="FirstNameLabel" AssociatedControlId="FirstNameTextBox">
Input First Name:
</asp:Label>
<asp:Textbox runat="server" id="FirstNameTextBox" />
Will render as:
<label for="FirstNameTextbox" id="FirstNameLabel">Input first name:</label>
<input type="text" id="FirstNameTextbox" name="FirstNameTextBox" />
See also here on W3 Schools.
One thing also to note is if you are justing using it to display something and have no need for formatting the text use a Literal control. The ViewState is not as heavy with a Literal vs a Label control and when you have many of these on a page using ViewState it can really bloat your page size.
I always ask myself, do I need to apply a custom style or formatting? Yes, use a Label. No, use a Literal.
It is used to display text on the page, the text that is displayed can be set at runtime via server side code.
The label control also has the AssociatedControlId property that associates the label with another control. An example of where this is useful is with a textbox control. Once these are associated, screen readers are more able to give better results.
Another example is a radio button with a label allows you to click on the label and the radiobutton will select if the AssociatedControlId property is set.
MSDN on AssoicatedControlId
As splattne mentions, the label encloses its text in a span, whereas the literal is simply a placeholder. However, be careful in making assumptions about how ASP.Net controls are going to render. It can depend on the user agent that you are using. For instance, the panel control renders as a div in IE, but renders as a table with Firefox.
It will place LITERALLY whatever text you place in it on the page. You can use it to write html, JavaScript or just plain text.
We can use literal control in the title tag whereas label cannot be used in the title tag
Label can be used to set focus on other controls like Textbox.
Whereas Literal simply rander the static text on the web page

Resources