asp:label doesn't render children - asp.net

I have an asp:label with a nested custom control and it simply doesn't render. I tried to register a custom WebControlAdapter for Label type and while debugging I noticed that there is apparently no control in the Controls collection, it seems to be completely ignoring any nested elements.
Here's markup
<asp:Label ID="lbl13" runat="server" AssociatedControlID="txt13" Text="<%$ Resources:Resources, lbl13 %>">
<asp:ValidationMessage ID="vm13" runat="server" MessageFor="txt13" CssClass="field-validation-error"></asp:ValidationMessage>
</asp:Label>
Any idea how to bypass this problem?

When you set the Text property, it clears the child controls. If you remove the Text="<%$ Resources:Resources, lbl13 %>" from the Label, your child controls should render.
EDIT
If you set the Text property to a static string and add only literal content, the label will only render the literal content:
<asp:Label runat="server" Text="Hello"> World</asp:Label>
Output: World
If you set the Text property to a static string and add child controls, the label will render the text and the child controls:
<asp:Label runat="server" Text="Hello">
<asp:Label runat="server" Text="World" />
</asp:Label>
Output: HelloWorld
If you set the Text property using an expression builder, the label will only render the text:
<asp:Label runat="server" Text="<%$ Resources:Resources,Hello %>">
<asp:Label runat="server" Text="World" />
</asp:Label>
Output: Localised version of "Hello"
To override this behaviour, you'll need a custom Label control. For example:
public class MyLabel : Label
{
public override string Text
{
get { return base.Text; }
set
{
if (HasControls())
{
Controls.AddAt(0, new LiteralControl(value));
}
else
{
base.Text = value;
}
}
}
}

Related

How to change the properties of controls in a UserControl at runtime in WebForms

I have a simple UserControl defined as:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="AdminBanner.ascx.cs" Inherits="OrderManager.Controls.AdminBanner" %>
<asp:Panel runat="server" ID="AlertPanel" BorderWidth="1px">
<asp:Label runat="server" ID="LabelTitle" Text="Test!!!!!!!" ></asp:Label>
</asp:Panel>
I include the UserControl on an aspx page like so:
<table style="width: 100%;">
<tr>
<td>
<asp:Panel ID="PanelPreviewBanner" Visible="False" runat="server">
<OrderManager:AdminBanner Id="AdminBanner" Visible="True" runat="server" />
</asp:Panel>
</td>
</tr>
</table>
When I click a button on the web form, I want to not only display the Panel, but also change the default value of the LabelTitle in the UserControl. I can show the Panel like this:
// Display the Preview Panel.
this.PanelPreviewBanner.Visible = true;
If I try to change the LabelTitle of the USerControl, wither using the ID of the UC or by way of a property in the UserControl, I always get an error stating that the control is null.
My ultimate goal is to be able to set the css class and style on objects defined in my UserControl.
I have tried numerous examples I found here and through Google and nothing works.
I am assuming you've already done this -
A public property in your AdminBanner.ascx.cs that exposes label's text property to be set by the parent page
public string LabelTitleValue
{
get { return LabelTitle.Text; }
set { LabelTitle.Text = value; }
}
Below is how you should be able to get/set the usercontrol's label text.
AdminBanner.LabelTitleValue = "Test changed";
var text = AdminBanner.LabelTitleValue;
In case you've already done this, you may first try removing your user-control outside the panel (something like below) and at least confirm that user-control is being displayed.
<td>
<asp:Panel ID="PanelPreviewBanner" Visible="False" runat="server"></asp:Panel>
<OrderManager:AdminBanner Id="AdminBanner" Visible="True" runat="server" />
</td>
If it's displaying properly, you may do below in addition to setting panel's visibility
// Display the Preview Panel.
this.PanelPreviewBanner.Visible = true;
this.AdminBanner.Visible = true;

Why my asp:Literal control doesn't work in a asp:ListView ItemTemplate

Why my asp:Literal control doesn't work in a asp:ListView ItemTemplate ?
ASP.NET 4.5 WebForms
ASPX code :
<asp:ListView ID="lv" runat="server" ItemType="WebApplication1.ItemClass">
<ItemTemplate>
<asp:Literal runat="server" Text='<%#: Item.HtmlText %>' />
</ItemTemplate>
</asp:ListView>
ASPX code behind :
this.lv.DataSource = new ItemClass[] {
new ItemClass() { HtmlText = "<p>Hello 01<br/>blablabla</p>"},
new ItemClass() { HtmlText = "<p>Hello 02<br/>blablabla</p>"},
new ItemClass() { HtmlText = "<p>Hello 03<br/>blablabla</p>"}
};
this.lv.DataBind();
My object class :
public class ItemClass
{
public string HtmlText { get; set; }
}
Result on my page :
<p>Hello 01<br/>blablabla</p> <p>Hello 02<br/>blablabla</p> <p>Hello 03<br/>blablabla</p>
I can see html elements tags. I don't known why.
Thanks
When you use the colon character (:) in your code block, it automatically HTML encodes the output. (see my answer here for more information)
To fix this, simply drop the colon from your code, so it looks like this instead:
<asp:ListView ID="lv" runat="server" ItemType="WebApplication1.ItemClass">
<ItemTemplate>
<asp:Literal runat="server" Text='<%# Item.HtmlText %>' />
</ItemTemplate>
</asp:ListView>

How to databind alt tag in ASP.NET hyperlink control?

I have this tag: alt='<%# Eval("ImageDesc") %>' inside a DataList control
When I view the page in my browser the alt tag is always empty: alt=""
Maybe this is because the alt tag is not supported by the hyperlink control? How can I databind the alt tag?
------- this is the exact code
<asp:HyperLink ID="linkQuoteImage" runat="server" EnableViewState="False" ImageUrl='<%# "/images/"+Eval("ImageFile").ToString().Replace("/","/tmb/") %>' NavigateUrl='<%# "~/Quotes/"+Eval("SlugText") %>' alt='<%# Eval("ImageDesc") %>' Font-Underline="False" ForeColor="#333333"></asp:HyperLink>
if in the aspx page
<asp:HyperLink ID="linkQuoteImage" runat="server">Text for image alt</asp:HyperLink>
If in the code behind just do linkQuoteImage.Text = "Text for image alt" -- The text you need in the alt
Really, the Hyperlink does not have the alt property. Use Tooltip instead, if this is what want:
<asp:HyperLink ID="lnkTest" runat="server" ToolTip='<%# Eval("Data") %>'>'<%# Eval("OtherData")%>'</asp:HyperLink>
The alt property is used in a <img> tag to show a replace text when the image is not found. Also, the text is shown as a tooltip in some browsers. If you want to show a tooltip, use the HTML title property, which can be obtained by using the Tooltip ASP.Net property.
alt attribute is used in hyperlink when we are using basic html
but in case of u r using asp.net hyperlink control alt property is replaced by ToolTip use this instead
replace
alt='<%# Eval("ImageDesc") %>' by ToolTip='<%# Eval("ImageDesc") %>'
Is your <asp:HyperLink> control inside of some control like a Repeater?
This code works for me.
Front end:
<asp:Repeater ID="rptTest" runat="server">
<ItemTemplate>
<asp:HyperLink ID="testLink" runat="server" alt='<%# Eval("ImageDesc") %>'>This is a link</asp:HyperLink>
</ItemTemplate>
</asp:Repeater>
Back end:
protected void Page_Load(object sender, EventArgs e)
{
var images = new List<ImageData>
{
new ImageData { ImageDesc = "test" },
new ImageData { ImageDesc = "test2" }
};
rptTest.DataSource = images;
rptTest.DataBind();
}
...
public class ImageData
{
public string ImageDesc { get; set; }
}
If you aren't using a control that has the Eval method available, then you'll need to add the property differently. I would recommend just setting it in the code-behind.
Front end:
<asp:HyperLink ID="testLink" runat="server">This is a link</asp:HyperLink>
Back end:
protected void Page_Load(object sender, EventArgs e)
{
if (testLink.Attributes["alt"] == null)
{
testLink.Attributes.Add("alt", "test");
}
}

User control (ascx) in asp.net and its bindable properties dont show up in the data container

When I add a user control which has bindable properties to an asp.net page I do not see its bindable properties on the designer's dialog box when I click the edit Data bindings on the Repeater-Listview
should I add any other propery descriptors?
the markup would be like this inside a repeater or similar control
<uc1:Menu ID="Menu1" superman="<% Eval("userid") %>" runat="server" />
on the bindable property I only use
[System.ComponentModel.DefaultBindingProperty("superman")]
[System.ComponentModel.Bindable(true)]
public int superman
{
get;
set;
}
People discuss it here but they couldn't find a solution
I think this might be a problem with Visual Studio, I have not found a way to show the properties of user controls, but still, you can bind the property even when it does not appear in the options box:
This is how you bind it:
In the user control code behind:
[Bindable(true)]
public string MyProperty
{
get
{
return this.lblMessage.Text;
}
set
{
this.lblMessage.Text = value + DateTime.Now.ToString();
}
}
In the ASCX file:
<asp:Label ID="lblMessage" Text="something" runat="server" />
In the ASPX page:
<asp:DataList runat="server" ID="dataList" Width="50%"
DataSourceID="ObjectDataSource1">
<ItemTemplate>
<uc1:WebUserControl1 runat="server" ID="myUC" MyProperty='<%# Eval("Name") %>' />
</ItemTemplate>
</asp:DataList>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
SelectMethod="GetProducts" TypeName="WebApplication1.Repository">
</asp:ObjectDataSource>
Simple just add the property you created in your UC in the tag:
<uc1:WebUserControl1 runat="server" ID="myUC" MyProperty='<%# Eval("Name") %>' />
This is the output:
I think you just make the markup by typing it,
the user control must be drag and drop to the page in the design view.
try this may helps.

Public Properties in Code Behind of a User Control

In a regular .aspx page, you can access public properties from the codebehind. Is there any way to do a similar thing in a user control. For example, in the following code 'List' is public property of the codebehind of the user control and yet it is not accessible.
<% foreach (TripTeam team in List) { %>
<div>
<label><%= team.Name %></label>
</div>
<%} %>
You can access the public properties of a UserControl from your .aspx page. Here's an example
<script runat="server">
public string Caption { get { return _caption.Text; } set { _caption.Text = value; } }
public string Text{ get { return _tb1.Text; } set { _tb1.Text = value; } }
<div>
<asp:Label ID="_caption" runat= "server" class="caption" /><br />
<asp:TextBox ID="_tb1" runat="server" CssClass="textBox" Width="25px" />
Then on your aspx page, you can set the Text and Caption properties within your user control:
<uc1:CaptionText ID="ct1" runat="server" Caption="User name" />
You need to say 'userControlId.List' to access the property when accessing a property in the control from a page which uses the control.

Resources