Solved

Creating rows dynamically on client side and saving them to database on server side

Posted on 2014-03-04
4
1,409 Views
Last Modified: 2014-03-18
Hi,

I have a asp.net site and my question is if it's possible to create rows dynamically like in a table on the client side with javascript or jquery and then loop through that set of rows on server side and save the entered content to the database.

For example I have a row with a dropdown and a textbox, and I want the user to dynamically create rows on the client side, enter content to the dropdown and the textbox for each row and then when they hit save iterate through this table and save each row to the database.

How can I achieve this?

Peter
0
Comment
Question by:peternordberg
4 Comments
 
LVL 7

Assisted Solution

by:cstsang
cstsang earned 167 total points
ID: 39903615
You can create the following form:
<form action=[i]your server side script[/i]>
    <table id=dataEntry>
        <tr>
         <td><select name=select1><option value=1>One</option></select></td>
         <td><input type=text1></td>
         <td><input type=button value="Add row" onclick="addRow()"></td>
         </tr>
</table>
<input type=hidden name=count>
</form>

Open in new window


For the function "addRow()", you may refere the following web page:

http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Jquery/Q_28373610.html#a39885044

Don't forget to handle the form field named count, you need this data to indicate the no. of rows that user have been added.

Good Luck
0
 
LVL 32

Assisted Solution

by:Big Monty
Big Monty earned 166 total points
ID: 39903749
why build the logic from scratch when there are plenty of objets already created, both client side and server side?

you could use .NET's built-in GridView control, which is server side, or you could use something like jqGrid or FlexiGrid for client side functionality (they both will allow you to load/save data from the server as well.

I disagree @cstsang's suggestion of manually building out the logic of these types or tables/grids, especially with so many controls out there that have already been written and tested.
0
 
LVL 17

Accepted Solution

by:
OriNetworks earned 167 total points
ID: 39912507
I agree with using an existing component if that's a possible option. I also found myself in a situation where I had to recreate this from scratch so here's what i did.

I have a regular html table that the user builds using fields on the page and javascript/jQuery. When the user adds rows to this table then clicks the save button, it converts the table into a javascript object array and builds a json string for a hidden asp:TextBox control. On the server end I grab the value of that textbox and deserialize it using the deserialize method of System.Web.Script.Serialization.JavaScriptSerializer which populates a list of a custom object . VB.Net Example:  
Public Class Person
Public FirstName As String = ""
Public LastName As String = ""
End Class

Open in new window


Then I can deserialize the json using
Dim jss As New JavaScriptSerializer()
Dim ListOfPeople As List(Of Person)
ListOfPeople = jss.Deserialize(Of List(Of Person))(txtJsonFromForm.Text)

Open in new window


Using this new ListOfPeople object I can iterate over the list to add to the database as needed. In this case of course the property names FirstName and LastName match the json that I am building on the client side of the page.
0
 

Author Closing Comment

by:peternordberg
ID: 39937639
All Solutions have me an idea on how to do it.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now