• GridView 事件


    1.主要是对gridview 事件说明;

      aspx:

        <form id="form1" runat="server">
        <input type="text" id="text_id" value="23" />
        <div>
        </div>
        <asp:GridView ID="GridView1" runat="server"
            onrowdatabound="GridView1_RowDataBound" ondatabound="GridView1_DataBound"
            onrowcreated="GridView1_RowCreated">
        </asp:GridView>
        </form>
         <script>
             function getvalue(objtext) { document.getElementById("text_id").value = objtext; }
        </script>

    cs;

     protected void Page_Load(object sender, EventArgs e)     {        

          this.GridView1.DataSource = GetDataTable();  

           this.GridView1.DataBind();

    }

    //数据源构造

    public System.Data.DataTable GetDataTable()
        {
            System.Data.DataTable dt = new System.Data.DataTable();
            dt.Columns.Add("Id", typeof(int));
            dt.Columns.Add("Name",typeof(string));
            dt.Columns.Add("Sex",typeof(int));
            for (int i = 0; i < 20; i++)
       {
                dt.Rows.Add(i, "lin.su" + i,i%2==0?0:1);
       }
            dt.AcceptChanges();
            return dt;
        }

     //数据行绑定事件

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)     {  

           if (e.Row.RowType == DataControlRowType.DataRow)  

           {           

                   //鼠标经过时,行背景色变            

                    e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#E6F5FA'");            

                 //鼠标移出时,行背景色变            

                    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");

                //当有编辑列时,避免出错,要加的RowState判断

                if (e.Row.RowState == DataControlRowState.Normal ||

                            e.Row.RowState == DataControlRowState.Alternate)                                 

                     {                

                      e.Row.Attributes.Add("onclick", "getvalue('" + e.Row.Cells[0].Text + "')");  

                  }

            }    

    }

    //创建行事件 主要是生成表头

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)    

    {        

    switch (e.Row .RowType)     

        {            

                    case DataControlRowType.Header:             

                    TableCellCollection tabcollention = e.Row.Cells;            

                    tabcollention.Clear();            

                    tabcollention.Add(new TableHeaderCell());              

                    tabcollention[0].Text = "编号";            

                    tabcollention.Add(new TableHeaderCell());            

                    tabcollention[1].Text = "名称";            

                    tabcollention.Add(new TableHeaderCell());            

                   tabcollention[2].Text = "性别";           

                        break;       

           }       

        }

    //gridview 绑定数据事件

     protected void GridView1_DataBound(object sender, EventArgs e)    

    {        

                  for (int i = 0; i <= GridView1.Rows.Count - 1; i++)       

                          {            

                                  if (Convert.ToInt16(GridView1.Rows[i].Cells[2].Text) == 0) //sex           

                                       {                

                                         GridView1.Rows[i].Cells[2].BackColor = System.Drawing.Color.Red;

                                        GridView1.Rows[i].Cells[2].Text = "男";            

                                     }            

                                  else     

                                   {                

                                       GridView1.Rows[i].Cells[2].BackColor = System.Drawing.Color.Yellow;

                                       GridView1.Rows[i].Cells[2].Text = "女";            

                                 }       

                }

        }

  • 相关阅读:
    Java学习的第二十八天
    Java学习的第二十七天
    第五章 作用域闭包
    第4章提升
    第3章函数作用域和块级作用域
    Vant中List列表下拉加载更多
    获取当前时间
    js比较两个时间的大小
    边框引起页面抖动
    计算两个数的百分比,保留两位小数
  • 原文地址:https://www.cnblogs.com/linsu/p/2827298.html
Copyright © 2020-2023  润新知