I having Five Rows in the Grid.In the GridView Template I using Button in Column[6].If I edit Third Row after I Click button (Column[6] ).I want to take the Column[9] value in the Grid from Selected Third Row in
Client Side.
In the RowDataBound event of the GridView, add an "onclick" attribute to the Button to a function that accepts "current index" as the parameter...It's something like this....
Javascript function:
function setCurrentIndexOfGrid(index) {
var grid = document.getElementById('<%=GridView1.ClientID %>');
var cell = grid.rows[(+index)+1].cells[9];
alert(cell.innerHTML);
}
KAMMIE Don't forget to Mark as Answer on the post that helped you. It encourages them to share their knowledge, and it helps others to easily identify the solution.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if ( e.Row.RowType == DataControlRowType.DataRow )
{
// The value that we want is in column #9...
string valueText = e.Row.Cells[9].Text;
// Find the Button with an ID of "gridButton" (in column #6 of the Grid)...
Button gridButton = (Button)e.Row.Cells[4].FindControl("gridButton");
if ( gridButton != null )
{
string clickHandler = string.Format("alert('Value in column #9 is {0}", valueText);
gridButton.Attributes.Add("onclick", clickHandler);
}
}
}
ganeshpa
0 Points
2 Posts
How to get the Gridview Selected row cell value in Javascript?
Mar 12, 2009 09:14 PM|LINK
Sir,
I having Five Rows in the Grid.In the GridView Template I using Button in Column[6].If I edit Third Row after I Click button (Column[6] ).I want to take the Column[9] value in the Grid from Selected Third Row in Client Side.
Please Reply Soon....
Regards,
Ganeshpa.S
grid view buttonfield click
kammie
Contributor
2163 Points
352 Posts
Re: How to get the Gridview Selected row cell value in Javascript?
Mar 12, 2009 09:47 PM|LINK
Button1.Attributes.Add("onclick", string.Format("setCurrentIndexOfGrid('{0}')", e.Row.RowIndex);
Javascript function:
function setCurrentIndexOfGrid(index) {
var grid = document.getElementById('<%=GridView1.ClientID %>');
var cell = grid.rows[(+index)+1].cells[9];
alert(cell.innerHTML);
}
Don't forget to Mark as Answer on the post that helped you. It encourages them to share their knowledge, and it helps others to easily identify the solution.
NC01
All-Star
82577 Points
15430 Posts
MVP
Re: How to get the Gridview Selected row cell value in Javascript?
Mar 13, 2009 03:32 PM|LINK
Try this:
aspx file:
<asp:GridView ID="GridView1" AutoGenerateColumns="False" ShowHeader="True" runat="server" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField HeaderText="0" DataField="Col0"/>
<asp:BoundField HeaderText="1" DataField="Col1"/>
<asp:BoundField HeaderText="2" DataField="Col2"/>
<asp:BoundField HeaderText="3" DataField="Col3"/>
<asp:BoundField HeaderText="4" DataField="Col4"/>
<asp:BoundField HeaderText="5" DataField="Col5"/>
<asp:TemplateField HeaderText="6">
<ItemTemplate>
<asp:Button ID="gridButton" Text="Press Me" runat="server"></asp:Button>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="7" DataField="Col7"/>
<asp:BoundField HeaderText="8" DataField="Col8"/>
<asp:BoundField HeaderText="9" DataField="Col9"/>
</Columns>
</asp:GridView>
aspx.cs file:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if ( e.Row.RowType == DataControlRowType.DataRow )
{
// The value that we want is in column #9...
string valueText = e.Row.Cells[9].Text;
// Find the Button with an ID of "gridButton" (in column #6 of the Grid)...
Button gridButton = (Button)e.Row.Cells[4].FindControl("gridButton");
if ( gridButton != null )
{
string clickHandler = string.Format("alert('Value in column #9 is {0}", valueText);
gridButton.Attributes.Add("onclick", clickHandler);
}
}
}
NC...