Pages

Ads 468x60px

For New Update Use this Link http://dotnethubs.blogspot.in/ Offer for you

.

Showing posts with label asp.net checkbox. Show all posts
Showing posts with label asp.net checkbox. Show all posts

Thursday 25 April 2013

onclick checkbox select data from gridview asp.net

onclick checkbox select data from gridview asp.net
 Introduction : -

In this Tutorial when user select the one or multipal  checkbox  from the gridview and show the values on the form.

Description : -
 
In my Previous article i have provided the asp.net interview questions with answers for 2 years experience  Ajax AsyncFileUpload control example in asp.net to upload files to server   jquery form validation in asp.net  I have Provide the More Difference b/w Differences Between WCF and ASP.NET Web Services   and also provide the  create restful wcf service api using post step by step
To get checkbox selected row values from gridview we need to write the code like this


C# Code

foreach(GridViewRow  gvrow in gvDetails.Rows)
{
CheckBox chk = (CheckBox)gvrow.FindControl("chkSelect");
if (chk != null & chk.Checked)
{
str += gvDetails.DataKeys[gvrow.RowIndex].Value.ToString() + ',';
strname += gvrow.Cells[2].Text+',';
}
}
If you want to see complete example we need to write the following code in aspx page
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    <script type="text/javascript" language="javascript">

        function ConfirmOnDelete(item) {

            if (confirm("Would you like to delete selected item(s)?") == true)

                return true;

            else

                return false;

        }

    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:GridView ID="gvDetails" DataKeyNames="UserId" AutoGenerateColumns="False"
            CellPadding="4" runat="server" ForeColor="#333333" GridLines="None">
          <AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="UserId" DataField="UserId" />
<asp:BoundField HeaderText="UserName" DataField="UserName" />
<asp:BoundField HeaderText="Education" DataField="Education" />
<asp:BoundField HeaderText="Location" DataField="Location" />
</Columns>
          <EditRowStyle BackColor="#7C6F57" />
          <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="true" ForeColor="White" />
          <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
          <RowStyle BackColor="#E3EAEB" />
          <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
          <SortedAscendingCellStyle BackColor="#F8FAFA" />
          <SortedAscendingHeaderStyle BackColor="#246B61" />
          <SortedDescendingCellStyle BackColor="#D4DFE1" />
          <SortedDescendingHeaderStyle BackColor="#15524A" />
</asp:GridView>
<asp:Button ID="btnProcess" Text="Get Selected Records" runat="server"
Font-Bold="true" onclick="btnProcess_Click" /><br />
<asp:Label ID="lblmsg" runat="server" />
    </div>
    </form>
</body>
</html>

Now in code behind add the following namespaces

C# Code

using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
Now add below code in code behind

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridviewData();
        }
  
    }

     protected void BindGridviewData()
{
DataTable dt = new DataTable();
dt.Columns.Add("UserId", typeof(Int32));
dt.Columns.Add("UserName", typeof(string));
dt.Columns.Add("Education", typeof(string));
dt.Columns.Add("Location", typeof(string));
DataRow dtrow = dt.NewRow();    // Create New Row
dtrow["UserId"] = 1;            //Bind Data to Columns
dtrow["UserName"] = "chitranjan";
dtrow["Education"] = "M.C.A";
dtrow["Location"] = "Indore";
dt.Rows.Add(dtrow);
dtrow = dt.NewRow();               // Create New Row
dtrow["UserId"] = 2;               //Bind Data to Columns
dtrow["UserName"] = "Aman";
dtrow["Education"] = "B.E";
dtrow["Location"] = "Pune";
dt.Rows.Add(dtrow);
dtrow = dt.NewRow();              // Create New Row
dtrow["UserId"] = 3;              //Bind Data to Columns
dtrow["UserName"] = "Rahul";
dtrow["Education"] = "B.Tech";
dtrow["Location"] = "Bombai";
dt.Rows.Add(dtrow);
dtrow = dt.NewRow();              // Create New Row
dtrow["UserId"] = 4;              //Bind Data to Columns
dtrow["UserName"] = "Amar";
dtrow["Education"] = "B.E";
dtrow["Location"] = "Bhopal";
dt.Rows.Add(dtrow);

gvDetails.DataSource = dt;
gvDetails.DataBind();
}


     protected void btnProcess_Click(object sender, EventArgs e)
     {
         string str = string.Empty;
         string strname = string.Empty;
         foreach (GridViewRow gvrow in gvDetails.Rows)
         {
             CheckBox chk = (CheckBox)gvrow.FindControl("chkSelect");
             if (chk != null & chk.Checked)
             {
                 str += gvDetails.DataKeys[gvrow.RowIndex].Value.ToString() + ',';
                 strname += gvrow.Cells[2].Text + ',';
             }
         }
         str = str.Trim(",".ToCharArray());
         strname = strname.Trim(",".ToCharArray());
         lblmsg.Text = "Selected UserIds: <b>" + str + "</b><br/>" + "Selected UserNames: <b>" + strname + "</b>";
     }
Screen Short as shown below

After Select Check Box Show The Data


Download sample code attached

 

..




New Updates

Related Posts Plugin for WordPress, Blogger...

Related Result