Fetch multiple values from same form - c#

I have a form in which I am gathering information from the user. Based on the value of one of the dropdowns, I am showing a panel to the user. This panel contains textboxes, dropdowns etc.
Suppose the user fills in all the values, I want to show him an add more button. On clicking this button the value in the panel should be saved somewhere and the panel should go blank for adding more values. This way a user could enter values a number of times before the final submission. On final submission all these values would be saved to a table. Could somebody guide me with the methodology of how this could be achieved??
Also I want to know how this kind of thing can be achieved at the frontend?

You can either make use of Lists for this purpose or simple arrays.
You have not mentioned your fields clearly , but suppose they are, username, address, telephonenumber.
Then you can make list or dynamic arrays for each of these fields and on final submission, you can do Bulk Transfer to the database.
Link For Lists:
http://www.dotnetperls.com/list
Link For Bulk Insert:
http://blogs.msdn.com/b/nikhilsi/archive/2008/06/11/bulk-insert-into-sql-from-c-app.aspx

One way to do it, would be to use KnockoutJS where all adding/removing and UI manipulations are done by the library. Then submit JavaScript object as JSON to the server, serialize into objects and insert/update the database.

Related

Best way of tracking changes code behind

I have an Asp.NET form. On page load, I am populating text boxes and drop down lists.
When the user clicks on the edit button, I want to know if the user changed the values or selected a new value but then re-selected the old value and didn't actually change anything.
I can think of two methods:
Create hidden labels for each input and then track in code behind.
Use session state or re-query the source where I uploaded the data from in the first place.
Do you know any better way?
you can use a stringbuilder
At page load you can put all values in stringbuilder now create a another stringbuilder in that put changes values or values after button editing of records completed
After that you can compare two string
or you can store those strings in database for future reference

How to dynamically add a dataentry form in asp.net webforms

I have to create a data entry form in asp.net webforms where the user could enter several member details. Each member detail will have about twenty data items such as FirstName, Lastname, DateOfBirth etc. The number of members entered can be anywhere from one to say twenty.
Once all the members entered or during the entry process, they should be able to go to the previous or next entry and make changes. Essentially there should be a Next and Previous buttons which will traverse the pages. At the end when the Save button is clicked all the entered data should be saved.
I have created a UserControl, which has all the textboxes to enter data. I have created five hardcoded panels which display the usercontrol five times. This makes the total members that could entered as five.
I would like to replace this hardcoded model with a dynamically added panel, when the next button is clicked.
Please let me know how this could be achieved. Or if there is a better architecture please let me know. I tried the wizard control, but it was too cumbersome in maintaining the previously entered values and traversing data using Next and Previous buttons.
Thanks
You could add the controls dynamically to the page using:
Page.Controls.Add(new MyControl());
You'll have to find a temporary store for the data entered in the controls though such as Session.
Session["addedControls"] = myControls;
You can track the number of controls they add and then loop though and save all the data to whatever permanent store you're using.
There's an example of this here: http://howto.jamescarnley.com/2008/05/adding-fields-dynamically-in-aspnet.html
Storing the entire control in session does have it's drawbacks such as the overhead involved, you could just store the data which is more efficient but is a tiny bit more effort to implement.
This will also only work if they have cookies enabled.
Alternatively you could have a drop down list for them to select the number of entries they wish to make and display that number of controls by calling Page.Controls.Add the number of required times. Then just save them all in bulk at the end.
You could dynamically add the controls you need to input a new member easily enough in javascript. What you propose sounds like a nightmare to me. I'd put up a page with the controls to add a member and an 'Add Member' button. When the Add Member button is clicked - I would save the member to the database, retrieve a list of members added in this session - and display the names of the members in a list to the right of the 'Add Member' area of the page. If they want to review a member they can click on their name and you can show that member ready to be edited. Or they can add another member until they have finished. What you are trying to do is going to be a nightmare to manage and won't be any easier for the user to use.
Using dynamic controls will be very difficult and not user friendly as it may require refresh the page or using update panel
in your case, I would recommend doing it all from JavaScript using Knockout, This is a full sample Knockout Grid
Make sure after adding or editing the grid, you will serialize the JavaScript objects into string using JSON.Stringify and add this text to a hidden field.
On form submission, just get the hidden field value, use newtonsoft json library to convert the string into an object and fee free to use the object which will be a list of your model (ex: Order).
Compared to dynamic controllers, this is more manageable and user friendly

Call code behind c# function from javascript

I know this has been asked, but none of the numerous answers fit with my situation. So I will humbly ask if someone can walk me through this again gently.
Environment: .Net 4 web app using c#, javascript, jquery, sql server, and entity framework.
Question: I need to be able to allow a user to "quick add" a value into a table that is bound to a drop down box without losing values a user has already entered onto current page. I don't care if I have to do a postback or use jquery or use ajax, etc. I need the functionality first - prettiness later ... the scenario is described below if you care to keep reading. With all that being said, I would love for this to be smooth and not "clunky" for the user.
Scenario:
I have a page where the user is able to enter many pieces of information into textboxes, static drop down boxes, and drop down boxes that are bound to other tables that will all get combined and inserted into one table. Well, it never fails, a user could be filling in the page and when they get to one of the table bound drop down boxes - a value is missing or they haven't added something item yet. I would like to add a button or link or something next to that drop down box that would allow the user to "quick add" an item to the table that fills that drop down box and then refresh the drop down box so they can choose that new value from the list ... all while not losing the other data on the screen they already entered.
What I've tried:
-Tried WebMethod option. Currently, this web page calls some custom "bind" methods in the code behind to fill drop down boxes so they are not filled directly from objects from entity framework. So that means using a static method will not work for a solution because part of the static function would need to call the bind method to refresh the drop down box with the newly inserted values.
-Tried PageMethod option. Some of the other samples used Page Method settings, but my site uses master and content pages and the posts seem to state that the page method route was not going to work.
Make sense?
Add an UpdatePanel to the page. The update panel should wrap the drop down to be added, as well as the textbox/button for adding a new entry to that drop down. You would want to have a different UpdatePanel for each dropdown/button pair, if applicable.
When they click the button inside of the update panel you can read the textbox, add an extra item to the drop down, and send an update to the database. (I'd manually add the new value to the drop down rather than updating the database and re-binding, if possible, but it may not be.)
The magic of UpdatePanels will make sure that this is all asynchronous and so doesn't disturb the user working on the page.

Is this a good approach for avoiding postbacks?

I'm creating an e-commerce site in ASP.NET.
For the actual products available to buy, I've placed a box with two arrows, which allow the user to modify the quantity of a specific item. This feature has been implemented in Javascript, so as to avoid postbacks which would be the result of managing this through ASP.NET/C# code.
Now the problem is, how am I to store the items which are ordered? Since the user can modify the quantities and products selected on the fly, I need to figure out how to pass the selected items onto another aspx page (for checkout).
Due to strict requirements, I cannot use a shopping cart. Just literally allow the user to play around with quantities of items, then once the 'Checkout' button is pressed, the user is directed to a payment page where his items are displayed.
Since the selection of items is carried out in javascript, I need to find a way of passing the selected items to another .aspx page.
I thought of storing the items in a javascript data structure, sort of like a table. A product ID will be stored in one field, and the quantity of te respective product will be stored in another field. Once the checkout button is pressed, this data structure is passed on to the .aspx page and handled from there in C# code.
It seems a bit coplicated, especiially since the javascript structure may be modified multiple times if the user changes the selected products and quantities before pressing 'checkout'.
Any ideas?
You can use JSON data in your javascript code and then pass it to ASP page with GET or POST.
At the server side you can parse JSON to .net object using JavaScriptSerializer or DataContractJsonSerializer.
Updated: In JavaScript JSON data should be looked this way:
var orders = [
{
"ProductID" : 123,
"SomeData": "some additional data if needed",
"OrderedCount": 3
},
{
"ProductID" : 321,
"SomeData": "some new additional data if needed",
"OrderedCount": 1
}
];
It is an array of orders.
Sounds like it should be pretty easy. On the page with the quantity boxes and checkboxes, you just run an iterator over them, finding first products to be ordered (assuming the page is not already displaying only products to be ordered) and then within that set finding out the quantities. You can then serialize these or otherwise create a data object and pass these to your ASP page with GET or POST (whatever you prefer).
In other words, sort of like the idea you already had, but I don't see it as being overly complicated to create or manage. Most of your functions can be reusable and repeatable.
As per Jared Farresh's comment, cookies make good sense, too.
From the comments under your question it appears like your apprehension to a "shopping cart" is related to the overhead that would likely go along with a server-side shopping cart.
But, have you considered using a client-side shopping cart? You can still use the shopping cart metaphor to reduce the complexity of the "javascript data structure, sort of like a table", without having a persistent back-end implementation.
simpleCart js is an open source, light-weight implementation of this concept. It works by storing the data in a cookie. You can retrieve it directly by querying the object or you can specify a class on an HTML element and the start up script will automatically replace the inner HTML of that element with the requested value. For example, a <span class="simpleCart_quantity"></span> will immediately have the quantity of items in the cart as its inner HTML after the page loads.
You might also want to check out this post that I asked when I was working on a project with similar requirements: Open Source client-side Shopping Cart - jQuery/Cookies

Storing a collection into a list and session - Front End

I have this program in which I am trying to store a collection of values into a list and session
Example:
valueCollection = (List<Values>)Session["Value"];
I want to do the front end in a way which will show a table with each row showing a label and textbox. This would be too simple to do obviously but I want it to show 4 rows of the table by default and then the user can select "add another" this will then add another row onto the table with a label and textbox exactly similar to the 4 default. Everytime the user selects "add another" the table increments by 1.
How do you do something like that, or is there an easy way?
Here is a screenshot to explain it better:
http://img828.imageshack.us/img828/9986/idead.png
The basic idea is that you bind a control to your data and provide GUI controls to allow users to add/edit/delete records. Once the records are modified you re-bind the control to see the changes.
Take a look at the Databound controls in the .Net Framework. For a quick first pass you could use a ListView - a control which provides built-in functionality to automatically generate and mange the add/edit/delete GUI elements.
In 'real world' usage, storing big datasets isn't a great idea as more users = more sessions and each session uses server resources. You should really be getting/storing your data in a database or perhaps an XML file.
HTH

Categories