• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1903
  • Last Modified:

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

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
peternordberg
Asked:
peternordberg
3 Solutions
 
cstsangCommented:
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
 
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
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
 
OriNetworksCommented:
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
 
peternordbergAuthor Commented:
All Solutions have me an idea on how to do it.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now