Very simple scenario: I have a Repeater with a table spanning its Header, Item, and Footer Templates. Because it spans templates, I cannot simply do <table id="Blah" runat="server"> and then access it via FindControl.
Is there any way to overcome this?
EDIT: The goal is to be able to retrieve the HtmlTable then pass it into another method which parses over it, converting it to excel xml.
In short, no. By turning it into a server control (with runat=server) you can't make it span templates. Why do you need to get to the table element?
I was approaching the problem incorrectly. I should have been trying to pass the Repeater, not the HtmlTable. After changing methods, I was able to accomplish my goal and can now export the Repeater contents as XML.
Related
I am developing a website using mainly AJAX for saving and retrieving data in order to avoid postbacks (or at least full postbacks).
I "prepare" the page in codebehind, and on client I use JQ and Javascript.
When I want to save data I serialize the elements in a container (using JQ) and then AJAX post to send data to the WebMethod.
This is working well but I have to deal with long name elements ("ctl00$MainContent$grdEmployees$ctl03$ddlRole").
Sometimes is difficult to retrive data from the NameValueColletion as the same WebMethod can be called from different pages, so the "name" of the element is not the same as the control may be nested in other container.
Is there a way to set custom names for ASP.NET controls to avoid this issue?
Or other way to achieve this behavior?
Best regards.
Have you tried to use ClientIDMode="Static" which is found in .NET 4.5.
When a page is render the ids all change as you have noted. However, you set the ClientIDMode to static it will render with the ID that you have put for the control itself. That way you can work with the ID of the control instead of the generated id. If you would like to know more here is a like to the MSDNA about ClientIDMode
http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientidmode(v=vs.110).aspx
I have a database where I want to display 2 records in a nicely formatted ASP.NET with HTML
Each record would look like this on the web page
The layout would be layers. The data is in a SQL Server 2008 r2 database.
Now whats the best way to get data to populate each record.
Repeater? Or another method?
Regards
Tea
The repeater control is an easy control to use to make sure that your data is displayed the same for all entries in a given data source.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.aspx
This looks like a template and will follow the same html for each entries so it would be better to go for repeater as you will have more control over the html,css and the placemnet part.
yes repeater is best option to use in this situation or you can generate the whole html on you code behind page and assign it to a literal control.
I have a scenario where we need to show data in tabular form.
there will be 10 different data tables in form so which approach will be good to use:
1. Create html table at runtime based on data.
2. Use asp.net's repeater control.
Main criteria is performance, page should not take more time to load.
A repeater generates HTML. Why reinvent the wheel?
TheGeek's answer is valid. The custom html code you are about to write will not be very different from the code the repeater control is writing.
If performance is your main concern, you should consider using lazy loading.
I have a database table with some Id, parentId and other data-related fields.
I want to display all this in nested <ul>s (n-levels of nesting) using repeater control.
(I don't want to use any other 3-rd party controls and also no treeviews...)
First, i read the data (once) from database into a collection (List<myObject>) on which I would like to manipulate to get the hierarchical structure.
The problem is how to build repeater's template?
Repeater's template is static, so the only thing that i can prepare is the starting and ending <ul> and </ul>.
How to be with nested uls? how to create them?
Please suggest.
You could nest more repeater controls into the item template, and bind them to a subset of your data.
Alternately, I would suggest programatically building your item template.
You're not going to be able to do this with a standard repeater. You could nest repeaters, but that'd take some programatic tweaking to build your hierarchy. An alternative would be to use the TreeView control. If you don't like the built-in TreeView and you want cleaner markup, Asp.Net MVC sounds like it'd be a better fit. If you want a more middle-ground solution, Asp.Net's CSS Friendly Control Adapters may work for better you.
I have a Repeater control that I bind server-side. It repeats a series of divs, and does so with no problem. I have some buttons that I use to sort the repeater (newest, highest ranked, random) and this works the way it should.
I would like to improve my user experience by making the buttons sort the divs using Ajax/jQuery somehow so that there is no page postback and the user does not lose his/her spot on the page.
Is there a way to use jQuery to access server-side code like this, or use Ajax to re-bind a server-side control?
Thanks... if I need to list more details, please let me know!
EDIT I'm aware of UpdatePanels, but I would prefer not to use them if I don't have to.
Have you considered moving the Repeater's functionality to the client-side?
Doing it that way, functionality like paging and sorting is not very difficult to add. In fact, you can lean on the framework pretty heavily by using ADO.NET data services as the service layer.
It's relatively easy.
Move your repeater to a separate custom control, let's say MyControl. Now repeater in your page becomes uc1:MyControl.
Wrap MyControl into a div:
<div id="mydiv">
<uc1:MyControl ID="MyControl1" runat="server" />
</div>
Create a new page, pgMyControl.aspx, that contains MyControl only.
On your main page, add jQuery handlers to your sort links. Use load method to dynamically replace div contents:
$('#link_sort_random').click(function()
{
$("#mydiv").load("pgMyControl.aspx&sort=random");
}
Use QueryStringParameter in datasource inside MyControl to change order. Or use Request.QueryString in code-behind file.
Using an updatePanel or a jquery Ajax postback are the same thing essentially. Both will ask your code to fetch the new query, then make your control render itself, and then feed the HTML back to the client as a partial page render, and then insert the content in place of the old content in the same DOM location.
It is considerably harder to make JQuery and ASP.NET talk to each other this way due to the nature of web controls and their lifecycle that determines when they render. An updatePanel knows how to call all this, maintain proper viewstate and return the result to the correct location.
In this case, don't make things any harder on yourself, use the updatePanel unless you have some very specific reason not to.
EDIT: If you're having JQuery issues with update panels it is probably due to the fact that new DOM nodes being created. JQuery has the live event to handle this. It will notice when new DOM elements are created and match them against your selector even after the document ready.
Maybe it's an OT, but you can consider to change the way you bind even the client and the server control, using XSLT transformation instead od the classics server controls.
You can find an example here (sorry, it's in italian...).