Repeater: databind server-side ID in item control? - asp.net

When binding a datasource to a Repeater control, is it possible to databind to the ID property of a server-side control inside the ItemTemplate? like so:
<asp:Repeater ID="rptToolTips" runat="server">
<ItemTemplate>
<telerik:RadToolTip ID="tt<%# Eval("Name") %>" ClientIDMode="Static" runat="server">
<div class="tip">
<div class="segName">Segment #<%# Eval("Name") %></div>
<div>Flow:<%# Eval("Flow", "{0:N}") %></div>
</div>
</telerik:RadToolTip>
</ItemTemplate>
</asp:Repeater>
...here I'm trying to set the RadToolTip's ID property to "tt[name-value]". I've tried a few variants but they're invalid:
ID="tt<%# Eval("Name") %>"
ID='tt<%# Eval("Name") %>'
ID="tt<%# Eval('Name') %>"

It's not possible. A server control's ID is set at the time it's being created or at design time. Once it's set you cannot reset it during the data binding.
But, if you are trying to use this ID value in JQuery or JavaScript you could employ the following hack.
Add a custom property to your control (give it any name you like and in this case I'll use myId)
myId='<%# Eval("Name") %>'
No you can find this element by this myId property
Hope this helps.

Try this to assign ID
ID=' "tt" + <%# Eval("Name") %>'

Also see this answer by me to one of the questions at SO for another way (using pure JavaScript's this object rather than using a custom property to get the clientID) of doing this.
Just thought of keeping these linked for future reference.

Related

ASP.NET Linkbutton not getting dynamic value for commandargument value

<%
foreach (training t in traininglist)
{
%>
<tr>
<td><%=t.TrainingId%></td>
<td><%=t.TrainingName%></td>
<td>
<asp:LinkButton runat="server" ID="EditBtn"
Text="Edit" OnCommand="editbtn_OnCommand"
CommandArgument='<%# t.TrainingId %>' CommandName="edit" />
</td>
</tr>
<% } %>
where,
training is the class and traininglist is List<training> defined in Page_Load() function in codebehind.
I am trying to call the
public void editbtn_OnCommand(object sender, CommandEventArgs e)
{
String myeid = e.CommandArgument.ToString();
....
}
Here, myeid is not getting value but always shows <%# t.TrainingId %>
i have already tried all other options like <%: t.TrainingId %> or <%=t.TrainingId %>
The output of Tag "<%= %>" is more likely similar to use Response.Write in your code. so these tags are used to display the value to the response object.
That's why,as per my understanding, You can't used these tags to set commandargument property of controls unless you are using DataBinding. If you are using DataBinding then these tags "<%= %>" are used to set the property of controls.
Because you are here looping through each item in list on html table, my suggestion is to use GridView or Repeater and then Bind through your List Object. if you are using this way, you can get rid of unwanted formatting issues of html tables also.
Refer http://msdn.microsoft.com/en-us/library/6dwsdcf5(VS.71).aspx
If you want to use repeater then you can use these specific tags, and this should be your code(not actual code, just sample one)
<asp:Repeater id="myRepeater" runat="server" >
<ItemTemplate>
<div>
<asp:LinkButton runat="server" id="EditBtn" CommandName="edit"
CommandArgument="<%#Container.DataItem.TrainingId %>" Text="Edit"
OnCommand="editbtn_OnCommand" />
</div>
</ItemTemplate>
</asp:Repeater>

Is it possible to set up declarative control binding in the codebehind?

In ASP.NET WebForms, you can be all like:
<ItemTemplate>
<asp:Label ID="mylabel" Text='<%#Eval("myvalue") %>' runat="server"></asp:Label>
</ItemTemplate>
This declarative binding gets the value from the proper column/member in the datasource. Is it possible to do this in the codebehind:
mylabel.Text = String.Format("<%# Eval(""{0}"") %>", ControlName)
Edit: inb4 have you tried it? Yes. I will add that there's some parsing somewhere that actually prevents that string from showing when my grid binds, so I know I am touching onto something here.

ASP.NET Repeater control with nested repeaters

I have a nested repeater control. Here is what the HTML looks like:
<ItemTemplate>
<div class="boxLeft">
<h4><%# DataBinder.Eval(Container.DataItem, "DisciplineName") %></h4><asp:Label runat="server" ID="lblDisciplineID" Visible="false" Text='<%# DataBinder.Eval(Container.DataItem, "DisciplineID") %>'></asp:Label>
<p><%# DataBinder.Eval(Container.DataItem, "DisciplineNarrative") %></p>
<h5 class="articles"><%# DataBinder.Eval(Container.DataItem, "InstructorCount")%> instructors</h5>
<ul>
<asp:Repeater runat="server" ID="rptRegions">
<ItemTemplate>
<li><a href='/Lessons/<%# DataBinder.Eval(Container.DataItem, "DisciplineName") %>/<%# DataBinder.Eval(Container.DataItem, "CityName") %>'><%# DataBinder.Eval(Container.DataItem, "CityName") %></a></li>
</ItemTemplate>
</asp:Repeater>
</ul>
</div>
</ItemTemplate>
As you can see, I want to access the parent DisciplineName property from the parent repeater, in my child repeater in order to build the URL. I get the following error:
DataBinding: 'GolfLessonSearch.Model.CityEntity' does not contain a
property with the name 'DisciplineName'.
This is because it is trying to get "DisciplineName" from the child repeater but I want it from the parent repeater. I thought that the properties may still be in scope but it appears not. Is there any way of getting this?
If the objects behind your DataItems have the same parent/child relationship as you are trying to represent them in the repeater, you can always fully qualify the property name so that your child repeater is calling
DataBinder.Eval(Container.DataItem, "Parent.DisciplineName")
If this isn't the case, my gut would be to create a ViewModel object (even though it's not mvc) of your child object to fake that relationship...
EDIT
Just a note that when I said "Parent.DisciplineName", I meant to replace "Parent" with the object name... (I only qualify because "Parent" is a reserved word in so many other places in asp.net...

asp.net repeater with usercontrol - setting commandargument

I have the following repeater code:
<asp:Repeater ID="repMain" runat="server" OnItemCommand="repMain_ItemCommand" EnableViewState="false">
<ItemTemplate>
<dmg:testcontrol runat="server" MyData=<%#Container.DataItem %>>
</dmg:testcontrol>
</ItemTemplate>
</asp:Repeater>
The testcontrol usercontrol looks like:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="TestControl.ascx.cs" Inherits="TestRepeater.TestControl" %>
<asp:Literal runat="server" ID="litMain" Text="<%#MyData.MyValue %>"></asp:Literal>
<asp:DropDownList runat="server" ID="dropdownMain"></asp:DropDownList>
<asp:Button runat="server" ID="btnMain" Text="Click Me" CommandName="Update" CommandArgument="<%#dropdownMain.SelectedValue%>"/>
Is it possible for me to send through the dropdownMain.SelectedValue as the CommandArgument?
Just now it is an empty string.
Thanks
Duncan
PS This is related to ASP.NET Repeater not binding after ItemCommand but I thought the two sufficiently different to keep apart.
A bit old question but I just my self found the answer to this one.
CommandArgument="<%#dropdownMain.SelectedValue%>"
Needs to look like this instead with single quotes! All inline codes within a asp.net controls have to be done this way instead.
CommandArgument='<%#dropdownMain.SelectedValue%>'
Why not get the selected value, and use it inside the Command function ?
(why to try to send it as argument, from the moment you can get it inside the command called function and its the same)

Formatting DataBinder.Eval data

How can I format data coming from a DataBinder.Eval statement in an ASPX page?
For example, I want to display the published date of the news items in a particular format in the homepage. I'm using the ASP.NET 2.0 Repeater control to show the list of news items.
The code for this goes like this:
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="ObjectDataSource1">
<HeaderTemplate><table cellpadding="0" cellspacing="0" width="255"></HeaderTemplate>
<ItemTemplate>
<tr><td >
<a href='/content/latestNews.aspx?id=<%#DataBinder.Eval(Container.DataItem, "id") %>'>
<asp:Label ID="lblNewsTitle" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "title") %>'></asp:Label>
</a>
</td></tr>
<tr><td>
<asp:Label ID="lblNewsDate" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "publishedDate"))%>'></asp:Label>
</td></tr>
</ItemTemplate>
<FooterTemplate></table></FooterTemplate></asp:Repeater>
Is there a way I could call a custom method with the DataBinder.Eval value as its parameter (something like below)?
<asp:Label ID="lblNewsDate" runat="server" Text='<%# GetDateInHomepageFormat(DataBinder.Eval(Container.DataItem, "publishedDate")) )%>'></asp:Label>
If yes, then where do I write the GetDateInHomepageFormat method? I tried out in the code behind page but got a run time error?
If this is not possible, is there a way to do inline formatting?
There is an optional overload for DataBinder.Eval to supply formatting:
<%# DataBinder.Eval(Container.DataItem, "expression"[, "format"]) %>
The format parameter is a String value, using the value placeholder replacement syntax (called composite formatting) like this:
<asp:Label id="lblNewsDate" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "publishedDate", "{0:dddd d MMMM}") %>'</label>
After some searching on the Internet I found that it is in fact very much possible to call a custom method passing the DataBinder.Eval value.
The custom method can be written in the code behind file, but has to be declared public or protected. In my question above, I had mentioned that I tried to write the custom method in the code behind but was getting a run time error. The reason for this was that I had declared the method to be private.
So, in summary the following is a good way to use DataBinder.Eval value to get your desired output:
default.aspx
<asp:Label ID="lblNewsDate" runat="server" Text='<%# GetDateInHomepageFormat(DataBinder.Eval(Container.DataItem, "publishedDate")) )%>'></asp:Label>
default.aspx.cs code:
public partial class _Default : System.Web.UI.Page
{
protected string GetDateInHomepageFormat(DateTime d)
{
string retValue = "";
// Do all processing required and return value
return retValue;
}
}
Hope this helps others as well.
Why not use the simpler syntax?
<asp:Label id="lblNewsDate" runat="server" Text='<%# Eval("publishedDate", "{0:dddd d MMMM}") %>'</label>
This is the template control "Eval" that takes in the expression and the string format:
protected internal string Eval(
string expression,
string format
)
http://msdn.microsoft.com/en-us/library/3d2sz789.aspx
You can use a function into a repeater like you said, but notice that the DataBinder.Eval returns an object and you have to cast it to a DateTime.
You also can format your field inline:
<%# ((DateTime)DataBinder.Eval(Container.DataItem,"publishedDate")).ToString("yyyy-MMM-dd") %>
If you use ASP.NET 2.0 or newer you can write this as below:
<%# ((DateTime)Eval("publishedDate")).ToString("yyyy-MMM-dd") %>
Another option is to bind the value to label at OnItemDataBound event.
This line solved my problem:
<%#DateTime.Parse(Eval("DDDate").ToString()).ToString("dd-MM-yyyy")%>
To format the date using the local date format use:
<%#((DateTime)Eval("ExpDate")).ToString("d")%>
How to Format an Eval Statement to Display a Date using Date Locale
Thanks to all. I had been stuck on standard format strings for some time. I also used a custom function in VB.
Mark Up:-
<asp:Label ID="Label3" runat="server" text='<%# Formatlabel(DataBinder.Eval(Container.DataItem, "psWages1D")) %>'/>
Code behind:-
Public Function fLabel(ByVal tval) As String
fLabel = tval.ToString("#,##0.00%;(#,##0.00%);Zero")
End Function
Text='<%# DateTime.Parse(Eval("LastLoginDate").ToString()).ToString("MM/dd/yyyy hh:mm tt") %>'
This works for the format as you want
<asp:Label ID="ServiceBeginDate" runat="server" Text='<%# (DataBinder.Eval(Container.DataItem, "ServiceBeginDate", "{0:yyyy}") == "0001") ? "" : DataBinder.Eval(Container.DataItem, "ServiceBeginDate", "{0:MM/dd/yyyy}") %>'>
</asp:Label>
You can use it this way in aspx page
<%# DataBinder.Eval(Container.DataItem, "DateColoumnName", "{0:dd-MMM-yyyy}") %>

Resources