Change rendering order of controls - c#

I found this article on how to manipulate the rendering sequence of asp.net controls.: http://weblogs.asp.net/infinitiesloop/archive/2007/09/07/rendering-asp-net-controls-out-of-order.aspx
I placed some placeholders on the page to encapsulate the controls i want to move around. The problem is, that RenderChildren does render the controls without the html i placed into the placeholder like this:
<asp:PlaceHolder id="phOneToMove" Runat="server" Visible="true">
<tr>
<td><asp:Literal id="label1" Runat="server">Caption</asp:Literal></td>
<td>
<asp:TextBox ID="textbox1" runat="server"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="validator1" Enabled="true" ControlToValidate="textbox1" runat="server" EnableClientScript="False" ErrorMessage="error"></asp:RequiredFieldValidator>
</td>
</tr>
</asp:PlaceHolder>
The controls are rendered without the tr and td around.
How can I handle this? All I want is to change the order of the placeholders like in this example phOneToMove.
Note: I am running on asp.net 2.0.

HTML tags are stored as controls of tyep Literal or LiteralControl, so however th elogic works has to check for controls of that type to ensure they get to their destination too.

I found the solution. My problem was, that the literal html elements (not LiteralControls) were not in the Controls property of my usercontrol. I did not know why. So I started to debug the .net Framework code itself ans setup a project with no overhead.
Short: I had code in the ascx file itself like <b><%= myOutputVariable %></b>. This causes asp to not put the literal content of a file into the controls collection. After I removed this code it worked like expected.

Related

<%= %> tag not working to display content inside an ASP Label control

Noob question.
Why does this not work into my .aspx file?
<body>
<asp:Label ID="Label1" runat="server" Text='<%=System.DateTime.Today.Day.ToString()%>' ></asp:Label>
</body>
It does display the <%=System.DateTime.Today.Day.ToString()%> string which is obviously not what I want.
Same result if I try to display the content of a code behind variable:
<asp:Label ID="label" runat="server" Text='<%= versionNumber %>' >
versionNumber being properly instanced and set into the code behind.
You cannot mix server controls with code blocks.
There are two ways to work around that limitation:
Just use <%=System.DateTime.Today.Day.ToString()%> without a Label around it
Use codebehind to set Label1.Text = System.DateTime.Today.Day.ToString();
The first way will display the date to the user, but you cannot further change it from codebehind.
The second way does enable you to alter the text from codebehind.
It is true you can't mix server controls with Code blocks,
If its compulsory for you to use Server side control, and you don't even want to set value from code behind then you can go for this solution.
<asp:Label ID="Label1" runat="server"><%=System.DateTime.Today.Day.ToString() %></asp:Label>
Similarly you can use code behind variable as follows ,
<asp:Label ID="Label1" runat="server"><%=versionNumber %></asp:Label>
If you really want to use a asp:Label
Use it as follows:
<asp:Label ID="Label1" runat="server"><%=System.DateTime.Today.Day.ToString() %></asp:Label>

Set C# Inline Expressions in ASP.NET Controls On Server-Side

Is it possible to programatically insert C# Inline Expressions as the values for ASP.NET Controls in your server-side code?
I'm currently using a DataList to display a lot of data in a table. I want to be able to dynamically change the columns of that table, so I need to be able to edit what controls are in its ItemTemplate.
However, in addition to editing the controls in the ItemTemplate, I need to be able to alter the value that is binded to each control in the template, because I want to dynamically change what is being displayed.
So what I currently have is a static table that doesn't change:
<asp:DataList ID="dataList" runat="server" OnSelectedIndexChanged="dataList_OnSelectedIndexChanged" DataSourceID="peopleData">
<ItemTemplate>
<table>
<tr>
<td>
<asp:LinkButton ID="NameLink" OnClientClick="onPageUpdate()" CommandName="Select" Text='<%# Eval(this.Name) %>' ForeColor='<%# Eval("this.NameColor") %>' runat=Server" />
</td>
<td>
<asp:Label Text='<%# Eval("this.Values[\"Age\"]") %>' ForeColor='<%# Eval("this.ValueColors[\"Age\"]") %>' runat="Server">
</td>
// OTHER COLUMNS WITH DIFFERENT DATA
</tr>
</table>
</ItemTemplate>
</asp:DataList>
// OBJECT DATA SOURCE CODE
I know how to dynamically add Controls to the ASPX web page. However, I don't know how to add the inline expressions.
I've tried doing the following but it doesn't work because of the type mismatches:
Label label = new Label();
label.Text = "<%# Eval(\"this.Values[\\\"Age\\\"]\") %>";
label.ForeColor = "<%# Eval(\"this.ValueColors[\\\"Age\\\"]\") %>";
Is there a way of achieving this or doing something similar?
My only other option that I can think of right now is to scrap using the DataList and ItemTemplate and just generate each row myself.. That's a lot more coding versus just using the ItemTemplate.
Not sure if this would help you, but look at http://msdn.microsoft.com/en-us/library/system.web.ui.databinder(v=vs.100).aspx
It is showing using of the hand-written exposure of properties. If your property were always call "magic" and you return the appropriate value for magic within your code would that get you what you need?

how do i response.write a block of code?

In php you can have the serverside script print back a block of formatted code by using something like
print <<<HERE
<code>
<somemorecode></somemorecode>
</code>
HERE;
What is the asp equivalent (C#)??
It doesn't seem to like when I don't keep all of the string on one line and i'd like to avoid having to concatenate everything as it kind of kills the purpose of preserving the formatting for readability. Here's what I have that it isn't liking. i know why its doing it, but I'd like to know how to achieve what you can in php:
<%
for(int i = 20; i<21;i++){
Response.Write("
<tr>
<td class="style1">
<asp:DropDownList ID="docNumber" runat="server" />
</td>
<td class="style1">
<asp:Label ID="Label1" runat="server" Text="This would be the title of the document." /></td>
<td class="style1">#</td>
<td class="style1">
<asp:DropDownList ID="supervisorName" runat="server" />
</td>
</tr>");
%>
This isn't for anything anyone will ever see. I'm just trying to build a throw away data entry page for myself.
update: nevermind... i just realized this isn't going to work for me as it will duplicate each control id. fail.
At any rate, for future reference. is there a way to do what i was trying to do as far as the code blocks go?
You might consider using an <asp:Repeater /> for this purpose:
<asp:Repeater runat="server" id="repeater1">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td class="style1">
<asp:DropDownList ID="docNumber" runat="server" /></td>
<td class="style1">
<asp:Label ID="Label1" runat="server"
Text="<%# Container.DataItem("DocTitle") %>"/></td>
<td class="style1">#</td>
<td class="style1">
<asp:DropDownList ID="supervisorName" runat="server" /></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
In your code behind, you would then bind this to a list or datasource on page load (or where ever appropriate). Finally check out the ondataitembound action for custom handling of each item as it's bound.
MSDN Repeater - Everything you need to know
&lt%= Some String Expression Here %>
or
&lt% code with Response.Write("some string"); %>
the first is better if you want to print a single statement. The second is better if you have more than one expression that prints stuff (if you use loops, if/else, etc.)
Keep in mind that this is considered bad practice and is frowned upon in the ASP.NET Web Forms world. You should be using controls.
If you want to create some more complex layout that repeats something you should use databound controls. For tables use the GridView control, for list of options use DropDownList and for other things use layout use the ListView control (you can use it for tables too). These controls use default formatting or template for each item and are bound to a collection of items via code behind. All this is for ASP.NET Web Forms. If you are using ASP.NET MVC things are done differently.

Show and hide labels with assosicatedControls and their controls

I want to show and hide a label and its control. I can do this in c# in the code behind. But, I can only show/hide the control. Any ideas?
<asp:label AssociatedControlID="thisLabel" runat="server">This:
<asp:label ID="thisLabel" CssClass="ascontrol" runat="server" />
</asp:label>
I want to be able to show and hide that whole thing depending on what user gets to the page. I just need to know how to show/ hide that whole thing in the c# code behind...cannot seem to get the visibility of the wrapper label to go away.
You haven't supplied a server-side Id:
<asp:Label ID="label_MyControl" AssociatedControlID="txt_MyControl" runat="server" />
<asp:TextBox ID="txt_MyControl" runat="server" />
What you've done is nest a asp:Label control within another asp:Label control....
Since I normally hide more than one field contiguously, I tend to wrap the whole thing in an asp:Panel and hide the panel. However, that's just my particular usage. But since it's my usage, I tend to block those sorts of things out into panels even for something as simple as your example.
Just my nickel's worth, your mileage may vary, as always.
It should work if you get you r markup correct, like this:
<asp:Label ID="lblYear" runat="server" Text="Year (yyyy):"
AssociatedControlID="txtYear"></asp:Label>
<asp:TextBox ID="txtYear" runat="server" Columns="30" MaxLength="4"></asp:TextBox>
Then in the code behind you could have:
lblYear.visible = False
txtYear.Visible = False
Now, my understanding of the "AssociatedControlID" property of an asp:label is mainly for accessibility purposes. You don't need to have the AssociatedControlID value set to make things work as I've shown.

Why can't I set the asp:Label Text property by calling a method in the aspx file?

Can somebody please explain this to me:
I have a label and I want to be able to set the Text property by calling a method in the aspx file. It works fine if I set the property in code behind, but I really want to set this property in the aspx file.
I have tried a couple of things, but what I expected to work was this:
<asp:Label ID="Label1" runat="server" Text=<%# GetMyText("LabelText") %> />
I get no errors when doing this, but my method is never called and the Text property is left empty.
Is it not possible to set property values to server side controls directly in the aspx without using resources or use hard coded values?
Update: My first try was:
<asp:Label ID="Label1" runat="server" Text=<%= GetMyText("LabelText") %> />
But that results in the following error:
Server tags cannot contain <% ... %> constructs.
The syntax =<%# ... %> is Data binding syntax used to bind values to control properties when the DataBind method is called.
You need to call DataBind - either Page.DataBind to bind all the controls on your page, or Label1.DataBind() to bind just the label. E.g. add the following to your Page_Load event handler:
if (!IsPostBack)
{
this.DataBind();
// ... or Label1.DataBind() if you only want to databind the label
}
Using Text='<%= GetMyText("LabelText") %>' as others have proposed won't work as you'll find out. This syntax is inherited from classic ASP. It can be used in some circumstances in ASP.NET for inserting dynamic values in static HTML, but can not be used for setting propeties of server controls.
The sysntax you are looking for is <%= %> the # is for data binding. So your code should read:
<asp:Label ID="Label1" runat="server" Text='<%= GetMyText("LabelText") %>' />
EDIT: This answere is incrrect
I am leaving this answer here because lots of people agreed with me that this is infact the correct answer, but it will not work. This line of code will produce the following HTML output:
<span id="Label1"><%= GetMyText("LabelText") %></span>
Try this:
<asp:Label ID="Label1" runat="server" Text='<%= GetMyText("LabelText") %>' />
Edit
Yep. I was wrong. #Joe was right.
However, THIS works (and I'm not sure what the difference is):
<asp:Label ID="Label1" runat="server"><%= GetMyText("LabelText") %></asp:Label>
CodeBehind:
protected string GetMyText(string input)
{
return "Hello " + HttpUtility.HtmlEncode(input);
}

Categories

Resources