How to find values of selected checkbox after submit in C#?

................................source code........................

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>ASP.NET - Get selected checkbox value in DataList</title>
    <style type="text/css">
    body { background-color:white; margin-left:0; margin-top:1; margin-right:0; margin-bottom:0;font-family:Segoe UI,Tahoma,Arial,Verdana,sans-serif; font-size:10pt}
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DataList ID="BooksDataList" runat="server" BackColor="LightGoldenrodYellow" EnableViewState="false"
            BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black">
            <AlternatingItemStyle BackColor="PaleGoldenrod" />
            <FooterStyle BackColor="Tan" />
            <HeaderStyle BackColor="Tan" Font-Bold="True" />
            <SelectedItemStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
            <HeaderTemplate>List of Books</HeaderTemplate>
         <ItemTemplate>
           Select: <input runat="server" id="FavChkBox" type="checkbox" value='<%#Eval("ISBN") %>' /> <br />
           ISBN: <asp:Label ID="lblISBN" runat="Server" Text='<%#Eval("ISBN") %>' /> <br />
           Title: <asp:Label ID="Label2" runat="Server" Text='<%#Eval("Title") %>' /> <br />
           Author: <asp:Label ID="Label3" runat="Server" Text='<%#Eval("Author") %>' /> <br />
         </ItemTemplate>
        </asp:DataList>

    </div>
   
    <asp:CheckBox ID="chkAll" runat="server" Text="Select All" /> <br />
    <asp:Button ID="btnSubmit" runat="server" onclick="Button1_Click" Text="Submit" /> <br />
    <asp:Label ID="LblText" runat="server" Text="" /><br />
    <asp:Label ID="LblHiddenField" runat="server" Text="" />
    <asp:HiddenField ID="HiddenField1" runat="server" />

    </form>
    <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>

    <script type="text/javascript">
        $(document).ready(function () {
            //check all
            $("[id$='chkAll']").click(
             function () {
                 $("INPUT[type='checkbox']").attr('checked', $("[id$='chkAll']").is(':checked'));
             });

            $("[id$='btnSubmit']").click(function () {
                var ISBN = [];

                $("[id$='BooksDataList'] input[type=checkbox]:checked").each(function () {
                    ISBN.push($(this).val());
                });

                $("[id$='HiddenField1']").val(ISBN);
            });
        });

    </script>
</body>
</html>
...............................end souce ............................
....................................code behind............................
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using test;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //  if (!IsPostBack)
        //  {
        PopulateData();
        //  }

    }

    void PopulateData()
    {
        BookService bs = new BookService();

        BooksDataList.DataSource = bs.GetBooks();
        BooksDataList.DataBind();
    }

    void GetCheckedBox()
    {
        foreach (DataListItem li in BooksDataList.Items)
        {
            HtmlInputCheckBox cb = li.FindControl("FavChkBox") as HtmlInputCheckBox;

            if (cb != null)
            {
                if (cb.Checked)
                {
                    if (LblText.Text.Length > 0)
                        LblText.Text += ", ";

                    LblText.Text += cb.Value;
                }
            }
        }
    }
    
    protected void Button1_Click(object sender, EventArgs e)
    {
        LblText.Text = "Checkbox value from Code Behind: ";
        
        GetCheckedBox();

        LblHiddenField.Text = "Checkbox value from jQuery: " + HiddenField1.Value;

    }
}
.........................end code behind..............................
Share:

No comments:

Post a Comment

Sunday, 16 December 2012

How to find values of selected checkbox after submit in C#?

................................source code........................

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>ASP.NET - Get selected checkbox value in DataList</title>
    <style type="text/css">
    body { background-color:white; margin-left:0; margin-top:1; margin-right:0; margin-bottom:0;font-family:Segoe UI,Tahoma,Arial,Verdana,sans-serif; font-size:10pt}
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DataList ID="BooksDataList" runat="server" BackColor="LightGoldenrodYellow" EnableViewState="false"
            BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black">
            <AlternatingItemStyle BackColor="PaleGoldenrod" />
            <FooterStyle BackColor="Tan" />
            <HeaderStyle BackColor="Tan" Font-Bold="True" />
            <SelectedItemStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
            <HeaderTemplate>List of Books</HeaderTemplate>
         <ItemTemplate>
           Select: <input runat="server" id="FavChkBox" type="checkbox" value='<%#Eval("ISBN") %>' /> <br />
           ISBN: <asp:Label ID="lblISBN" runat="Server" Text='<%#Eval("ISBN") %>' /> <br />
           Title: <asp:Label ID="Label2" runat="Server" Text='<%#Eval("Title") %>' /> <br />
           Author: <asp:Label ID="Label3" runat="Server" Text='<%#Eval("Author") %>' /> <br />
         </ItemTemplate>
        </asp:DataList>

    </div>
   
    <asp:CheckBox ID="chkAll" runat="server" Text="Select All" /> <br />
    <asp:Button ID="btnSubmit" runat="server" onclick="Button1_Click" Text="Submit" /> <br />
    <asp:Label ID="LblText" runat="server" Text="" /><br />
    <asp:Label ID="LblHiddenField" runat="server" Text="" />
    <asp:HiddenField ID="HiddenField1" runat="server" />

    </form>
    <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>

    <script type="text/javascript">
        $(document).ready(function () {
            //check all
            $("[id$='chkAll']").click(
             function () {
                 $("INPUT[type='checkbox']").attr('checked', $("[id$='chkAll']").is(':checked'));
             });

            $("[id$='btnSubmit']").click(function () {
                var ISBN = [];

                $("[id$='BooksDataList'] input[type=checkbox]:checked").each(function () {
                    ISBN.push($(this).val());
                });

                $("[id$='HiddenField1']").val(ISBN);
            });
        });

    </script>
</body>
</html>
...............................end souce ............................
....................................code behind............................
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using test;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //  if (!IsPostBack)
        //  {
        PopulateData();
        //  }

    }

    void PopulateData()
    {
        BookService bs = new BookService();

        BooksDataList.DataSource = bs.GetBooks();
        BooksDataList.DataBind();
    }

    void GetCheckedBox()
    {
        foreach (DataListItem li in BooksDataList.Items)
        {
            HtmlInputCheckBox cb = li.FindControl("FavChkBox") as HtmlInputCheckBox;

            if (cb != null)
            {
                if (cb.Checked)
                {
                    if (LblText.Text.Length > 0)
                        LblText.Text += ", ";

                    LblText.Text += cb.Value;
                }
            }
        }
    }
    
    protected void Button1_Click(object sender, EventArgs e)
    {
        LblText.Text = "Checkbox value from Code Behind: ";
        
        GetCheckedBox();

        LblHiddenField.Text = "Checkbox value from jQuery: " + HiddenField1.Value;

    }
}
.........................end code behind..............................

No comments:

Post a Comment

Popular

Blog Archive

Total Pageviews

Archive