我在GridView里放了一个<input type="button">
想在button里加一个onclick  
请问如何用JS获取点击的button这行的数据呢~
可以获取么~?

解决方案 »

  1.   


    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %><!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>Untitled Page</title>    <script type="text/javascript">
       
           function showInfo(name)
           {
             alert(name);
           }
        </script></head>
    <body>
        <form id="form1" runat="server">
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound">
                <Columns>
                    <asp:BoundField DataField="ID" HeaderText="编号" />
                    <asp:TemplateField HeaderText="名称">
                        <ItemTemplate>
                            <asp:Literal ID="TestButton" runat="server"></asp:Literal>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </form>
    </body>
    </html>
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Collections.Generic;
    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 Default2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ID", typeof(string));
            dt.Columns.Add("Name", typeof(string));        DataRow dr = null;        for (int i = 0; i < 5; i++)
            {            dr = dt.NewRow();            dr["ID"] = i.ToString();
                dr["Name"] = "Name" + i.ToString();            dt.Rows.Add(dr);
            }        GridView1.DataSource = dt;
            GridView1.DataBind();
        }
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType != DataControlRowType.DataRow)
            {
                return;
            }        if (e.Row.FindControl("TestButton") != null)
            {
                Literal literal = e.Row.FindControl("TestButton") as Literal;            if (literal != null)
                {
                    DataRowView drv = e.Row.DataItem as DataRowView;
                    object[] oValue = drv.Row.ItemArray;
                    List<string> oList = new List<string>();
                    for (int i = 0, iLength = oValue.Length; i < iLength; i++)
                    {
                        oList.Add(oValue[i].ToString());
                    }                string strInfo = String.Join(",", oList.ToArray());
                    literal.Text = "<input type=\"button\" onclick=\"showInfo('" + strInfo + "')\" value=\"测试\" />";
                }
            }
        }
    }
      

  2.   

    对你这个问题不报幻想。“获取这行数据”这对于javascript编程人员来说有点托大。
      

  3.   

    这行的数据??值的是否是当前所在的tr td快,用jquery查找一下,根据this的parent
      

  4.   

    http://www.cnblogs.com/chenping-987123/archive/2010/08/27/1809877.html
    这个可以参考一下
      

  5.   

    函数传个参数this,然后通过jquery的parent、next、children等查找就好了