当前位置: 代码迷 >> ASP.NET >> gridview单击行任意位置事件! 单击行显示隐藏的panel,该如何处理
  详细解决方案

gridview单击行任意位置事件! 单击行显示隐藏的panel,该如何处理

热度:1410   发布时间:2013-02-25 00:00:00.0
gridview单击行任意位置事件! 单击行显示隐藏的panel
gridview单击行任意位置显示panel不会!

我知道在GridView1_RowDataBound中做单击双击事件

代码如下:
C# code
//鼠标移动改变行的颜色,自动编号    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)    {        //其实就是给GridView加上鼠标划过的JS        if (e.Row.RowType == DataControlRowType.DataRow)        {            //当鼠标停留时更改背景色            e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#EBFBBA'");            //当鼠标移开时还原背景色            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");            //单击/双击 事件             e.Row.Attributes["ondblclick"] = String.Format("javascript:dbl_click=true;window.open('check_xx.aspx?id={0}','mainFrame');", GridView1.DataKeys[e.Row.RowIndex].Value.ToString());            e.Row.Attributes["title"] = "双击打开详细页面";                        e.Row.Attributes.Add("onclick", "clickevent(this.Panel1.Visible=true)");                                    //自动编号            if (e.Row.RowIndex != -1)            {                int idb = e.Row.RowIndex + 1;                e.Row.Cells[0].Text = idb.ToString();            }            //判断事由事项字符大于15个字后用...替代            if ((e.Row.Cells[3].Text).Length > 10)            {                e.Row.Cells[3].Text = (e.Row.Cells[3].Text).Substring(0, 10) + "…";            }            if ((e.Row.Cells[4].Text).Length > 10)            {                e.Row.Cells[4].Text = (e.Row.Cells[4].Text).Substring(0, 10) + "…";            }                    }    }


我知道这句写的不对!
e.Row.Attributes.Add("onclick", "clickevent(this.Panel1.Visible=true)");

还是应该写在GridView1_SelectedIndexChanged里?

下面这个是 点击行里的button实现的效果
(显示隐藏的panel,及根据gridview1行选择得到id,再根据id取得number号,再根据这个number号取得另一个数据表里的数据显示在gridview2中)
考虑到用户操作的方便性,我是想点击行中的任意位置实现这样的效果!

C# code
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)    {        this.Label2.Text = this.GridView1.SelectedValue.ToString();        this.Panel1.Visible = true;                //string number=this.GridView1.Rows[1].Cells.ToString();                //this.Label3.Text = this.GridView1.Rows[1].Cells.ToString();                //接收订单号        string sql = "select * from A where id=" + this.Label2.Text;        sms_conn.Open();        SqlDataAdapter da = new SqlDataAdapter(sql, sms_conn);        DataSet ds = new DataSet();        da.Fill(ds, "A");        DataRowView rowview = ds.Tables["A"].DefaultView[0];        this.Label3.Text = rowview["number"].ToString();                string Sql2 = "select * from checksh where num="+ "'"+this.Label3.Text+"'";        SqlDataAdapter da2 = new SqlDataAdapter(Sql2, sms_conn);        DataSet ds2 = new DataSet();        try        {            da2.Fill(ds2, "checksh");            GridView2.DataSource = ds2.Tables["checksh"].DefaultView;            GridView2.DataBind();                    }        catch (Exception ex)        {


附上图片


------解决方案--------------------------------------------------------
在你现有程序上 在GridView1_RowDataBound 事件中 加上

C# code
Button btn = e.Row.FindControl("buttonid") as Button;if(btn!=null){   e.Row.Attributes.Add("onclick", "document.getElementById('btn.ClientID').click();");}
------解决方案--------------------------------------------------------