This is featured post 1 title
Replace these every slider sentences with your featured post descriptions.Go to Blogger edit html and find these sentences.Now replace these with your own descriptions.This theme is
This is featured post 2 title
Replace these every slider sentences with your featured post descriptions.Go to Blogger edit html and find these sentences.Now replace these with your own descriptions.This theme is Bloggerized by Lasantha - Premiumbloggertemplates.com.
This is featured post 3 title
Replace these every slider sentences with your featured post descriptions.Go to Blogger edit html and find these sentences.Now replace these with your own descriptions.This theme is Bloggerized by Lasantha - Premiumbloggertemplates.com.
Showing posts with label Grid View. Show all posts
Showing posts with label Grid View. Show all posts
Friday, 13 July 2012
Get ASP.Net Grid View Row Value and its Row Index when clicked using JavaScript
04:44
Unknown
Introduction
Get ASP.Net Grid View Row Value and its Row Index when clicked using JavaScript
Here I explained how to get the ASP.Net Grid View Row Value and its Row Index client side using JavaScript. He has also explained how we can find the Grid View Cells and the controls value inside the Grid View Template Fields client side using JavaScript.
Description
Below I have a simple ASP.Net Grid View. That content two columns SNo and Name both column are Template Fields.
When any gird View Cell is clicked, then Cell value and cell index value show on Alert box.
<%@ 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 runat="server">
<title>Get ASP.Net Grid View Row Value and its Row Index when clicked using JavaScript title>
<style type="text/css">
table.hovertable
{
font-family: verdana,arial,sans-serif;
font-size: 11px;
color: #333333;
border-width: 1px;
border-color: #999999;
border-collapse: collapse;
}
table.hovertableth
{
background-color: #c3dde0;
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #a9c6c9;
}
table.hovertabletr
{
background-color: #d4e3e5;
}
table.hovertabletd
{
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #a9c6c9;
}
.style1
{
height: 400px;
width: 33%;
}
.style2
{
height: 200px;
width: 33%;
}
style>
<script type="text/javascript">
functionaddhiddenfieldValue(cellvalue,portvalue)
{
alert('You Click on Cell NO:'+cellvalue+' and Cell Value:'+portvalue);
}
script>
head>
<body>
<form id="form1" runat="server">
<div class="hovertable">
<asp:HiddenField ID="hdnfsplicvalue" runat="server" />
<table class="hovertable" align="center">
<tr>
<td valign="top" id="from" runat="server">
<asp:GridView runat="server" ID="GrdFrom" AutoGenerateColumns="false" Width="100%"
OnRowDataBound="GrdFrom_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="From"SortExpression="From">
<ItemTemplate>
<asp:Label ID="txtSNO" BorderWidth="0"BorderStyle="None"runat="server"Text='<%# Bind("SNO") %>'>asp:Label>
ItemTemplate>
asp:TemplateField>
<asp:TemplateField HeaderText="Name"SortExpression="Name">
<ItemTemplate>
<asp:Label ID="txtName"BorderWidth="0"BorderStyle="None"runat="server"Text='<%# Bind("Name") %>'>asp:Label>
ItemTemplate>
asp:TemplateField>
Columns>
asp:GridView>
td>
tr>
table>
div>
form>
body>
html>
using System;
usingSystem.Configuration;
usingSystem.Data;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.HtmlControls;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Xml.Linq;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(objectsender, EventArgs e)
{
if(!IsPostBack)
{
this.BindGridView();
}
}
private void BindGridView()
{
DataTable_dtData = new DataTable();
_dtData.Columns.Add("SNO");
_dtData.Columns.Add("Name");
_dtData.Rows.Add("1", "Sunil Gurjar");
_dtData.Rows.Add("2", "Prashant");
_dtData.Rows.Add("3", "anil Gurjar");
_dtData.Rows.Add("4", "Narendra Gurjar");
_dtData.Rows.Add("5", "Varun Gurjar");
GrdFrom.DataSource = _dtData;
GrdFrom.DataBind();
}
protected voidGrdFrom_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataRowViewrow = e.Row.DataItem as DataRowView;
if(e.Row.RowType == DataControlRowType.DataRow)
{
Label cell1value = e.Row.Cells[0].FindControl("txtSNO") as Label;
Label cell3value = e.Row.Cells[1].FindControl("txtName") as Label;
e.Row.Cells[0].Attributes.Add("onclick", "addhiddenfieldValue('1','"+ cell1value.Text + "');");
e.Row.Cells[1].Attributes.Add("onclick", "addhiddenfieldValue('2','" + cell3value.Text + "');");
}
}
}
Downloads
You can download the complete source code in C#
in ASP.NET copy cell value from one gridview and paste to another gridview
04:02
Unknown
Introduction
Here I explained how to get and Set the ASP.Net Grid View Row value to other Grid View Row client side using JavaScript. This feature using Click on cell 2 value of grid view and then click on other grid view at any ROW then this value is copy the Second Grid View. And also this process also for second grid copy value from CELL 2 using click on cell then paste this value on first Gird view using click on first gird view.
<%@ 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 runat="server">
<title>Copy value from one grid to other gird viewtitle>
<style type="text/css">
table.tableizer-table
{
border: 1px solid #CCC;
font-family: Arial, Helvetica,sans-serif;
font-size: 12px;
}
.tableizer-tabletd
{
padding: 4px;
margin: 3px;
border: 1px solid #ccc;
}
.tableizer-tableth
{
background-color: #104E8B;
color: #FFF;
font-weight: bold;
}
.style3
{
width: 258px;
}
style>
<script type="text/javascript">
functionaddhiddenfieldValue(cellvalue,portvalue)
{
if(cellvalue == 2)
{
document.getElementById('hdnfsplicvalue').value=portvalue;
}
else
{
varSpliceValue = document.getElementById('hdnfsplicvalue').value;
if(SpliceValue != '')
{
varobjCell = document.getElementById(portvalue);
if(objCell)
{
objCell.value = SpliceValue;
document.getElementById('hdnfsplicvalue').value = '';
}
}
}
}
functionsetReadonly()
{
vara=document.getElementsByTagName('input');
for(i=0;i<=a.length;i++)
{
if(a[i])
{
a[i].readOnly=true;
}
}
}
script>
head>
<body>
<form id="form1" runat="server">
<div class="tableizer-table">
<asp:HiddenField ID="hdnfsplicvalue" runat="server" />
<table style="width: 42%; height: 393px;" class="tableizer-table"align="center">
<tr>
<td valign="top" id="from" runat="server">
First Grid
<br />
<asp:GridView runat="server" ID="GrdFrom" AutoGenerateColumns="false" Height="363px"
OnRowDataBound="GrdFrom_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="From"SortExpression="From">
<ItemTemplate>
<asp:TextBox ID="txtFrom"BorderColor="#999999"BorderWidth="0"BorderStyle="None"
runat="server"Text='<%# Bind("From") %>' Width="80px" Wrap="true">asp:TextBox>
ItemTemplate>
asp:TemplateField>
<asp:BoundField DataField="Port"HeaderText="Port"HeaderStyle-Width="80px">
<HeaderStyle Width="80px">HeaderStyle>
asp:BoundField>
<asp:TemplateField HeaderText="To"SortExpression="To">
<ItemTemplate>
<asp:TextBox ID="txtTo" BorderColor="#999999"BorderWidth="0"BorderStyle="None"
runat="server"Text='<%# Bind("To") %>' Width="80px" Wrap="true">asp:TextBox>
ItemTemplate>
asp:TemplateField>
Columns>
asp:GridView>
td>
<td valign="top" id="section" runat="server">
Second Grid
<br />
<asp:GridView runat="server" ID="grdSection" AutoGenerateColumns="false" Height="362px"
OnRowDataBound="grdSection_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="From"SortExpression="From">
<ItemTemplate>
<asp:TextBox ID="txtFrom"BorderColor="#999999"BorderWidth="0"BorderStyle="None"
runat="server"Text='<%# Bind("From") %>' Width="80px" Wrap="true">asp:TextBox>
ItemTemplate>
asp:TemplateField>
<asp:BoundField DataField="Port"HeaderText="Port"HeaderStyle-Width="80px">
<HeaderStyle Width="80px">HeaderStyle>
asp:BoundField>
<asp:TemplateField HeaderText="To"SortExpression="To">
<ItemTemplate>
<asp:TextBox ID="txtTo" BorderColor="#999999"BorderWidth="0"BorderStyle="None"
runat="server"Text='<%# Bind("To") %>' Width="80px" Wrap="true">asp:TextBox>
ItemTemplate>
asp:TemplateField>
Columns>
asp:GridView>
td>
tr>
table>
div>
form>
body>
html>
using System;
usingSystem.Configuration;
usingSystem.Data;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.HtmlControls;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Xml.Linq;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(objectsender, EventArgs e)
{
if(!IsPostBack)
{
this.BindGridView();
}
}
private void BindGridView()
{
DataTable_dtData = new DataTable();
_dtData.Columns.Add("From");
_dtData.Columns.Add("Port");
_dtData.Columns.Add("To");
for (int i = 0; i < 5; i++)
{
_dtData.Rows.Add("","From-Port-"+i,"");
}
GrdFrom.DataSource = _dtData;
GrdFrom.DataBind();
DataTable_dtData1 = new DataTable();
_dtData1.Columns.Add("From");
_dtData1.Columns.Add("Port");
_dtData1.Columns.Add("To");
for (int i = 0; i < 5; i++)
{
_dtData1.Rows.Add("", "To-Port-"+ i, "");
}
grdSection.DataSource = _dtData1;
grdSection.DataBind();
}
protected void GrdFrom_RowDataBound(objectsender, GridViewRowEventArgs e)
{
DataRowViewrow = e.Row.DataItem as DataRowView;
if(e.Row.RowType == DataControlRowType.DataRow)
{
TextBox cell1value = e.Row.Cells[0].FindControl("txtFrom") asTextBox;
TextBox cell3value = e.Row.Cells[2].FindControl("txtTo") asTextBox;
e.Row.Cells[0].Attributes.Add("onclick", "addhiddenfieldValue('1','"+ cell1value.ClientID + "');");
e.Row.Cells[1].Attributes.Add("onclick", "addhiddenfieldValue('2','"+ e.Row.Cells[1].Text + "');");
e.Row.Cells[2].Attributes.Add("onclick", "addhiddenfieldValue('3','"+ cell3value.ClientID + "');");
}
}
protected void grdSection_RowDataBound(objectsender, GridViewRowEventArgs e)
{
DataRowViewrow = e.Row.DataItem as DataRowView;
if(e.Row.RowType == DataControlRowType.DataRow)
{
TextBoxcell1value = e.Row.Cells[0].FindControl("txtFrom") as TextBox;
TextBoxcell3value = e.Row.Cells[2].FindControl("txtTo") as TextBox;
e.Row.Cells[0].Attributes.Add("onclick", "addhiddenfieldValue('1','"+ cell1value.ClientID + "');");
e.Row.Cells[1].Attributes.Add("onclick", "addhiddenfieldValue('2','"+ e.Row.Cells[1].Text + "');");
e.Row.Cells[2].Attributes.Add("onclick", "addhiddenfieldValue('3','"+ cell3value.ClientID + "');");
}
}
}
Description
I got requirement like Copy Cell value from One Grid View and Paste this value to Second Grid View cell using Client Site JavaScript so I implemented this code for this activity.
Downloads
You can download the complete source code in C#

