现在能在GridView控件里启用选定内容来选定一行,但每次都要点那个选中按钮,有没有什么办法,不启用选定内容,而是通过代码来选定一行,就是说没有选中按钮,而是用鼠标点中哪行就选择哪行,谢谢

解决方案 »

  1.   

    可以在gridview的datarow添加属性,onclick,在onclick方法里写你要触发点击某一行时触发的事件
      

  2.   


    gridview里没有datarow这个属性啊
      

  3.   

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.RowType != DataControlRowType.Header)
                { 
                    e.Row.Attributes.Add("onclick", "this.style.backgroundColor='#ff0000'");
                }
            } 
        }
      

  4.   

    现在有空随便给你写个简单的例子好了
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="CMET.Web.test" %><html>
    <head>
        <title>test</title>
        <meta http-equiv="Content-Type " content="text/html;   charset=gb2312 ">
    </head>
    <body>
        <form id="form1" runat="server">
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound">
            <Columns>
                <asp:BoundField DataField="ID" HeaderText="ID" />
                <asp:BoundField DataField="Name" HeaderText="Name" />
            </Columns>
        </asp:GridView>
        </form>
    </body>
    </html>using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    namespace CMET.Web
    {
        public partial class test : System.Web.UI.Page
        {        protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    DataTable dt = new DataTable();
                    DataColumn dc = new DataColumn("ID", typeof(string));
                    DataColumn dc2 = new DataColumn("Name", typeof(string));
                    dt.Columns.Add(dc);
                    dt.Columns.Add(dc2);
                    
                    for (int i = 0; i < 10; i++)
                    {
                        DataRow dr = dt.NewRow();
                        dr["ID"] = i.ToString();
                        dr["Name"] = "Name" + i.ToString();
                        dt.Rows.Add(dr);
                    }
                    this.GridView1.DataSource = dt;
                    this.GridView1.DataBind();
                }
            }        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                e.Row.Attributes.Add("onclick", "window.alert('您选择了第"+(e.Row.RowIndex+1).ToString()+"行');");
            }
        }
    }
      

  5.   

    HTML:
    <script type="text/javascript"> 
        var oldrow=null; 
        var currentcolor=null; 
        var oldcolor=null; 
        function selectx(row) 
        { 
            if(oldrow!=null) 
            { 
                oldrow.style.backgroundColor=oldcolor; 
            } 
            row.style.backgroundColor='#99ccff'; 
            oldrow=row; 
            oldcolor = currentcolor;
        } 
    </script> .cs:
      
      protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {        if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "if(this!=oldrow){currentcolor=this.style.backgroundColor;this.style.backgroundColor='PeachPuff',this.style.fontWeight='';}");
                e.Row.Attributes.Add("onmouseout", "if(this!=oldrow){this.style.backgroundColor=currentcolor;this.style.fontWeight='';}");
                e.Row.Attributes.Add("onclick", e.Row.ClientID.ToString() + ".checked=true;selectx(this)");
            } 
        }