inline code on server control properties - asp.net

I have a public POCO property (SiteDetail) on my page and I need to know what's the best approach when setting properties of server controls:
Use inline code and Page.DataBind(); on load
<asp:Label ID="lbName" runat="server" Text="<%# SiteDetail.Name %>"/>
Do not use inline code and set control properties on page load
lbName.Text = SiteDetail.Name;
Is it "dangerous" to use Page.DataBind() on load?

Did you see the rendered source? There is no difference at all. Both labels will render text in the span. Just perform a simple test and it will be clear to you.
here is what I have for a test
<asp:Label ID="Label1" runat="server" Text='<%#test %>'></asp:Label>
<asp:Label ID="Label2" runat="server"></asp:Label>
here is generated source below.
<span id="Label1">this text is from binding expression</span>
<span id="Label2"><br/>this text set from code behind</span>
You will see no difference at all

So...I did some more research and found Page.DataBind() is not a good thing, is better to call DabaBind on every single control you need, as #Muhammad Akhtar says, both ways renders the same so I prefer to use inline code because it seems clearer, now I have
<asp:Label ID="lbName" runat="server" Text="<%# SiteDetail.Name %>"/>
and code behind:
if (!IsPostBack)
{
lbName.DataBind();
}

Related

ASP.NET closing tag

When I use autocompletion in VisualStudio 2010 within my .aspx application, there are different default completions at closing control tags:
<asp:CheckBox />
<asp:Label></asp:Label>
Is there a explaination for this behaviour?
<asp:CheckBox></asp:CheckBox>
<asp:Label />
Wouldn't be invalid.
This is because ASP.NET's Label control is decorated with the ParseChildrenAttribute, with ParseChildren(false) while CheckBox isn't.
You can support the same behavior whith your custom controls, for example, with the following code, Visual Studio will behave like Label if you use MyControl in the web form editor:
[ParseChildren(false)]
public class MyControl : WebControl
{
...
}
The label closing is like that
<asp:Label runat="server"></asp:Label>
because usually we type something between
<asp:Label runat="server" ID="lblOne">better start programming now</asp:Label>
that is not the case for checkbox, that we type inside of it
<asp:CheckBox runat="server" Text="enable" ID="cbOne" />
We have on both elements the Text field, why on the one we prefer to write out side... Look at this example, on Label, or On other similar controls the text that we may have to write may include characters that are not allowed inside the Text Property, maybe a complex css style or what ever... The check box from the other side is only include a small text (yes, not, something like that)
<asp:Label ID="lblLabel" runat="server">
This is a <b>"label"</b>
<br />And one more line
</asp:Label>
and more example that compiles
<asp:Label ID="lblLabel" runat="server">
This is a <b>"label"</b>
<br />And one more line
<asp:Literal runat="server" ID="ltrOneMore">One more Control Inside</asp:Literal>
</asp:Label>
---but this is not compile--
<asp:Label ID="lblLabel2" runat="server"
Text="This is a <b>"label"</b>
<br /> and one more line"
/>
At the final end is a decision that the makes make - maybe we need to ask them for the real real reason.
Now this is also not compile
<asp:CheckBox runat="server" ID="cbMyChbx">one<asp:CheckBox>
check box when is render on page is use two controls, one input and one label, so they maybe need to help user to understand that the text is not going on the input control.
<asp:CheckBox />
Because the element has no content, you can close the tag with /> instead of using a separate closing tag
<asp:Label></asp:Label> or <asp:Label />
Displays static text on a Web Forms page and allows you to manipulate it programmatically.
Learn more about it Web Server Control
All the answers above are valid, but something additional. All the asp controls are eventually rendered as HTML controls and that also defines how the asp controls behave. For e.g. it is not necessary that text in a label is always set as
<asp:Label runat="server" ID="lblOne">better start programming now</asp:Label>
it can be also done as follows
<asp:Label runat="server" ID="lblOne" Text="better start programming"></asp:Label>
now both are correct format, so it is not valid to say that any control which needs content will have a separate closing tag. It also depends on how it rendered in HTML. for e.g by default asp Label is rendered as a span and doesnt conform to XHTML standards. Hope this makes it clear, always think of how it will be rendered and ASP tries to adhere to what eventually will be rendered.
cheers

The server tag is not well formed when puting data in label

In my code I have a repeater and in it's datasorce i have a property FirstName. When I try to get the data from it I get this error:
The server tag is not well formed.
The problematic code is:
<asp:Label ID="lblOName" runat="server" Text="<%# Eval("FirstName") %>"></asp:Label>
What am I missing?
Never mind I fixed it. I just had to replace the quotes to:
<asp:Label ID="lblOName" runat="server" Text='<%# Eval("FirstName") %>'></asp:Label>
You should also use a literal control rather than a label in most cases.
A label should really only be used as a 'Label' for another control. So for example a label would be used next to a textbox input to inform people of what info to put in the textbox.
A literal should be used for all other eg non label text.

ASP.NET server tags rendered in client HTML, not values?

Maybe I've forgotten how to use these, but I am going crazy trying to inject a server-side value into an HTML output. There are reasons why I am doing this inline, and not server-side, so please don't suggest that as a solution.
This code on the server side:
<asp:Label ID="Label1" runat="server" Text='<%= DateTime.Now.ToString() %>' />;
Renders as this in the client HTML sent to the browser:
<span id="Label1"> <%= DateTime.Now.ToString()></span>;
And it displays as big fat empty space, and nothing output to the interface.
If I change the ASP source to using the "#" character to define as data-binding syntax, then the rendered output to browser becomes:
<span id="Label1"></span>
EDIT:
Setting Label text was just a simplified object for the sake of asking the question. In real life, I am setting the CssClass attribute, which does not allow me to use the "wrapping" workaround some have suggested. I wanted to set a public property and have all the controls update from it dynamically on page load.
Ideally, since I already have all the controls laid out on the aspx page. Just looking to add an attribute. I wanted to have:
<asp:textbox ID='MyTxtBox1' CssClass='<% strVal1 %>' />
<asp:textbox ID='MyTxtBox2' CssClass='<% strVal1 %>' />
<asp:textbox ID='MyTxtBox3' CssClass='<% strOtherVal %>' />
<asp:textbox ID='MyTxtBox4' CssClass='<% strVal1 %>' />
Now what it looks like I need to do is repeat all my (250+) controls on the codebehind in a block of code that looks like:
MyTxtBox1.CssClass=strVal1
MyTxtBox2.CssClass=strVal1
MyTxtBox4.CssClass=strVal1
MyTxtBox3.CssClass=strOtherVal
I believe that may not work on a compiled Web Application as it's not interpreted at run-time like a C# "Web Site". However, I was able to get it to work wrapping the label around the value:
<asp:Label runat="server"><%= DateTime.Now.ToString() %></asp:Label>
Set the Label1.Text = value instead of trying to use server side attrs inside of the server control

How can I prevent duplication of the content in itemtamplate for the alternating template in repeater?

Is there a way to prevent duplication ot an itemtemplate content which will just appear with a different css class for the alternating template block?
<asp:Repeater ID="rptCommentHistory" runat="server">
<ItemTemplate>
<asp:Label ID="lblComment" runat="server" Text='<%#Eval("Comment").ToString() %>'
Class="itemTemplate"/>
</ItemTemplate>
<AlternatingItemTemplate>
<asp:Label ID="lblComment" runat="server" Text='<%#Eval("Comment").ToString() %>'
Class="alternatingTtemTemplate"/>
</AlternatingItemTemplate>
</asp:Repeater>
This should do what you want:-)
<asp:Repeater ID="rptData" runat="server">
<ItemTemplate>
<asp:Label ID="lblData" runat="server" Text='<%# Eval("Comments") %>' CssClass='<%# Container.ItemIndex%2==0?"itemTemplate":"alternatingTtemTemplate" %>'></asp:Label>
</ItemTemplate>
</asp:Repeater>
I never make use of the AlternatingItemTemplate. I don't like having to duplicate my code for the purpose of having an alternating item, and I think that if the code is that different that it cannot be classed as a duplicate, then you shouldn't be using a Repeater control anyway.
Therefore I always just use the ItemTemplate, and make any changes I need to in the ItemDataBound event.
To determine whether the item is a normal, or alternating item, I would do something like:
if ((e.Item.ItemIndex+1 % 2)=0){
//Alternating code here..
}
In your case the only difference is a change to the Label CssClass, so I would do something like:
if ((e.Item.ItemIndex+1 % 2)=0){
Label lblComment = e.Item.FindControl("lblComment");
lblComment.CssClass = "alternatingTtemTemplate";
}
It's not really duplication as such, don't worry about it. You are implementing this pretty much as intended.
The only other option I can think of would be to use the itemtemplate, write some code that is fired on the itemdatabound event and programatically change the css class using FindControl.
I know which I would rather use...
As it seems that the proposed solutions are not satisfying and that there are no other ".net" solution, I would suggest that you do that in javascript (as it doesn't exist in pure CSS).
jQuery allows you to apply a different style on even and odds elements of the selector.
It should work with other elements than table rows...
So something like this should work :
$("#rptCommentHistory span:even").addClass("itemTemplate");
$("#rptCommentHistory span:odd").addClass("alternatingTtemTemplate");
Or you can just set ItemTemplate to all elements and use (it may perform better) :
$("#rptCommentHistory span:odd").removeClass("ItemTemplate").addClass("alternatingTtemTemplate");

Override FormView Templates

By default the FormView control creates html like :
ID <asp:TextBox ID="IdTextBox" runat="server" Text='<%# Eval("ID") %>' />
<br />
Name <asp:TextBox ID="NameTextBox" runat="server" Text='<%# Eval("Name") />
I prefer:
<ol class="form-layout">
<li><asp:Label AssociatedControl="IdTextBox" runat="server">ID:</aspLabel><asp
....
</ol>
My plan is to create a new control ( OrderedListFormView ) that inherits the FormView and overrides the method that generates the default "crap" html. I have been unable to find the method. Can anyone help? Do you have a better solution that costs $0 dollars?
I would prefer to change the default behavior at design time.
You sound like you have the ASP.NET form blues. Have you tried ASP.NET MVC? It gives you far better control of your rendered HTML, and you can mix it in with existing ASP.NET applications.
Try using Control Adapters to change the rendered HTML from a FormView, there is a tool kit and are pretty easy to code
http://weblogs.asp.net/scottgu/archive/2006/09/08/CSS-Control-Adapter-Toolkit-Update.aspx
http://msdn.microsoft.com/en-us/magazine/cc163543.aspx

Resources