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?
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
"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:
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.
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
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.
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.
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
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 ?
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?
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?
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:
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).
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:
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:
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.
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?
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.
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.
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.
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; } } }
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?
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.