Click here to Skip to main content
15,917,005 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

how to change the value in a gridview column for selected row on button click using javascript.
that is i want to a new value to a column using javascript function.

thanks
Posted
v2
Comments
What have you tried so far ?
Thanks7872 9-May-13 2:00am    
What does it mean by change the value in a gridview column for selected row?Can you elaborate more on this? Any example?

1 solution

XML
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:GridView ID="grd" runat="server" BackColor="White" BorderColor="#CC9966"
            BorderStyle="None" BorderWidth="1px" CellPadding="4">
            <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
            <Columns>
                <asp:CommandField ButtonType="Button" ShowSelectButton="True" />
            </Columns>
            <RowStyle BackColor="White" ForeColor="#330099" />
            <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
            <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
            <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
        </asp:GridView>
    </form>
</body>
</html>






C#
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable tbl = new DataTable();
        tbl.Columns.Add("Name");
        tbl.Columns.Add("Title");
        tbl.Columns.Add("FullName");

        tbl.Rows.Add("DINESH", "TAILOR", "");
        tbl.Rows.Add("RAJESH", "TAILOR", "");

        grd.RowDataBound += new GridViewRowEventHandler(grd_RowDataBound);

        grd.DataSource = tbl;
        grd.DataBind();
    }

    void grd_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Button b = (Button)e.Row.Controls[0].Controls[0];

            b.Attributes.Add("onClick", "var tr = document.getElementById('" + e.Row.ClientID + "'); tr.cells[3].innerText = tr.cells[1].innerText + ' ' + tr.cells[2].innerText; return false;");
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900