C# - Reusing The Same Repeater ItemTemplate

Mar 11, 2010

I'm currently using a certain ItemTemplate for three repeaters that are all on the same page, but they are all bound to different data sources. Is there any way to refactor my web form without using a user control to easily refer to this template so I will only have to define it once?

View 2 Replies


Similar Messages:

C# - How To Vary ItemTemplate Inside An Asp:Repeater

Apr 4, 2011

I have a user control which is used to display search results. The HTML for each result displayed will vary based on the type of result being displayed: "contacts" are displayed in one way, "news articles" are displayed in another, etc. There are around 10 different types of results that are all marked up differently when they get to HTML — so I need around 10 or so different templates for individual results that I can choose between based on the current item being displayed.

I'm using an asp:Repeater to display the results, but I don't know how to select the appropriate template within the asp:Repeater <ItemTemplate>. Ideally I'd like the ASP to select the appropriate template to use based upon the object type being passed in via the searchResultsRepeater.DataSource — but unfortunately I can't use switch on type (see this blog entry for C# switch on type). I can however just pass through an enum value for the type of result being displayed.

In the backend C# code I have an abstract inline SearchResult class, and children of that class like ContactSearchResult, NewsArticleSearchResult, etc. The searchResultsRepeater.DataSource would then be bound to a List<SearchResult>. Each SearchResult contains a ResultListingType type field which gives the type of the listing to be displayed.

Attempt 1: using control flow inside the ASP itself

My first attempt was something like this:

<asp:Repeater ID="searchResultsRepeater" runat="server">
<ItemTemplate>
<div class="item">
<% switch (DataBinder.Eval(Container.DataItem, "type")) { %>
<% case ResultListingType.CONTACT: %>
<p><%# DataBinder.Eval(Container.DataItem, "firstName") %></p>
<p><%# DataBinder.Eval(Container.DataItem, "lastName") %></p>
<% break; %>
<% case ResultListingType.NEWS: %>
<p><%# DataBinder.Eval(Container.DataItem, "newsHeadline") %></p>
<p><%# DataBinder.Eval(Container.DataItem, "newsDate") %></p>
<% break; %>
<% Case AnotherTypeOfListing1: %>
<% Case AnotherTypeOfListing2: %>
<% Case AnotherTypeOfListing3: %>
<% Case AnotherTypeOfListing4: %>
<% Case AnotherTypeOfListing5: %>
<% etc... %>
<% } %>
</div>
</ItemTemplate>
</asp:Repeater>

Unfortunately, this doesn't work:

"switch" and "if" both give "invalid expression term" inside the <%# ... %> brackets.
"Container.DataItem" gives "the name "Container" does not exist in the current context" inside <% ... %> brackets.

Attempt 2: setting asp:PlaceHolder's to Visible = False

I found something that looked useful at how to change the ItemTemplate used in an asp:repeater?. I then tried something like:

<asp:Repeater ID="searchResultsRepeater" runat="server">
<ItemTemplate>
<div class="item">
<asp:PlaceHolder ID="newsResultListing" runat="server">
<p><%# DataBinder.Eval(Container.DataItem, "newsHeadline") %></p>
<p><%# DataBinder.Eval(Container.DataItem, "newsDate") %></p>
</asp:PlaceHolder>
<asp:PlaceHolder ID="contactResultListing" runat="server">
<p><%# DataBinder.Eval(Container.DataItem, "firstName") %></p>
<p><%# DataBinder.Eval(Container.DataItem, "lastName") %></p>
</asp:PlaceHolder>
</div>
</ItemTemplate>
</asp:Repeater>

In my ItemDataBound event I did:

Control newsResultListing = e.Item.FindControl("newsResultListing");
newsResultListing.Visible = false;
Control contactResultListing = e.Item.FindControl("contactResultListing");
contactResultListing.Visible = false;
switch (item.type)
{
case ResultListingType.CONTACT:
contactResultListing.Visible = true;
break;
case ResultListingType.NEWS:
newsResultListing.Visible = true;
break;
default:
throw new Exception("Unknown result listing type");
}

Unfortunately this doesn't work because ASP seems to still be running the contents of the PlaceHolder even after I set Visible = false. I get the error "DataBinding: 'usercontrols_ResultsListing+ContactResultsListing' does not contain a property with the name 'newsHeadline'" — i.e. the newsResultListing PlaceHolder is still looking for the "newsHeadline" field, even though that field doesn't exist for the result listing type being displayed.

In fact I've tried a quick test throw new Exception("e"); in my ItemDataBound, and it looks like the "DataBinding" error is thrown even before control flow gets to the ItemDataBound method, so there's really nothing I can do in there to avoid this error.

I suppose I could add every single field to the parent class and leave most of them null in my children, but that seems really ugly.

Is there a way to make this work, or an easier way to vary my ItemTemplate based upon the type of Container.DataItem I'm currently iterating over? I'm very new to ASP so there's likely something simple that I've missed.

View 2 Replies

.net - Asp Repeater Databound - At The End Creates A New ItemTemplate?

Jul 22, 2010

isn't present databound event on asp repeater server control?I just want to bind all my data, and at the end creates a new ItemTemplate and add it, but just when is all data binded

View 1 Replies

Setting ID To A Control Inside Repeater Itemtemplate?

Apr 1, 2010

Inside repeater's itemtemplate I have a Panel server control.I need to assign special Id for it, because I need to work with some javascript functions that use this Id.

In repeater ItemDataBound event I have this:

pnlButtonsPanel.ID = pnlButtonsPanel.ID + DataBinder.Eval(e.Item.DataItem, "ID");

But this solution is not good because after a postback the page is re -rendered and I lose the new ID. (And I don't want to rebind repeater after every postback)

I tried to set the ID on aspx page like that:

<asp:Panel id='<%# Eval("ID") %>'

and some other variations but always get compile errors.

View 1 Replies

Forms Data Controls :: Repeater With A Tag In The ItemTemplate

Jun 22, 2010

I have tried many different combinations to try to get this to work, but I cannot. In the ItemTemplate of a Repeater control, I have a <form> tag. For this third party button to work, each button needs to be surrounded by <form></form> tags. I cannot get the first iteration of the repeater to display the tag, but 2, 3 and beyond, it will. It doesn't make sense to me. Because of this, IE is displaying the page in a funky way.

To see the generated code on the page, http://AdvancedSurveillancePro.com/services.aspx

Here is my repeater code:

[Code]....

View 3 Replies

JQuery :: Using SlideToggle In ItemTemplate Of Repeater Control

Jan 19, 2011

can anyone tell how to use slideToggle function on LinkButton that is placed in ItemTemplate of a Repeater control. Upon clicking the LinkButton a Panel below that link will apear that contains other controls. But how can i write dynamic jQuery Script for each LinkButton ?

View 6 Replies

C# - Getting The Object Inside Repeater ItemTemplate With / Without Eval

Nov 13, 2010

I am new to Repeater and DataBinding

In PageLoad, I have

var photos = from p in MyDataContext.Photos
select new {
p,
Url = p.GetImageUrl()
};
repeater1.DataSource = photos;
repeater1.DataBind();

In the Repeater control, I have

<ItemTemplate>
<% Photo p = (Photo) Eval("p"); %> <!-- Apparently I can't do this -->
...
<asp:TextBox runat="server" ID="txtTime" Text='<%= p.Time == null ? "" : ((DateTime)p.Time).ToString("dd/MM/yyyy HH:mm:ss") %>' />
...
</ItemTemplate>

But that is wrong.

What I need is to get the Photo object in ItemTemplate so I can do things with it (eg. to display the time as in the second line in ItemTemplate above). Is it even possible to do this in a Repeater?

View 2 Replies

Forms Data Controls :: Using Rowspan In ItemTemplate Of Asp:repeater

Feb 22, 2011

I have the following problem and I hope some of you could help me because I'm sick and tired of trying to get this fixed.

I have an asp:repeater which has the following <ItemTemplate>

[Code]....

[Code]....

[Code]....

[Code]....

[Code]....

but it really looks like this.

View 1 Replies

C# - How To Access ItemTemplate Control From ItemCommand Event Using Repeater

Nov 26, 2010

My repeater:

<asp:Repeater ID="rptrContacts" runat="server" OnItemCommand="rptrContact_ItemCommand" >
<div ID="itemTemplate>
<ItemTemplate>
<%# Eval("Name") %>
<%# Eval("Email") %>
<asp:LinkButton ID="lbtnEditContact" runat="server" CommandName="Edit" Text="Edit" CommandArgument='<%# Eval("ContactID") %>' />
<asp:Label ID="lblUpdateConfirm" runat="server" Text="Update Confirmed" Visible="false" />
</ItemTemplate>
</div>
<div ID="editTemplate runat="server" visibility="false">
Update your Info:<br>
Name: <asp:TextBox ID="txtName" runat="server Text="<%# Eval("Name") %>"/> <br>
Email: <asp:TextBox ID="txtEmail" runat="server Text="<%# Eval("Email") %>"/><br>
<asp:LinkButton ID="lbtnUpdateContact" CommandArgument='<%# Eval("ContactID") %>' CommandName="UpdateContact" runat="server" >Update</asp:LinkButton>
</div>
</asp:Repeater

and code for ItemCommand:

switch(e.CommandName)
{
case "Edit":
//make editTemplate div visible
HtmlGenericControl divEditContact = (HtmlGenericControl)e.Item.FindControl ("divEditContact");
divEditContact.Visible = true;
break;
case "Update":
Employee updateEmployee = new Employee
{
employeeName = txtName.Text:
employeeEmail = txtEmail.Text:
}
updateEmployee = API.UpdateEmployee(updateEmployee);
//display lblUpdateConfirm visible to True
// so user sees this confirm messge in the newly updated ItemTemplate
}

How can I access my lblUpdateConfirm and turn its Text state to visible from inside the ItemCommand, so that when the user sees the newly updated ITemTemplate, the label is showing the "Update Confirmed" message?

View 1 Replies

Forms Data Controls :: Repeater ItemTemplate To Create A Div Container

Apr 16, 2010

In my application I have successfully pulled some rows from a database into a DataSet and then used an ASP.NET Repeater Control to create a div container for each row with each field contained within a span element as follows:

<asp:Repeater
id="repeater"
runat="server">
<ItemTemplate>
<div
id="ticketContainer">
<a
href="#"><img
src="~/Images/treeview_expand.png"
alt=""
runat="server"
/></a>
<span><%#DataBinder.Eval(Container.DataItem,
"USERNAME")%></span>
<span><%#DataBinder.Eval(Container.DataItem,
"COMPUTERNAME")%></span>
<span><%#DataBinder.Eval(Container.DataItem,
"CATEGORY")%></span>
<span><%#DataBinder.Eval(Container.DataItem,
"DATE")%></span>
<span><%#DataBinder.Eval(Container.DataItem,
"TIME")%></span>
<span><%#DataBinder.Eval(Container.DataItem,
"STATUS")%></span>
<p><%#DataBinder.Eval(Container.DataItem,
"DESCRIPTION")%></p>
</div>
</ItemTemplate>
</asp:Repeater>

My problem is that I want the .png graphic link to have a onclick event so that a new div is dynamically inserted under the div to show data from a seperate database table. However since its all declared within the ItemTemplate then targeting the correct div using the JS DOM would be difficult/impossible as the id for each of the divs will be the same.

If this is going to be infeasible then what is the possibility of having data brought in from multiple tables would I have to manipulate the DataSet to include the extra columns as I think Repeater Controls can only have a single DataSet (corret me if i'm wrong).

View 3 Replies

Forms Data Controls :: Work With Repeater Itemtemplate Coding?

Feb 7, 2010

I have the following code in an aspx form for an asp:Repeater

<ItemTemplate>
<tr class='<%#Container.ItemIndex % 2 == 0 ? "normalRow" : "alternateRow" %>' >
<tr>
<td>
<asp:LinkButton ID="LinkButton1" runat="server">blabla
<%#DataBinder.Eval(Container.DataItem, "ListName")%>
</asp:LinkButton>
</td>
</tr>
</ItemTemplate>

Which produces a syntax error (and a sqiggle blue underline) on

<%#Databinder.Eval(Container.DataItem, "ListName")%>

the build tells me I have an invalid character in the position before the opening <%
above

This only happens after I add the conditional class for the <tr> tag

"<tr class='<%#Container.ItemIndex..."

before I add the above line (to replace the <AlternatingItemTemplate>) everythings ok.

View 4 Replies

Web Forms :: Set Label In Repeater Itemtemplate To Fixed Length And Allow Multiple Line

Feb 4, 2010

how to set the label in repeater itemtemplate to fixed length and allow multiple line

View 3 Replies

Forms Data Controls :: Show Hyperlink On Condition In Repeater ItemTemplate

Nov 2, 2010

I have a Repeater displaying some data that doesn't do what I'd like to see. If a certain condition is met (ex: parent.NumOfChildren > 0) I would like a hyperlink shown. Here is what I have so far:

<asp:Repeater ID="parents" runat="server" >
<ItemTemplate>
<asp:HyperLink NavigateUrl='<%# "Children.aspx?parent=" &#43; DataBinder.Eval(Container.DataItem,"Parent_ID")%>'
Text='View Children' runat="server" target="_self" ID="Hyperlink2" NAME="Hyperlink2"/> -
</ItemTemplate>
</asp:Repeater>

In this case, the "View Children" link shows up for every item in my Repeater, but I'd like it shown only if parent.GetNumOfChildren() > 0. Would it be correct to try setting the Visible="true/false" property of the HyperLink? Is that the norm for this sort of problem? Or, is there a way to wrap the entire <asp:Hyperlink> ... </asp:Hyperlink> tag in an if statement that has a reference to the object that is currently being interated in the Repeater? Ie:

if(CurrentDataItemInIteration.GetNumOfChildren() > 0)
{
<asp:Hyperlink> ..... </asp:Hyperlink>
}

To me, the Visible="true/false" solution seems cleaner, if possible.

View 5 Replies

Forms Data Controls :: Conditionally Create A Hyperlink Within A Repeater ItemTemplate?

Dec 12, 2010

using a repeater to display multiple 'comments' related to a parent database entity (a 'task' in my case). When the user creates these comments they can optionally attach a file. Now, I'm working on the page that lists these comments and I'm not sure how to go about displaying the link to the attached file, if one is present. There may not be a file. The database record that contains the comment will also contain the url to the file if one was attached.

what are my options for this? I would like to just display a link to the file after the comment content, only if a file is available.

View 11 Replies

Forms Data Controls :: How To Alter A Control In A Repeater's ItemTemplate From A Handler

Mar 27, 2010

I have a repeater bound to a datasource that servers as a list of birds. When the user clicks an item in the list the bird's image is displayed on the page. I want to change the background color of the item that is currently selected in the OnClick handler for the main item in my repeater's ItemTemplate.

I have changed the styling of controls prgrammatically before and it worked fine but in those cases i used the ID of the control to directly access it in my code behind file. But since this is some arbitrary item in a repeater I don't know it's ID at runtime, all I have is the sender object passed to the control's OnClick handler. So in my code behind file, I tried casting the sender object passed to the OnClick handler to the appropriate type and used Style.Add("background-color", highlitColor) but it failed to change anything.

I had a thought - maybe this sender object gets passed by value so all I did was change the Style of a COPY of the control item.

If this is indeed my problem, how do I start with a casted sender object in an event handler and get a reference to the actual control in order to change it's Style?

View 13 Replies

AJAX :: Repeater Data Server Control Has HTMLEditor Inside Is ItemTemplate?

Jun 20, 2010

I am having big trouble with this issue, getting a message from IE that a script is being running for a long time and asking me to stop it myself.

The HTMLEditor seems to be uncomptible with this one.

I am also getting a very weak performance when adding this Ajax control.

I think this control is very heavy, but I'm not sure. If I am not wrong, this post I'm writing is inside the same control and I can see it takes time to load it in my page.

View 1 Replies

Data Controls :: Export Repeater Control With TextBox In ItemTemplate To Excel

Jul 17, 2015

I have a repeater control that is getting exported to excel. Well excel will no longer show the data that was in the textboxes. I've read the article on exporting a gridview having the same problem and how you replace the texboxes with literal contrls. I was wondering if there is an article like it only for a repeater control.

View 1 Replies

Web Forms :: Display Facebook Like Button Inside Repeater Control ItemTemplate

Jul 23, 2012

I have repeter where i display news headline and news. Five news bind each time. now i want to show facebook send button below with with each news.

View 1 Replies

Forms Data Controls :: Repeater ItemTemplate - If Eval(field)=0 Then HTML Else HTML

Feb 20, 2010

The following seems reasonable, but it returns an error:

<asp:Repeater ID="RepeaterF" runat="server" DataSourceID="DSF" >
<ItemTemplate>
<%
If Eval("Item_Batch") = 0 Then
%><tr><td></td><td colspan="2"></td><td></td></tr><%
Else %>........

Error: "Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control."

I am finding it difficult to accept that the whole Repeater approach has any benefits over just creating a loop in code, iterating through a recordset, and building a html table into a variable, then dumping it to the page. This repeater is spawning pages of code and objects, and surely this is all using up server resources.

View 2 Replies

C# - Reusing PagedList Object On WCF

Jan 6, 2011

I have a custom collection PagedList<T> that is being returned from my WCF service as PagedListOfEntitySearchResultW_SH0Zpu5 when T is EntitySearchResult object. I want to reuse this PagedList<T> type between the application and the service. My scenario:

I've created a PagedList<T> type that inherits from List<T>. This type is on a separated assembly that is referenced on both application and WCF service. I'm using the /reference option on the scvutil to enable the type reusing. I also don't want any arrays returned so I also use the /collection to map to the generic List type. I'm using the following svcutil command to generate the service proxy:

"C:Program Files (x86)Microsoft SDKsWindowsv7.0ABinNETFX 4.0 Toolssvcutil.exe"
/collectionType:System.Collections.Generic.List`1
/reference:.... inDebugApp.Utilities.dll
http://localhost/App.MyService/MyService.svc?wsdl
/namespace:*,"App.ServiceReferences.MyService"
/out:..ServiceProxyMyService.cs
The PagedList object is something like:
[CollectionDataContract]
public partial class PagedList<T> : List<T>
{
public PagedList() { }
/// <summary>
/// Creates a new instance of the PagedList object and doesn't apply any pagination algorithm.
/// The only calculated property is the TotalPages, everything else needed must be passed to the object.
/// </summary>
/// <param name="source"></param>
/// <param name="pageNumber"></param>
/// <param name="pageSize"></param>
/// <param name="totalRecords"></param>
public PagedList(IEnumerable<T> source, int pageNumber, int pageSize, int totalRecords)
{
if (source == null)
source = new List<T>();
this.AddRange(source);
PagingInfo.PageNumber = pageNumber;
PageSize = pageSize;
TotalRecords = totalRecords;
}
public PagedList(IEnumerable<T> source, PagingInfo paging)
{
this.AddRange(source);
this._pagingInfo = paging;
}
[DataMember]
public int TotalRecords { get; set; }
[DataMember]
public int PageSize { get; set; }
public int TotalPages()
{
if (this.TotalRecords > 0 && PageSize > 0)
return (int)Math.Ceiling((double)TotalRecords / (double)PageSize);
else
return 0;
}
public bool? HasPreviousPage()
{
return (PagingInfo.PageNumber > 1);
}
public bool? HasNextPage() { return (PagingInfo.PageNumber < TotalPages()); }
public bool? IsFirstPage() { return PagingInfo.PageNumber == 1; }
public bool? IsLastPage() { return PagingInfo.PageNumber == TotalPages(); }
PagingInfo _pagingInfo = null;
[DataMember]
public PagingInfo PagingInfo
{
get {
if (_pagingInfo == null)
_pagingInfo = new PagingInfo();
return _pagingInfo;
}
set
{
_pagingInfo = value;
}
}
}

View 1 Replies

Forms Data Controls :: Itemtemplate And Edittemplate - Row In Itemtemplate Will Disappear?

Jul 9, 2010

Now I have a datalist

I have done the itemtemplate and edittemplate,when I click on the button in itemtemplate, the edittemplate will show.But that row in itemtemplate will disappear.How can I keep that row in itemtemplate and ecotent in edittemplate both appear?

View 4 Replies

Create Some Control For Reusing HTML?

Jun 24, 2010

I'm new to asp.net mvc. I'm looking to create some control for reusing HTML. I have a complex HTML box for example:

<div class="Box">
<div class="Top"></div>
<div class="Content">
<div style="padding:10px;">
[CONTENT GOES HERE]
</div>
</div>
<div class="Bottom"></div>
</div>

Previously using webforms I've been able to reuse this by inheriting from WebControl and override Render. But how can I implement this in MVC?

View 2 Replies

.net Mvc - Reusing Pages/controllers In Workflow

May 3, 2010

1) the user signs up for the first time. They see 3 different screens, their basic user information, their credit card, and some additional profile information. They complete these 3 steps in a wizard like fashion, where each time they hit "submit" they leave the current screen and move on to the next.2) the user already is signed up. He has links in the navigation to these 3 seperate pages. He can update them in any order. When he hits save, he doesn't leave the page he's on, it just shows something at the top that says "Credit Card Info saved..." or whatever. Possibly using ajax or maybe a full page refresh.I would like to reuse the code not only the view but also in the controller for these 3 screens between the two workflows, but without a ton of if...then logic to determine where to go next depending on whether its a first signup in the wizard or updating individual parts of a profile.

[code]...

View 2 Replies

Web Forms :: Itemtemplate Dropdownlist Not Binding Based On First Itemtemplate?

Jul 29, 2010

I have two item template which consist two dropdownlist D1,D2...

here what im doing is d2 is binding based d1 selected value...

im using this code in D1_Selectedindexchanged

foreach (GridViewRow row in Gridview1.Rows)

View 21 Replies

C# - Assigning Values To Properties And Reusing The Code?

Sep 28, 2010

I have some code like this. But imagine that there are hundreds of lines like this all duplicated across multiple functions.

MyClass.prop1 = doingsomething("test1");
MyClass.prop2 = doingsomething("test55");

I want to reuse my code... how can I do it?

I want to do something like this:

foreach(ClassInfo)
{
dosomethingAssignment(ClassInfo.propertiesToAssign[i], ClassInfoStringIPassedToFunctionInFirstExample[i]);
}

Maybe this isn't the way to do this in C#, but what is the best way to reuse my code?

View 4 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved