Multiple Selection With Checkbox Under Grid View
Some Time You Need A Mail Like Look Or Just Say Multiple Selection User Interface With Whom User Can Easily Intract,Here I am giving you a short tutorial on how to do multiple selection job in grid view
<asp:GridView runat="server" ID="GridView1" DataKeyNames="amt">
<Columns>
<asp:TemplateField HeaderText="Select">
<HeaderTemplate>
<asp:CheckBox ID="chkSelectAll" runat="server" onclick="SelectAll(this.id)" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<p>
<asp:Button ID="btnGet" runat="server" Text="Get Selected Records" OnClick="GetSelected" />
</p>
<script language="javascript" type="text/javascript">
function SelectAll(Id)
{
var myform = document.forms[0];
var len = myform.elements.length;
document.getElementById(Id).checked == true ? document.getElementById(Id).checked = false : document.getElementById(Id).checked = true;
for (var i = 0; i < len; i++)
{
if (myform.elements[i].type == 'checkbox') {
if (myform.elements[i].checked) {
myform.elements[i].checked = false;
}
else
{
myform.elements[i].checked = true;
}
}
}
}
</script>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class R2_trial : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["yourconnectionstring"]);
DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetData();
}
}
private void GetData()
{
DataTable table = new DataTable();
// get the connection
{
// write the sql statement to execute
string sql = "SELECT amt,ProductId,Name from MyTable ";
// instantiate the command object to fire
using (SqlCommand cmd = new SqlCommand(sql, con))
{
// get the adapter object and attach the command object to it
using (SqlDataAdapter ad = new SqlDataAdapter(cmd))
{
// fire Fill method to fetch the data and fill into DataTable
ad.Fill(table);
}
}
}
// specify the data source for the GridView
GridView1.DataSource = table;
// bind the data now
GridView1.DataBind();
}
protected void GetSelected(object sender, EventArgs e)
{
Response.Write("<h3>Selected records</h3>");
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox chk = (CheckBox)row.FindControl("chkSelect");
if (chk.Checked)
{
Response.Write(GridView1.DataKeys[row.RowIndex].Value + ",");
}
}
}
}
thing highlighted with green color are strongly recommended for beginners to watch them and understand what and where they used in this project sample!
Note for Beginner :just Copy paste above code,apply breakpoints and view whats going on in every step.
Some Time You Need A Mail Like Look Or Just Say Multiple Selection User Interface With Whom User Can Easily Intract,Here I am giving you a short tutorial on how to do multiple selection job in grid view
1)First thing :your UI page i.e aspx page
<asp:GridView runat="server" ID="GridView1" DataKeyNames="amt">
<Columns>
<asp:TemplateField HeaderText="Select">
<HeaderTemplate>
<asp:CheckBox ID="chkSelectAll" runat="server" onclick="SelectAll(this.id)" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<p>
<asp:Button ID="btnGet" runat="server" Text="Get Selected Records" OnClick="GetSelected" />
</p>
<script language="javascript" type="text/javascript">
function SelectAll(Id)
{
var myform = document.forms[0];
var len = myform.elements.length;
document.getElementById(Id).checked == true ? document.getElementById(Id).checked = false : document.getElementById(Id).checked = true;
for (var i = 0; i < len; i++)
{
if (myform.elements[i].type == 'checkbox') {
if (myform.elements[i].checked) {
myform.elements[i].checked = false;
}
else
{
myform.elements[i].checked = true;
}
}
}
}
</script>
2)Your Server side coding i.e cs file
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class R2_trial : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["yourconnectionstring"]);
DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetData();
}
}
private void GetData()
{
DataTable table = new DataTable();
// get the connection
{
// write the sql statement to execute
string sql = "SELECT amt,ProductId,Name from MyTable ";
// instantiate the command object to fire
using (SqlCommand cmd = new SqlCommand(sql, con))
{
// get the adapter object and attach the command object to it
using (SqlDataAdapter ad = new SqlDataAdapter(cmd))
{
// fire Fill method to fetch the data and fill into DataTable
ad.Fill(table);
}
}
}
// specify the data source for the GridView
GridView1.DataSource = table;
// bind the data now
GridView1.DataBind();
}
protected void GetSelected(object sender, EventArgs e)
{
Response.Write("<h3>Selected records</h3>");
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox chk = (CheckBox)row.FindControl("chkSelect");
if (chk.Checked)
{
Response.Write(GridView1.DataKeys[row.RowIndex].Value + ",");
}
}
}
}
thing highlighted with green color are strongly recommended for beginners to watch them and understand what and where they used in this project sample!
Note for Beginner :just Copy paste above code,apply breakpoints and view whats going on in every step.
No comments:
Post a Comment