js获取鼠标点击GridView中某一列的值

2024-10-31 21:30:27
有1个网友回答
网友(1):

我用的是asp.net,GridView实现它的RowDataBound事件,具体看代码,希望可以帮到你:
前台:



    
    
    
        function getId(row_index) {
            var grid_view = document.getElementById('<%=GridView1.ClientID %>');
            var rows = grid_view.rows;
            var personID = rows[row_index].cells[0].innerHTML;
            alert("获取的ID为:" + personID);
        }
    


    
    

        
            
                                    HeaderStyle-BackColor="LightSkyBlue" />
                                    HeaderStyle-BackColor="LightSkyBlue" />
                                    HeaderStyle-BackColor="LightSkyBlue" />
            

        
    

    



后台代码:
public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            List list = new List();
            list.Add(new Person("001", "张三"));
            list.Add(new Person("002", "李四"));
            list.Add(new Person("003", "王五"));
            list.Add(new Person("004", "赵六"));
            list.Add(new Person("005", "何七"));
            GridView1.DataSource = list;
            GridView1.DataBind();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int row_index = e.Row.RowIndex + 1;
                e.Row.Attributes.Add("onclick", "getId(" + row_index + ");");
            }
        }
    }

    class Person
    {
        public Person(string id, string name)
        {
            ID = id;
            Name = name;
        }

        private string id;

        public string ID
        {
            get { return id; }
            set { id = value; }
        }
        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        private string tel;

        public string Tel
        {
            get { return tel; }
            set { tel = value; }
        }
    }