A clean solution for dynamic entry table rows in ASP.Net - asp.net

I'm working on a project in ASP.Net that uses the UpdatePanel to handle all AJAX requests. I'm much more familiar with using jQuery AJAX methods (and strategies) so I'm kind of stumped on finding a clean solution for the functionality I need.
Basically I need a table that allows me to dynamically add as many rows as I want using AJAX. These rows will contain text boxes whose values must be preserved by ASP.Net until I'm ready to submit the form. To further complicate matters there may already be records that exist in the database that should display as well with the values pre-populated and any changes to these fields must also be preserved.
I had started out by using a GridView and setting it's datasource to a list of the database records but unfortunately I believe this means I can't add new rows to the GridView.
Any assistance would be much appreciated. Keep in mind the solution must use ASP.Net controls.

I believe I figured out a solution that seems pretty straightforward.
When the page loads I gather any relevant records from the database and perform a query expression combined with a projection select to create an anonymous type array. When the add new row button is clicked instead of trying to add the row directly or retrieve potentially old data from the database I perform a query expression on the datagrid items themselves and union a new blank item. From there it's just a simple matter of setting the datagrid data source, triggering a databind and updating the update panel.
The only question now is how to identify the rows, particularly new rows that have no database keys yet. I could devise some clever method for generating row ids but I have a feeling that ASP.Net already does this and all I need to do is associate these ids with the anonymous types.

Related

Multiple datasources for same SQL DB

I have an app page that shows information from the same table in 2 places on a page.
In the place A, I want an editable field that is owned by the current user.
In the place B, I want to see the list of entries for all users.
I thought I could somehow have two pointers into the same table by configuring more than one datasource for the table, and bind placeA/B widgets to the two separate datasource.
However, I could not find out how I can bind a panel or widget to a specific datasource within a table.
What bugs me is that clearly the UI allows me to add multiple datasources for a table, so I probably misunderstand what the usercase for multiple datasources are.
My current plan is to ivestigate whether I can create a computed datasource that limits results to just the current user, but I don't understand how I could updated a computed datasource (which is something I want to do).
This is probably less than clear, happy to add clarifying information.
Many thanks in advance for any help
Never mind. App maker behaves as I thought it should be. The two datasources are behaving as two cursors into the same table. For some reason, I could not seem to find the 2nd datasource in the widget dropdown for datasource, but I could find it by selecting "Advanced" and then finding datasource starting from "datasources" instead of "widget.datasource".
Nothing to see here, move along now ;)

Key-Value pairs in ASP.NET - Clientside

I am very new to writing ASP.NET web apps..but i've been tasked with doing so. I have an extensive set of web services i've written that work with our backend databases. Up until now, i've written solely desktop apps using these web services. this is my first venture into web applications.
So far i've been doing quite well. But i've come to a point where i'm trying to minimize some postback delays.
I'm using a gridview to have users enter some data for a one-to-many relationship. One of the fields they select from a dropdown list. After selecting this list, i was doing a postback to get the corresponding key value and then displaying it but this took to long. Ideally, this should be instantaneous, so i was wondering what the best approach would be - clientside/javascript - to do a lookup on a list of name-value pairs?
From some preliminary web searching, it appears the two most popular approaches would be to use either a hidden field to store the lists, or to store it in a JSON object?
I'm looking for opinions on the best approach, and how specifically to store this list on the client side? From what i understand, using a hidden field could be the easiest.
Opinions?
I don't think you need to store it at all on the client side but rather do an async Ajax call to the server to retrieve the actual value. You should be able to accomplish this easily by putting the GridView and the displayed label controls into an UpdatePanel where the Trigger is defined as your dropdown list.

About the GridView control in asp.net

I'm working on a web application, on one page I am inserting records in the database and I want to display the data in a GridView but on a diffrent page. How can I do this?
I know how to display records in a GridView, but I want to know if there are two web pages,
on one page provides the facility to insert the records and U want to display the records in the GridView bit on the second page.
While it is possible to retain the data being inserted without retrieving it from the database, I think it is better to save the data on the first page and retrieve it from the database on the second page.
You can do this by writing inline SQL or a stored procedure. One simple approach would be to pass the resultset into a DataTable and bind a GridView to that.
That does involve more work -- more code and more trips to the database. However, I think it is very useful when performing INSERTs that the web page is updated to display what actually got into the database. Sometimes, this is different from what the user thinks they entered, and they can see the problem immediately.
One question would be how to identify the data that has just been inserted. I can think of several ways to do that. One is to query for all records entered today by the person logged in (which is recorded in the CreatedBy and CreatedDate columns of the database tables). Sort the resultset in descending order of CreatedDate, so that the most recent entries appear at the top of the GridView. Another would be by assigning a batch number to the data entry and retrieving only the data in that batch.
If you really want to hang on to the data entry, you could put it into Session on the first page, and then retrieve it from Session for display on the second page.
Following along the lines of what DOK said, it's also a lot easier to validate data entered by your users in your business logic before you submit it to the database.
Secondly, users can change their minds about data on a webpage frequently. The data on the web could be in an partially-finished state or could have typos or errors in it. If someone else saw this data and believed that it needed to be completed, then you could end up with duplicated entries in the database that would then require reconciliation.
Honestly, your best bet is to use the Session object to hold temporary user data. The MSDN entry for the GridView RowEditing event contains some great source code for this approach. Whenever I have to use GridViews to handle data from the database, I mimic this.
In addition to handling problems with temporary data storage, you can compare the Session object to your database results to determine whether or not new rows have been inserted. This is somewhat costly as it involves overloading the Equals method (and GetHashCode as well, if you follow what Microsoft recommends) and using Equals to iterate over the two collections, comparing the properties of both objects, and determining which records are new based on records that don't exist in your Session object, but do exist in your database object.
It's also worth noting that this approach assumes that you don't delete data from your database, but set the status of a record in your database to "Deleted" -- if that's a boolean field or an sequence of codes you use to describe the state of rows in a table.

Manipulating data in sql / asp.net / c# - how?

Not sure how to word the question...
Basically, so far all my SQL stuff has been stored procedures and dumped into a gridview. The odd case where I had to perform an action based on a value (such as highlighting a row green if a certain value was true) were done as the gridview was rendering in one of the overrides.
Now however I have to do something far more complicated - pull three sets of data down, run a series of checks on all three and some date related checks and stuff, then populate a gridview with some of the items.
In logic terms, I want to run three queries, and store the lists of results (presumably in Lists?) then run some logic, then populate the gridview.
Specifically what I don't know how to do is:
Best way of pulling the data, and putting it into a List or other datastructure that lets me easily run through it, and retrieve data based on column (myList.age, or more likely, myList["Age"]).
One I have compared the data, I assume I create a new list that will be put into the gridview... how do I put the contents of a list INTO a gridview? How would I add other stuff such as buttons or checkboxes at the same time?
Any nudge in the right direction would be appreciated! Particularly doing cool stuff with lists and sql (if there is anything cool you can do with them)
There are actually several ways to do this with .Net without using an ORM solution. Instead trying to list all of them here I will link you to an article series that should help you accomplish what you want. The following series of articles is about N-Layer design, and includes answers to the information you are looking for. The first set of 4 articles were created some time ago with ASP.Net 2.0 then the author updated them with an additional six articles using ASP.Net 3.5
http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc=476
If you want to avoid having to write SQL query and concentrate on the data instead, go with the Entity Framework. It's quite well integrated with VS. It will connect to your SQL database, you will tell it which tables to import, and it will create a series of classes for you to talk to your database.
Queries are quite easy to do, and they will return lists of objects which should be easy to manipulate and put to a grid later on.
You can create datatables and populate them using the lists that you have generated and then bind your grid using the newly created tables as the datasource.

MS Reportviewer - Save Expanded/Collapsed Nodes in ViewState?

I have a report that's being populated from an ObjectDataSource. The report includes expandable/collapsible nodes per user.
I'm allowing the viewer to filter the report by setting the FilterExpression on the datasource, then calling ReportViewer1.LocalReport.Refresh(). This is done during PostBack.
This filters the results, but the state of the expanded/collapsed nodes is lost. Is there some way to store the expanded/collapsed nodes in the ViewState? Or is there a better way to accomplish this altogether?
I know its been a while since this question was asked, but since i had to go through this recently i want to share my experience with somebody who might be dealing with this.
It's tricky but if you add a multivalued parameter to your report and the values match the values in the toggable fields, then you can write a group expression in the grid (And in the labels so those go away too). Once that is done, you just have to keep track of what is toggled (this is hard, but is possible with javascript).
Basically, there are two of the hidden child controls in the reportviewer control (indexes 4 & 5 in vs2008) that have the name of the event and the id of the table cell clicked, with this you cand build an array in javascript and keep track of the status of all toggable fields.

Resources