Solved

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

Posted on 2014-03-04
4
1,554 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 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 33

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
asp.net uploading picture to a temporary folder 4 26
Two decimal 5 30
assigning javascript variable to php variable 8 43
Diff of the day 2 10
This article discusses how to create an extensible mechanism for linked drop downs.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
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…

726 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