Creating Dynamic Tables in VB.Net - asp.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.

Related

asp.net + Binding DataSet to already bounded Listview

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.

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

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.

Gridview binding without using a Database

Can a GridView be bound to a List<>?
I want to create a Master Details scenario. So The first List<> contains Master data and the second Child data. When the user clicks on the Master Data, the child data, should appear in a pop.
Is that possible? Please share some code.
Yes, this is possible. Take a look at the ObjectDataSource to use for databinding a list to a GridView. It requires some more manual coding, but it works.
There's a good discussion of one possible solution here that manually constructs a DataTable from your business object list to make filtering/sorting/paging easier than manually implementing those methods.
You can create on temporary table and then bind that table with grid.
Like,
dataset.DataSource=tempTable;
tempTable.DataBind();
GridView1.DataSource = theList;<br/>
GridView1.DataBind();

Telerik GridViewDataControl - load from datatable

does anyone uses this control and knows how to load data to it from simple datatable. There is not such a trick as DataSource...
Im assuming you just mean a datatable you have created programatically?
It will bind straight up like it would from a DataSource.
Just make sure your DataField name in the columns mark up matches the name of the column and your good to go, just as you would with a different datasource.
Then just do
grvFoo.DataSource = yourProgrammticallyCreatedTable;
grvFoo.DataBind();
But, since your doing this progrmatically I would recommend making a "BindTable" function. Eg a function that builds your datatable and bind. You can then use this for sorting / paging etc. As your doing this all programatically you must handle such events by hand, they are normally done with the SQL / Object Datasource etc.
you mean Telerik Radgrid ? If yes, you can also specify a "NeedDataSource" event in the aspx/ascx
http://www.telerik.com/help/aspnet-ajax/grdadvanceddatabinding.html
On a side note, just to let you know, we are having a lot of performance problems with Telerik. For the most part, you can use the inbuilt asp.net gridview (and do some more styling if needed) than taking a performance hit by using telerik (for their fancy grid)
We must use ItemsSource property like this:
dataGridView1.ItemsSource = LoadDataTable.DefaultView;
LoadDataTable - this is some DataTable, filled from SQL.

Resources