Making asp:Label a specific HTML tag - vb.net

New to VB.net, just trying to figure out how to properly manipulate the asp:Label control.
I have a page that, based on if there are results, etc should display an <h1></h1> tag with a header, then the data. As I am using the code-behind model, my user facing page essentially just has the following:
<asp:Label ID="lblMessage" runat="server"
Visible="false" />
<asp:DataList ID="dlCurriculumLists" runat="server"
DataSourceID="sdsCurriculumLists"
DataKeyField="Entry No_"
RepeatLayout="Flow"
Visible="false">
<ItemTemplate>
<div>
<asp:HyperLink ID="hlCurriculum" runat="server"
Text='<%# DataBinder.Eval(Container.DataItem, "Title") %>'
NavigateUrl='<%# DataBinder.Eval(Container.DataItem, "File Path") %>'
ToolTip='<%# DataBinder.Eval(Container.DataItem, "Title") %>'
Target="_blank"
Style="font-weight: bold;">
</asp:HyperLink>
</div>
</ItemTemplate>
</asp:DataList>
On my code-behind page, I then set the asp:Label and asp:DataList to Visible="true" based on the data from the database. Here's the catch - if there is data, I want to set lblMessage to be an H1, and if not, just standard Label text. I realize I can emulate the look through the CSS, but was just hoping there was another way (maybe similar to the ItemTemplate concept) to specify the HTML type of the Label control - it appears to be a by default.

For people coming from a VB background, it's a common mistake to think that the most basic control for displaying arbitary text is a Label.
That's not true. A label should label something, usually another UI control (that's what the AssociatedControlId property is for).
If you just want to display arbitrary text or HTML markup, use something more basic instead. Some examples are asp:Literal, asp:Placeholder or asp:Localize.
Using, for example, an asp:Literal called myLiteral, you can easily create a heading in code:
myLiteral.Text = "<h1>" & Server.HtmlEncode(myHeading) & "</h1>"

As far as I know, the asp:Label component will always generate a <label> HTML tag when its AssociatedControlId attribute is set.
What you could do instead is use a Literal Control and populate it with the HTML you wish at runtime.
UPDATE
One thing you could do to make this work as required using your current Label control is to have a theme for the label that marks it up as a H1. You could then toggle the controls EnableTheming property as required.

Aside from what already has been suggested here, you can also implement your own ASP.NET control with any properties you want, then modify its rendering on the fly, depending on the properties' values. It is pretty fun to do and is not as hard as one might think. Here is some information on the subject: http://msdn.microsoft.com/en-us/library/vstudio/zt27tfhy(v=vs.100).aspx

Related

HTML MAILTO hyperlink doesn't respond

My hyperlink doesn't open up an email with my database text value.
I have a previous application that uses this exact format and works; however, for this application it doesn't.
One of the many examples I have referenced is: ASP.NET mailto: misfunction
my Database column name is email_Own and so I have tried to DataItem.email_Own and DataItem.email like other examples show to see if whether the value after DataItem is the database column name or not. Both don't work.
Working EX
My other application showed an email within a gridview so I am wondering if that is the reason why the coding below worked:
<asp:TemplateField HeaderText="Description" >
<ItemTemplate >
<br /><br />
Email:
<asp:HyperLink id="lnkEmail" cssClass="emailColor" runat="server" text='<%#DataBinder.Eval(Container, "DataItem.email") %>' NavigateUrl='<%#DataBinder.Eval(Container, "DataItem.email","MAILTO:{0}")%>'>
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
Coding that doesn't work:
Any suggestions for the coding below? This hyperlink is inside a table.
<tr>
<td width="25%">
<font class="Blackfont" size="2" > <b>Owner Email </b> </font>
</td>
<td>
<asp:HyperLink id="Owner_E" runat="server" text='<%#DataBinder.Eval(Container, "DataItem.email") %>' NavigateUrl='<%#DataBinder.Eval(Container, "DataItem.email","MAILTO:{0}")%>'Font-Size="10pt"></asp:HyperLink>
</td>
</tr>
The text of the hyperlink shows up correctly when the page loads. Its the linking to mail that isn't working.
I also tried this code snippet instead but the hyperlink doesn't respond as well: NavigateUrl='<%#Bind("email_Own", "mailto:{0}") %>' Text='<%#Bind("email_Own") %>'
FYI:
I am not sure if this changes anything but I fill the hyperlink in VB.net like so:
If Not DsAds.Tables(0).Rows(0).Item(14) Is DBNull.Value Then
lnkEmail.Text = DsAds.Tables(0).Rows(0).Item(14)
End If
where DsAds is a dataset
The HTML source code shows this:
I referenced http://forums.asp.net/t/1071308.aspx?mailto+link+in+textbox
Like so:
<asp:HyperLink id="lnkEmail" runat="server" Font-Size="10pt" ></asp:HyperLink>
and in Page Load[at end of it] I add:
lnkEmail.NavigateUrl = "mailto:" + DsAds.Tables(0).Rows(0).Item(14)
lnkEmail.Text = DsAds.Tables(0).Rows(0).Item(14)

What is the simpliest way to add a single image to a Sitefinity template?

I have a Sitefinity 7.0 installation. I've added a custom content type, which includes a field for a single, required, image.
I created a custom template for this content type. On the site you can click on examples for how to add your fields to the widget template. This is what it gives me for the 'Picture' field:
<%--The following namespace should be registered at the top of the control if it doesn't exists--%>
<%# Register Assembly="Telerik.Sitefinity" Namespace="Telerik.Sitefinity.Modules.Libraries.Web.UI.Images" TagPrefix="sf" %>
<%# Register Assembly="Telerik.Sitefinity" Namespace="Telerik.Sitefinity.Modules.Libraries.Web.UI" TagPrefix="sf" %>
<sf:ImagesView ControlDefinitionName="ImagesFrontend" runat="server" MasterViewName="ImagesFrontendThumbnailsListLightBox" Title="" UrlKeyPrefix="">
<RelatedDataDefinition RelatedFieldName="Picture" RelatedItemType="Telerik.Sitefinity.DynamicTypes.Model.GIARRecipients.GiarRecipient" RelationTypeToDisplay="Child" RelatedItemSource="DataItemContainer">
</RelatedDataDefinition>
<ControlDefinition ControlDefinitionName="ImagesFrontend" runat="server" ProviderName="OpenAccessDataProvider">
<Views>
<sf:ImagesViewMasterDefinition SortExpression="" ViewName="ImagesFrontendThumbnailsListBasic" runat="server">
</sf:ImagesViewMasterDefinition>
<sf:ImagesViewMasterDefinition SortExpression="" ViewName="ImagesFrontendThumbnailsListLightBox" runat="server">
</sf:ImagesViewMasterDefinition>
<sf:ImagesViewMasterDefinition SortExpression="" ViewName="ImagesFrontendThumbnailsListSimple" runat="server">
</sf:ImagesViewMasterDefinition>
<sf:ImagesViewMasterDefinition SortExpression="" ViewName="ImagesFrontendThumbnailsListStrip" runat="server">
</sf:ImagesViewMasterDefinition>
<sf:ImagesViewDetailDefinition ViewName="ImagesDetailView" runat="server">
</sf:ImagesViewDetailDefinition>
</Views>
</ControlDefinition >
</sf:ImagesView>
This is way too much and the markup produced uses <ul><li>, even though it's always exactly one image. Ideally, I want it as simple as <img src='<%# Eval("Picture") %>' />
What is the simpliest way to add a single image to a Sitefinity template?
For the image fields on custom content types, you can limit the number of images by clicking on the "Limitations" tab when adding the field. Select "Only 1 image can be uploaded or selected"
.
The control that gets used in the template is instead is a Sitefinity Images control:
<sf:ImageControl runat="server" Title="" UrlKeyPrefix="" ProviderName="OpenAccessDataProvider" >
<RelatedDataDefinition RelatedFieldName="OneImage" RelatedItemType="Telerik.Sitefinity.DynamicTypes.Model.Solutions.Solution" RelationTypeToDisplay="Child" RelatedItemSource="DataItemContainer">
</RelatedDataDefinition>
</sf:ImageControl>
And the markup that gets rendered is a image tag wrapped in a div:
<div class="sfimageWrp">
<img id="ctl09_ctl00_ctl00_detailContainer_ctl03_0_ctl00_0_ctl00_0_imageItem_0" title="ootb" src="/images/default-source/default-album/ootb.png?sfvrsn=0" alt="ootb">
</div>
I choose Ben's answer as correct, but an alternative I found in the mean-time may also be worth mentioning.
<img src='<%# ((Telerik.Sitefinity.Libraries.Model.Image)Eval("Picture")).Url %>' />

how to pass variable in the url from gridview in vb.net

I have Gridview of the form results. I am trying to add an url to one of the field. How do I pass the variable in the url. intLib is the variable. This is what I have:
VB.Net Code:
<a href="EditIncident.aspx?ID=<%# Eval("Inci_ID")%>&Val2="& intLib>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Inci_ID") %>'></asp:Label>
</a>
I created a session variable and passed it on to the next page.
Thanks for your help.

how to validate a textbox length in vb.net?

In VB.NET , I am trying to validate a
<asp:textbox id="name" runat=server> </asp:textbox>
textbox value which should not exceed more than 200 characters and the textbox takes only string.
Which validator will be a good option ? i want to use custom validator, will it work with string values?
<asp:RegularExpressionValidator ID="regexTextBox1"
ControlToValidate="YourTextBoxID" runat="server"
ValidationExpression="^[\s\S]{0,200}$" Text="200 characters max" />
You can avoid the use of custom validator altogether.
If you only need to force the max length of a text box field you have this at your disposal
<asp:TextBox MaxLength="200" id="name" runat=server> </asp:TextBox>

How to set DataNavigateUrlFormatString dynamically ? or How to eval Page.User.Identity.Name on a webpage?

I am building a GridView which can download songs. So I use a hyperlink fileld and use DateNavigateUrlFormatString.
E.G.
DataNavigateUrlFormatString="~/uploads/{0}"
Now,
I need to combine Page.User.Identity.Name into DateNavigateUrlFormatString
For example,
DataNavigateUrlFormatString="~/uploads/UserName/{0}" which Page.User.Identity.Name depending on the user login.
I tried to access DataNavigateUrlFormatString from the code behind but I couldn't.
I tried to use Eval such as
'~/uploads/" <%# Eval("Page.User.Identity.Name") %> /{0}'
this doesn't work as well.
Hope someone could point me out.
Thanks,
L
FInal Solution
<asp:TemplateField HeaderText="Play">
<ItemTemplate>
<asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl = '<%# String.Format("~/UserUploads/{0}/",Page.User.Identity.Name)+ "/" + Eval("Song_Name") %>' Text="Play" >
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
DataNavigateUrlFormatString expects the placeholder(s) to come from fields in the datasource of the control.
My suggestion is that since you already know the url you are trying to build you could set the NavigateUrl property of hyperlink control.
NavigateUrl = '<%# String.Format("~/UserUploads/{0}/",Page.User.Identity.Name)+ "/" + Eval("Song_Name") %>'