Click here to Skip to main content
15,914,111 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have created a jquery dynamic table for invoice form on which user can add product,price, quantity and all data's in dynamically. I want this jquery dynamic table data/values in code behind file.

How can i make it possible ?

Much appreciation if you could help in this regards.

Thanks
Posted
Updated 10-May-16 0:17am
v2

You'll have to put the data in a hidden variable as well as on the screen in a table. So when you submit the form you could use jQuery to read the data from the table and store it in a hidden field like;

ASP.NET
<form id="form1" runat="server">
    <asp:HiddenField ID="MyData" runat="server" />

    <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click"/>
        
    <script type="text/javascript">
        $(document).ready(function () {
            $("#<%=btnSubmit.ClientID%>").click(function () {
                // I'm hard-coding this, you can keep a track of this data as you add\remove products
                // or you can scan the finished table and build it from there
                var myData = { Products: [{ "ID": 1, "Name": "Product 1" }, { "ID": 1, "Name": "Product 1" }] };
                $("#<%=MyData.ClientID%>").val(JSON.stringify(myData));
                return true;
            });
        });
    </script>
</form>


Code-behind classes

C#
public class PageData
{
    public List<Product> Products;
}

public class Product
{
    public int ID;
    public string Name;
}


Submit event

C#
protected void btnSubmit_Click(object sender, EventArgs e)
{
    string json = MyData.Value;
    // this needs a reference to System.Web.Extensions.dll
    System.Web.Script.Serialization.JavaScriptSerializer s = new System.Web.Script.Serialization.JavaScriptSerializer();
    PageData data = s.Deserialize<PageData>(json);

    foreach(Product p in data.Products)
    {

    }
}
 
Share this answer
 
v2
Try HtmlTable Class.

You have to add runat="server" tag to the HTML table and access that table by Id in Code behind. and then CAST it to HTMLTable.
ASP.NET
<table id="tblTest" runat="server">
   <tr>
       <td></td>
    </tr>
</table>

for example :
C#
HtmlTable table = ((HtmlTable)tblTest);
table.Rows[0].Cells[0].FindControl("ControlId");
 
Share this answer
 
Comments
F-ES Sitecore 10-May-16 6:02am    
That's not going to work for rows\cells created via jQuery or javascript.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900