Saturday, 31 January 2015

Detect Checked CheckBox Indise GridView on Button Click In Asp.Net Using C#.Net

1/31/2015 - By Pranav Singh 0

This article will show you how you can detect the checkbox check inside gridview on button click on asp.net using c#.net.


So for this article first we will create a new asp.net application and add the below code into the aspx page.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!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 runat="server">
    <title>Detect Checked CheckBox Indise GridView on Button Click In Asp.Net Using C#.Net
    </title>
</head>
<body>
    <form id="form1" runat="server">
    <table width="100%" cellpadding="4" cellspacing="4">
        <tr>
            <td align="left">
                <asp:GridView ID="GridView1" runat="server" Width="100%" AutoGenerateColumns="False"
                    AllowPaging="True">
                    <Columns>
                        <asp:TemplateField HeaderText="">
                            <ItemTemplate>
                                <asp:CheckBox ID="CheckBox1" runat="server" />                            
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Name">
                         <ItemTemplate>
                             <asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
                         </ItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField DataField="Address" HeaderText="Address" />
                        <asp:BoundField DataField="UserType" HeaderText="UserType" />
                    </Columns>
                </asp:GridView>
            </td>
        </tr>
    </table>
    <asp:Label ID="lblSelectedName" runat="server" Style="color: #FF0000" Text=""></asp:Label>
    <br />
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Submit" />
    </form>
</body>
</html>

In this I have added gridview and a button control and label control. So when user click on button checkbox selected row name we will display on the label control.

Now please check the code which we will used for managing the checkbox control detection inside gridview.

using System;
using System.Data.SqlClient;
using System.Data;
using System.Web.UI.WebControls;
using System.Collections;
namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetDistinctValue();
            }
        }
        private void GetDistinctValue()
        {
            SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["con"].ToString());
            try
            {
                DataTable objdt = new DataTable();
                string query = "select * from UserDetail;";
                SqlDataAdapter da = new SqlDataAdapter(query, con);
                con.Open();
                da.Fill(objdt);
                con.Close();
                if (objdt.Rows.Count > 0)
                {
                    GridView1.DataSource = objdt;
                    GridView1.DataBind();
                }
            }
            catch
            {
                con.Close();
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string SelectedName = "";
            foreach (GridViewRow item in GridView1.Rows)
            {
                CheckBox checkbox1 = (CheckBox)item.FindControl("CheckBox1");              
                if (checkbox1.Checked == true)
                {
                    Label lblName = (Label)item.FindControl("lblName");
                    SelectedName = SelectedName + lblName.Text + ",";
                }
            }
            lblSelectedName.Text = SelectedName;
        }
    }
}

In above code I have first bind the grid view and then on button click event I have detected checkbox control and on the bases of selection I have detected the name of label control to check the name to display.

Have a look of the code to detect the checkbox checked.

protected void Button1_Click(object sender, EventArgs e)
        {
            string SelectedName = "";
            foreach (GridViewRow item in GridView1.Rows)
            {
                CheckBox checkbox1 = (CheckBox)item.FindControl("CheckBox1");              
                if (checkbox1.Checked == true)
                {
                    Label lblName = (Label)item.FindControl("lblName");
                    SelectedName = SelectedName + lblName.Text + ",";
                }
            }
            lblSelectedName.Text = SelectedName;
        }

Now we have done the application to check the output.


About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top