我为GRIDVIEW添加一个行事件  protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        e.Row.Attributes.Add("onclick", "alert(123)");
    }上面是最简单。我想当点击行事件的同时让此行的一个LABEL控件变个颜色 。。名为 label1如果实现呀。。帮忙 谢谢啦 。。

解决方案 »

  1.   

    点击行颜色变化或在js里遍历gridview,设置单元格颜色。
    或设置单元格颜色,查询label1
    label1.BackColor=System.Drawing.Color.FromName("#C0FFFF"); 
     e.Row.Cells[1].BackColor = System.Drawing.Color.Green;    
     protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
           
            if (e.Row.RowIndex != -1)
            {
                e.Row.Attributes.Add("onclick", "tog(this,'" + ApplicationAlert.pstrFirstColor + "')");
            }
        }
    var tgs; 
             var tmp_background_val; 
             function tog(n,flags)
             { 
               
                if (tgs){ 
                 tgs.style.background= tmp_background_val ; 
                } 
                
    n.style.background = '#D1D5E1' ;
                tmp_background_val=flags;
                tgs=n; 
             } 
      

  2.   

    不太理解呀  
    ApplicationAlert.pstrFirstColor  这个也不存在呀 
      

  3.   

    try the code below:
    // server side code
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            Label lbl = // get the Label control: e.g. (Label)row.Controls[2];
            e.Row.Attributes.Add("onclick",
                string.Format(CultureInfo.InvariantCulture, "toggle({0});", lbl.ClientID));
        }
    protected override void OnPreRender(EventArgs e)
        {
            string script = string.Format(CultureInfo.InvariantCulture,
                "function toggle(lbl) {" +
                "  if (lbl.style.color == 'red') {" +
                "    lbl.style.color = 'blue'; " +
                "  } else {" +
                "    lbl.style.color = 'red'; " +
                "  }"
                "}"; // 用 CSS 来管理颜色更好,这里图省事,直接修改style.color        ScriptManager.RegisterStartupScript(this, this.GetType(), "togglescript", script, true);
        }
      

  4.   


     protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label l=e.Row.Cells[3].Controls[0].FindControl("Label1") as Label;
                e.Row.Attributes.Add("onclick", "change('"+l.ClientID+"')");
            }
         
        }<script type="text/javascript">
            function change(element) {
                document.getElementById(element).style.backgroundColor = "red";
                alert(123);
            }
        </script>
      

  5.   

    这种事件完全可以卸载item_command事件当中,方便多了!
      

  6.   

     protected void MeetList_ItemCommand(object source, DataListCommandEventArgs e)
        {
            try
            {
                if (e.CommandName == "ibtnManager")
                {
                    Label lblmeetid = (Label)e.Item.FindControl("lblMeetID");
                    Response.Cookies["MeetID"].Value = lblmeetid.Text.ToString().Trim();
                    Session["Uer_Manager_MeetID"] = int.Parse(lblmeetid.Text.ToString().Trim());
                    Response.Redirect("Meet_Edit.aspx");
                }
            }
            catch (Exception ex)
            {
     
            }
        }
    参考一下这段代码。
      

  7.   

    Label l= (Label)GridView1.Item[e.Item.itemindex].FindControl("label1"); 
    l.BackColor=System.Drawing.Color.FromName("#C0FFFF"); 
    这个写在ItemCommand事件里面