Manoj Kumar
Manoj Kumar

Reputation: 901

get the values from checkboxes those were created dynamically C# JQuery Asp.net

I have these checkboxes those are created dynamically using C#. These are HTMLInputCheckboxes. They are kept under a panel called "panel_seat". enter image description here

I want to get the values from these checkboxes to post them in database. How to do this? Either creating them as checkbox list or group?

If so please give me some code references please.

Upvotes: 0

Views: 539

Answers (1)

Manoj Kumar
Manoj Kumar

Reputation: 901

Jquery code:

 $(document).ready(function () {
        $("#btn_check").click(function () {
            var str = "";
            x = $("#frm").serializeArray();
             str = JSON.stringify(x);

            $('#<%=hdnSelectedTicket.ClientID %>').val(str);


        });
    });

Asp.net:

 <asp:HiddenField ID="hdnSelectedTicket"  runat="server" />

C# code: Using special dll for Json

 using System.Web.Script.Serialization;
 using Newtonsoft.Json;
 namespace TestApp.components.seatfromdb
{
public class Test
{
    public string name { get; set; }
    public string value { get; set; }

}

public partial class seatfromdb : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {     

 }

    protected void btn_submit_Click(object sender, EventArgs e)
    {

        List<Test> myDeserializedObjList = (List<Test>)Newtonsoft.Json.JsonConvert.DeserializeObject(hdnSelectedTicket.Value, typeof(List<Test>));
        int count = myDeserializedObjList.Count;
        string[] chkarr = new string[count-4];
        int j=0;
        for (int i = 0; i < count; i++)
        {
            if (myDeserializedObjList[i].name.Substring(0, 6) == "check_")
            {
                chkarr[j] = myDeserializedObjList[i].name.Substring(6,1);
                j++;
            }
        }

    }


}

}

Checkbox group or list is not needed. I Used hidden field which carries the (stringified checkbox values) Json. charr[] array can be put into the database as by our need either as bytes or anything else.

Upvotes: 1

Related Questions