?
Solved

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

Posted on 2014-03-04
4
Medium Priority
?
1,734 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 7

Assisted Solution

by:cstsang
cstsang earned 668 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 33

Assisted Solution

by:Big Monty
Big Monty earned 664 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 668 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…
Suggested Courses

770 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