zoukankan      html  css  js  c++  java
  • 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 = "女";            

                                 }       

                }

        }

  • 相关阅读:
    转载:AAC编解码概述
    转载:ADTS header
    wcf寄宿在iis上的跨域访问问题【不止是添加跨域文件】
    转 http 分析工具
    时间管理1
    关于silverlight和Wcf分布式部署注意问题(收藏夹)
    c#修改xml文件
    关于在线编辑的异常
    创业文摘5--从程序员转向企业家的10个建议
    silverlight 后台代码生成gridview
  • 原文地址:https://www.cnblogs.com/linsu/p/2827298.html
Copyright © 2011-2022 走看看