asp.net + Binding DataSet to already bounded Listview - asp.net

Basically what I want to achieve here is to show more items in batches just like the YouTube comments session in every vid, about 10 comments shows up for the start with a "Show More" button below which loads additional comments in batches of 10 when clicked upon. Anyone has a clue of how to go about this?
Not sure if this is right but what I intend to do now is to bind additional records to my Listview again with DataSets without extracting the data that has already been bonded.
Currently I am binding my first set of records with a Dataset filled with data from my SQL Server like this:
DataSet ds = activityBll.GetActivityAttendees(activityId);
DataTable dt = ds.Tables[0];
uilvEventParticipants.DataSource = dt;
uilvEventParticipants.DataBind();

The simplest way to do this would be to do as you suggest, and just keep getting more records from farther and farther in the past. You'll experience increasing delays as you rebind, and if you allow users to make changes to comments you'll have to do a huge postback to process those. If you go this way you should look at UpdatePanels.
The most elegant would involve jQuery or a similar Ajax technology to fetch additional records and add them to the bottom of an HTML table (not necessarily a ListView) without refreshing the page. There are quite a few questions on this board about doing that.

Related

Do I need to re-retrieve data when paging using asp:GridView

I have a .aspx search screen that displays the results of the search in an asp:GridView component. There can be up to approx 1000 records returned by the search. I want to implememt paging on the grid so that only 15 records are displayed at any one time and the user can page through the results.
I am retrieving the records by passing search parameters to a WCF service which returns a List of particular entity objects. I create a Datatable and insert one DataRow per entity object from the List. I then bind the grid view to the Datatable.
This is how my grid is defined in the .aspx page:
<asp:GridView ID="gridCat" runat="server" AutoGenerateColumns="False" DataKeyNames="CatalogueID"
HeaderStyle-CssClass="fieldHeading" RowStyle-CssClass="fieldContent"
AlternatingRowStyle-CssClass="alternateFieldContent" Width="100%"
AllowPaging="True" AllowSorting="True" AutoGenerateDeleteButton="True"
PageSize="15">
and I also have this method in the code behind (.aspx.vb file):
Sub GridPagingAction(ByVal sender As Object, ByVal e As GridViewPageEventArgs) Handles gridCat.PageIndexChanging
gridCat.PageIndex = e.NewPageIndex
gridCat.DataBind()
gridCat.Visible = True
End Sub
My problem is this: the first page is rendered correctly i.e. the first 15 records are displayed correctly. However, when I navigate to page 2 in the grid, the GridPagingAction method is hit on the server but nothing is displayed in the grid - it is just blank.
I think the reason this is happening is because the Datatable no longer exists on the server when the request for the second page hits the server - is that right? And the asp:GridView, when it is rendering the first page of results, only takes the first 15 records from the Datatble and sends them back to the browser. So when the request for the second page comes in the other records (i.e. records 16 - 1000) don't exist anywhere - is all that correct?
If so, what's the best solution - I can't see how to implement paging without having to do one of the following:
re-perform the search each time the user uses the paging option;
saving the search results on the Session after the first Search retrieving them each time the user uses the paging option;
manually inserting the Search results into ViewState and retrieving them each time the user uses the paging option.
Is there a better way to do this (or am I doing it wrong)? If not, which of the 3 options do you think is the best? I'm leaning towards option 2 as I don't think option 1 is performant and I don't want to be sending loads of unnecessary data back to the browser as per option 3.
All you said is correct. You could either use ViewState or the Session to keep hold of the data on client- or server-side, but if you really have that many records, it might be a good idea to only collect the data you actually need.
So if you want to show records 1 to 10, you perform a query against the database and only fetch those ten records. If you want to show the next ten, you perform another query with the according parameters.
This will improve your performance and memory usage significantly, IF calling your DB is not overly expensive.
This article might give you a start on how to do this:
http://dotnetslackers.com/articles/gridview/Optimized-Paging-and-Sorting-in-ASP-NET-GridView.aspx
If you want an easy solution without any additional efforts, I would query all the records on each postback (your option #1).
If you want the best performing solution with not much overhead, use the custom paging.

Creating Dynamic Tables in VB.Net

I am new in .Net and I had a question regarding creating dynamic tables.
I am creating a page that adds a new row to a table (First Name, Last Name, Address, etc...) when a user clicks on a button. I have been reading that every time you do a postback to a dynamic table you have to re-create the rows. That is what I am doing.
I have been testing this and about 40 rows have being added already, when I click to add a new row, it runs completely slower and I can only imagine how long it would take to add 100 rows. I am assuming that it is because re-creating the rows takes time.
My questions is there a better approach or another way to accomplish task?
'***Edits Here what im currently doing
This is my table and button control:
Code when button is clicked, which creates the dynamic table:
Dim tblrow As TableRow
Dim tblcell As TableCell
Dim inputText As TextBox
tblrow = New TableHeaderRow
tblcell = New TableHeaderCell
tblcell.Text = tableCount 'variable used to count rows in the table
tblcell.HorizontalAlign = HorizontalAlign.Left
tblrow.Cells.Add(tblcell)
tblcell = New TableCell
inputText = New TextBox
inputText.ID = "txt_" & tableCount
tblcell.Controls.Add(inputText)
tblrow.Cells.Add(tblcell)
table1.rows.add(tblrow)
Now from what I learned and tested so far, everytime I do a postback I have to rebuild this table in order to keep all of the contents I entered into the table.
The next question is why dont you add another row using jquery so that you don't have to do postbacks. I have tried this approach and it worked well UNTIL I needed to put the information entered into the table into a database which required a postback. So I was back at my original problem.
Note, if there is a better way to approach this im all ears. Like I said before I am new to this language and im just trying to learn.
My suggestion for you will be to use the JqGrid it is a free open source jquery plugin but also available commercially. It is fast in data loading and have lots of dynamic features
If you know javascript and jquery then this will be easy for you to use, it comes as Asp.Net, mvc and php component
Are you using windows forms? If you are, use the ListView control and change the view (I believe thats what it's called...) to details. Then you can use one of the many tutorials like this to populate the list:
If not using Windows Forums sorry, I saw the VB.net part and assumed.
asp.net is a server side web technology which means that you are working with a stateless technology (basically). That is why you have to rebuild everything on every postback.
There are several ways of dealing with this:
using the asp.net ViewState and check for Page.IsPostback on PageLoad
using jQuery and clientside Templates like Pure and fetch the Data through webservices
I on your behalf would avoid creating a table manually like you showed above, but instead use templated controls and specify the behavior declaratively.
When you create a DataGrid for example and are using ViewState, you do not have to explicitly recreate the DataGrid, since asp.net is taking care of it when used correctly.

Trying to populate ASP GridView as it's data source gets populated on the server

Hi I am new to using ASP.NET and GridView and it would be great if someone could kindly help me out here.
My scenario is that on a button click event, my code tries to get data from a webservice in chunks for a long period of time. Now I create a DataTable and populate it as each chunk is retrieved. My question is how do I show in gridview whatever I have retrieved so far before the button click returns ? Say, if I have three chunks being retrieved one after another then as of now the gridview gets populated with all the data from the 3 chunks at once, I want to be able to show the first chunk as it is downloaded, then add the second chunk and finally the third. Not sure how to do this.
I'd be happy if you just give me an overall solution/point me in the asp feature I should be using to get this done. Thanks.
If you are binding using webservice. Then asp:GridView will create unnecessary hassles for you as its designed for simple server side bindings.
Why dont you try DataTables or jqGrid, as they are much better alternatives?
You can Merge the existing DataTable every time you receive a new chunk from the Web service and then bind the updated (Merged) DataTable to the gridview.
Check out this article from MSDN and get a better idea of how you can merge DataTables
DataSet.Merge Method (DataTable)
Also look at this article that may help you in your current problem.
Merging two Datasets into a single Datagrid

Two tables on one page Asp.Net

My project is in Asp.Net Webforms using C#.
On one of my web pages, I want to have two tables. One is going to be a summary table and the other a more detailed breakdown of the summary. When the user clicks on one of the rows of the summary table, I want the corresponding breakdown rows to appear in the breakdown table.
These tables can have anything between 60 and about 500 rows.
I have thought of two possible ways of doing this and I'm not sure which is best, or if there is another, better way of implementing this:
1) When a user clicks on one of the rows in the first table, a postback event happens, and the data for the second table is loaded from the code behind.
2) When the webpage is loaded for the first time, both tables are filled with all of the possible data, but all of the rows in the second table have the "display: none;" CSS attribute. When a user clicks on a summary row, the CSS of the corresponding breakdown rows is changed using JavaScript.
I'd go with your first thought and perhaps either
load the data for the second table through an AJAX request returning only row data in JSON (or maybe even the HTML for the table)
use an UpdatePanel
if the user only needs to view the data, I would go for the former, but if they need to edit the data in some way, I would probably choose the latter (even though I'm aware that UpdatePanels are effectively a full postback in async clothing, I feel their use can make the UI more fluid).
You could also make an AJAX call when the user clicks on a row in the summary table to return the data for the 2nd table.
second way no way, loading all of data and then hiding showing is not a good idea, it will be slow and if you add more functionality it will only get in your way...
I recommend this two approaches:
1)first table with pagination
click on row triggers ajax call to page method
page method return second table
here, you can see how to that:
http://encosia.com/2008/02/05/boost-aspnet-performance-with-deferred-content-loading/
2)using of uframe
http://labs.omaralzabir.com/UFrame2005/
cheers

What is the best approach for this CRUD search page in ASP.NET

I'm building a heavily CRUD based ASP.NET website and I've got to the phase of the project where I'm trying to build a search webpage that shows the user different subsets of a certain important table based on parameters they enter such as dates and different text fields.
I can't use a fixed SQL statement, because depending on whether they search by date or by something else, the SQL would be different, so instead I have been generating a results list using a table web control that starts out invisible and then is filled and set to visible once the user identifies a search they want to make. I used a table control because its easy to create and modify in the code behind, and I was able to make the needed calls to the database and populate that table as required.
However, the second thing I need with this search page, is the ability to allow the user to select a record from the results and then go edit it using the rest of the CRUD based pages on the site. To do that, I created asp:buttons in the table and gave them all different ids and the same event handler. But since I created the buttons dynamically, it seems the event disappears on callback and my scheme fails, so I'm taking a second look at how to do this.
My current code for the events looks like:
tempcell = new TableCell();
Button tempbutton = new Button();
tempbutton.Text = "Go";
tempbutton.ID = "gobutton" + rowN;
tempbutton.Visible = true;
tempbutton.Click += new EventHandler(tempbutton_Click);
tempcell.Controls.Add(tempbutton);
temprow.Cells.Add(tempcell);
My results table is declared like this:
<asp:Table ID="ResultTable" visible="false" runat="server"
GridLines="Both" CellSpacing="8">
</asp:Table>
I'd like to be able to identify which record the user selected, so I can send that info on to another page. And of course, I still need to be able to generate results for several different search criteria. Currently I have date, template and code based searches that lead to different stored procedures and different parameters based on what the user has entered.
Any suggestions on this? It seems like the data grids and repeaters require SQL statements in advance to work properly, so I'm not sure how to use them, but if I could then the item command approach would work with the buttons.
Your event will hook up successfully if the button is recreated in the page load event.
Otherwise, use a GridView. It is the perfect solution for simple CRUD, and you can control it as much as you need.

Resources