• 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 = "女";            

                                 }       

                }

        }

  • 相关阅读:
    Linux系统学习之网络管理
    EntityFramework 6.1.2-beta2
    微软开源的30个基础设施项目-C#
    vs2015 Android SDK
    批量删除空的文件夹
    统一者管理员指南(Unifier Administration Guide)中文
    XXX全球 IP 地址库
    mssql2008R2 RCU-6083:ALTER database FWC SET READ_COMMITTED_SNAPSHOT ON
    ORA-01843: 无效的月份,执行sql语句更改为美国语言后仍然失败的解决办法
    Win7如何安装IIS来浏览ASP网站
  • 原文地址:https://www.cnblogs.com/linsu/p/2827298.html
Copyright © 2020-2023  润新知