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

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?

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>

Auto add a new row into Repeater Control when it has a new row inserted into SQL Server 2008 DB

This is so hard to understand, I'll try to describe my problem so clearly.
I have a Table in SQL DB:
COMMENT(idcom, content,user,idtopic);
I use a Repeater Control to display all the comment in Database:
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<table>
<tr>
<td>
</td>
<td>
<asp:Label ID="lblcontent" runat="server" Text='<%#Eval("content") %>'>
</asp:Label>
</td>
<td>
<asp:Label ID="lbluser" runat="server" Text='<%#Eval("user") %>'>
</asp:Label>
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
My problem is: When client add a new comment (in another page)--> insert a new row into COMMENT table---> I have to load all the comment into Repeater again (both old comments and new comments).---> therefor, I use <UpdatePanel> and <Timer Control> Ajax Toolkit to update every 10sec. It's so amateur and not a good solution.
How can I just only load the new comment (new row) into the Repeater in every 10sec.
I always think Repeater is fixed and we cannot insert the single row.
Help! Can you give me some advice to insert single row into Repeater by load data from Database (not input directly)???
I'd say the best way to do this is with some Javascript/jQuery plugin + AJAX queries. ASP.NET Webforms does not usually works that way (it is prefered from the webform point of view to go through the entire page lifecycle for each update).
When you first populate your repeater, store the List and the biggest IdComm in variables, So you will use it in all subsequent selects you do in Database again.
Use the IdComm stored as a parameter and get all the new comments with IdComm bigger than the IDComm you have passed as parameter.
After that, add this new lines in the first list you have requested in the first request and populate the Repeater again in this updatePanel you are using.

HtmlInputTextbox showing an error

I designed my page with an htmlinputtext as follows
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblTrialPeriodEnds" runat="server" Text='<%# Eval("trialPeriodEnds","{0:dd/MM/yyyy}") %>'></asp:Label>
</ItemTemplate>
<HeaderTemplate>
TrialPeriodEnds
</HeaderTemplate>
<EditItemTemplate>
<input type="text" name="testinput" class="textbox" value='<%# Eval("trialPeriodEnds") %>'
id="txtEffectiveDate" runat="server" />
<a onclick="showCalendarControl('txtEffectiveDate')" href="#">
<img alt="cal" src="calendar.gif" style="width: 20px; height: 20px" border="0" /></a>
</EditItemTemplate>
</asp:TemplateField>
My code in rowupdating is as follows
HtmlInputText htmInput = (HtmlInputText)GridView1.Rows[e.RowIndex].FindControl("Daily");
strExpiry = htmInput.Value;
I am getting an error as Object reference not set to an instance of an object.
Why it is displaying can any on tell
The input that you are using is an HTML input tag. In order for ASP.NET to see this tag, you must include a runat="server" attribute. This will indicate to ASP.NET that it should track the tag and you will be able to interact with it from your code. Without this tag, ASP.NET does not know about the control and therefore it does not show up when you call FindControl.
Note that when you make ASP.NET aware of the control, it will may modify the ID. This is so that ASP.NET ensures that each control has a unique ID. Your example has the control within a GridView, so the input control's ID will be modified so that ASP.NET knows that the control is a child of the GridView. You will need to modify any client side scripting to be aware of the new ID. I caution against checking the ID in HTML and then simply assigning the new ID as modifications of the ASP.NET structure can change the ID. You mentioned in your comment that you are using jQuery. I would recommend using a class and have jQuery assign the control based on the class of the input control.

Change rendering order of controls

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.

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