中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

asp.net中GridView控件如何使用

發布時間:2021-07-16 13:56:30 來源:億速云 閱讀:189 作者:Leah 欄目:開發技術

本篇文章給大家分享的是有關asp.net中GridView控件如何使用,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

前臺.aspx

<asp:Label ID="tplb" runat="server" Text="總頁數:"></asp:Label>
<asp:Label ID="lblPageCount" runat="server" Text=""></asp:Label>
<asp:Label ID="curLabel" runat="server" Text="當前頁:"></asp:Label>
<asp:Label ID="lblPage" Text="1" runat="server"></asp:Label>&nbsp; 
<asp:LinkButton ID="lblFirstButton" runat="server" OnClick="lblFirstButton_Click" >|&lt;</asp:LinkButton>&nbsp; 
<asp:LinkButton ID="lblPreButton" runat="server" OnClick="lblPreButton_Click" >&lt;</asp:LinkButton>&nbsp; 
<asp:LinkButton ID="lblNextButton" runat="server" OnClick="lblNextButton_Click" >&gt;</asp:LinkButton>&nbsp;
<asp:LinkButton ID="lblLastButton" runat="server" OnClick="lblLastButton_Click" >&gt;|</asp:LinkButton>&nbsp; 
<asp:DropDownList ID="ddlPage" runat="server" Width="40px" AutoPostBack="True" 
      OnSelectedIndexChanged="ddlPage_SelectedIndexChanged"> 
      <asp:ListItem>10</asp:ListItem> 
        <asp:ListItem>15</asp:ListItem> 
      <asp:ListItem>20</asp:ListItem> 
      <asp:ListItem>30</asp:ListItem> 
</asp:DropDownList> 
<asp:Label ID="PageSizeLabel" runat="server" Text="條/頁"></asp:Label>  


后臺  

復制代碼 代碼如下:


#region分頁
protected void BindFollowExamInfoGridView(int PersonID)
  {
    int currentpage = Convert.ToInt32(lblPage.Text);
    DataTable dt = new DataTable();
    dt = feibf.GetByPersonIDFollowExamInfo(PersonID);  //查詢指定人的隨訪信息記錄
    if (dt.Rows.Count > 0)
    {
      FollowExamInfoGridView.DataSource = dt;
      FollowExamInfoGridView.DataBind();
      PagedDataSource ps = new PagedDataSource();
      ps.DataSource = dt.DefaultView;
      ps.AllowPaging = true;
      ps.PageSize = Convert.ToInt32(ddlPage.SelectedValue);
      lblPageCount.Text = ps.PageCount.ToString();
      this.lblPreButton.Enabled = true;
      this.lblNextButton.Enabled = true;
      ps.CurrentPageIndex = currentpage - 1;
      if (currentpage == 1)
      {
        this.lblPreButton.Enabled = false;
        this.lblFirstButton.Enabled = false;
      }
      else
      {
        this.lblPreButton.Enabled = true;
        this.lblFirstButton.Enabled = true;
      }
      if (currentpage == ps.PageCount)
      {
        this.lblNextButton.Enabled = false;
        this.lblLastButton.Enabled = false;
      }
      else
      {
        this.lblNextButton.Enabled = true;
        this.lblLastButton.Enabled = true;
      }
      FollowExamInfoGridView.DataSource = ps;
      FollowExamInfoGridView.DataBind();
    }
    
  }
  protected void lblPreButton_Click(object sender, EventArgs e)
  {
    this.lblPage.Text = Convert.ToString(Convert.ToUInt32(lblPage.Text) - 1);
    BindFollowExamInfoGridView(Convert.ToInt32(Request.QueryString["PersonID"]));
  }
  protected void lblNextButton_Click(object sender, EventArgs e)
  {
    this.lblPage.Text = Convert.ToString(Convert.ToUInt32(lblPage.Text) + 1);
    BindFollowExamInfoGridView(Convert.ToInt32(Request.QueryString["PersonID"]));
  }
  protected void lblFirstButton_Click(object sender, EventArgs e)
  {
    this.lblPage.Text = "1";
    BindFollowExamInfoGridView(Convert.ToInt32(Request.QueryString["PersonID"]));
  }
  protected void lblLastButton_Click(object sender, EventArgs e)
  {
    this.lblPage.Text = lblPageCount.Text;
    BindFollowExamInfoGridView(Convert.ToInt32(Request.QueryString["PersonID"]));
  }
  protected void ddlPage_SelectedIndexChanged(object sender, EventArgs e)
  {
    lblPage.Text = "1";
    BindFollowExamInfoGridView(Convert.ToInt32(Request.QueryString["PersonID"]));
  }
#endregion 

排序
Allowsort = "true"
sortExpression = "ID"
DataView dv = SortBindGrid(dt);
#region排序
  protected void FollowExamInfoGridView_Sorting(object sender, GridViewSortEventArgs e)
  {
    ViewState["sortexpression"] = e.SortExpression;
    if (ViewState["sortdirection"] == null)
    {
      ViewState["sortdirection"] = "asc";
    }
    else
    {
      if (ViewState["sortdirection"].ToString() == "asc")
      {
        ViewState["sortdirection"] = "desc";
      }
      else
      {
        ViewState["sortdirection"] = "asc";
      }
    }
   
    BindFollowExamInfoGridView(Convert.ToInt32(HiddenPersonID.Value));
  }
  public DataView SortBindGrid(DataTable table)
  {
    if (table != null)
    {
      DataView dv = table.DefaultView;
      if (ViewState["sortexpression"] != null && ViewState["sortdirection"] != null)
      {
        dv.Sort = ViewState["sortexpression"].ToString() + " " + ViewState["sortdirection"].ToString();
      }
      return dv;
    }
    else
    {
      return null;
    }
  }
  #endregion 

=======自帶分頁
  #region自帶分頁

protected void FollowExamInfoGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
  {
    FollowExamInfoGridView.PageIndex = e.NewPageIndex;
    BindFollowExamInfoGridView(Convert.ToInt32(HiddenPersonID.Value));
  }
#endregion

  選中Grid View 的實現

復制代碼 代碼如下:


  #region實現選中行
   <SelectedRowStyle BackColor="AliceBlue" ForeColor="Gray" />
   <asp:CommandField ShowSelectButton="True"/>
if (e.Row.RowType == DataControlRowType.DataRow)
  {
      e.Row.Attributes.Add("onclick", "this.cells[0].childNodes[0].click()");
}
protected void GridViewRegiment_SelectedIndexChanged(object sender, EventArgs e)
{
    GridViewRow row = GridViewRegiment.SelectedRow;
    int RegimentID = Convert.ToInt32(row.Cells[1].Text);
    Response.Redirect("UpdateRegimentation.aspx?RegimentID=" + RegimentID);
}
#endregion


顯示顏色和刪除

復制代碼 代碼如下:


  protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  {
    //int i;
    //for (i = 0; i < GridViewRegiment.Rows.Count; i++)
    //{
      if (e.Row.RowType == DataControlRowType.DataRow)
      {
        //當有編輯列時,避免出錯,要加的RowState判斷
        if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
        {
          ((ImageButton)e.Row.Cells[2].FindControl("IBtndelete")).Attributes.Add("onclick", "javascript:return confirm('你確認要刪除:"" + e.Row.Cells[0].Text + ""嗎?')");
        }
        e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#00A9FF'");
        e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#E6F5FA'");
      }
    //}


GridView空的處理
  1 顯示無表頭的空紀錄,EmptyDataText="沒有記錄"
  2 顯示表頭的空紀錄

復制代碼 代碼如下:


DataTable dt = new DataTable();
  dt = feibf.GetByPersonIDFollowExamInfo(PersonID);  //查詢指定人的隨訪信息記錄
    DataView dv = SortBindGrid(dt);
    if (dt.Rows.Count > 0)
    {
      FollowExamInfoGridView.DataSource = dv;
      FollowExamInfoGridView.DataBind();
    }
    else
    {
      //添加新行顯示表頭
      dt.Rows.Add(dt.NewRow());
      FollowExamInfoGridView.DataSource = dt;
      FollowExamInfoGridView.DataBind();
      //處理新行
      int columnCount = FollowExamInfoGridView.Rows[0].Cells.Count;
      //清除掉該空行的全部單元格
      FollowExamInfoGridView.Rows[0].Cells.Clear();
      //新建單元格對象
      FollowExamInfoGridView.Rows[0].Cells.Add(new TableCell());
      //合并單元格
      FollowExamInfoGridView.Rows[0].Cells[0].ColumnSpan = columnCount;
      //設置單元格提示內容
      FollowExamInfoGridView.Rows[0].Cells[0].Style.Value = "text-align:center";
      FollowExamInfoGridView.Rows[0].Cells[0].Text = "此人無隨訪信息";
    } 


GridView 的導出
EnableEventValidation="false"

復制代碼 代碼如下:


#region導出
 public override void VerifyRenderingInServerForm(Control control)
  {
  }
  protected void BtnPrint_Click(object sender, EventArgs e)
  {
    Response.Clear();
    Response.Buffer = true;
    Response.Charset = "GB2312";
    Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.xls");
    // 如果設置為GetEncoding("GB2312");導出的文件將會出現亂碼!!!
    Response.ContentEncoding = System.Text.Encoding.UTF7;
    Response.ContentType = "application/ms-excel";//設置輸出文件類型為excel文件。
    System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
    this.AfficheGV.RenderControl(oHtmlTextWriter);
    Response.Output.Write(oStringWriter.ToString());
    Response.Flush();
    Response.End();
  }
#endregion 

  ToolTip GridView詳細信息的顯示
  前臺
<script type="text/javascript" >
  function Tooltip(cella,cellb)
  {
    document.getElementById("dc").innerText = "詳細信息:"+cellb;
    document.getElementById("id").innerText = "ID:"+cella;
    x= event.clientX+document.body.scrollLeft;
    y=event.clientY+document.body.scrollTop+20;
    toolTipLayer.style.display="inline";
    toolTipLayer.style.left=x;
    toolTipLayer.style.top=y;
  }
</script>
<div id="toolTipLayer"  >
  <table>
  <tr><td>Affiche</td></tr>
  <tr><td id ="dc"></td></tr>
  <tr><td id ="id"> </td></tr>
  </table>
</div> 

后臺

復制代碼 代碼如下:


protected void AfficheGV_RowDataBound(object sender, GridViewRowEventArgs e)
  {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
      if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
      {
1 e.Row.Attributes.Add("onmouseover", "Tooltip('" +e.Row.Cells[0].Text.ToString()+ "','"+e.Row.Cells[1].Text.ToString()+"')");
2 e.Row.Attributes.Add("onmouseover","javascript:Tooltip('e.Row.Cells[0].Text');");
3 e.Row.Attributes.Add("onmouseover", "Tooltip('e.Row.Cells[0].Text')");
      } }

#region自帶編輯
  protected void GVAffiche_RowEditing(object sender, GridViewEditEventArgs e)
  {
    GVAffiche.EditIndex = e.NewEditIndex;
    BindGVAffiche();
  }
  protected void GVAffiche_RowDeleting(object sender, GridViewDeleteEventArgs e)
  {
    GVAffiche.EditIndex = -1;
    MyAffiche.DelAfficeBF( Convert.ToInt32(GVAffiche.DataKeys[e.RowIndex].Value.ToString()));
    BindGVAffiche();
  }
  protected void GVAffiche_RowUpdating(object sender, GridViewUpdateEventArgs e)
  {
    int id = Convert.ToInt32(((TextBox)(GVAffiche.Rows[e.RowIndex].Cells[0].Controls[0])).Text.ToString().Trim());
    string dc = ((TextBox)(GVAffiche.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim();
    MyAffiche.UpdateAfficheBf(id,dc);
    GVAffiche.EditIndex = -1;
    BindGVAffiche();
  }
  protected void GVAffiche_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
  {
    GVAffiche.EditIndex = -1;
    BindGVAffiche();
  }
#endregion 

#region樣式的控制
  protected void GVAffiche_RowDataBound(object sender, GridViewRowEventArgs e)
  {
    //首先判斷是否是數據行
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
      //當有編輯列時,避免出錯,要加的RowState判斷
      if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
      {
        ((Button)e.Row.Cells[7].FindControl("btnDel")).Attributes.Add("onclick","javascript:return confirm('你確認刪除:"" + e.Row.Cells[1].Text + ""')");
        //當鼠標停留時更改背景色
        e.Row.Attributes.Add("onmouseover", "color=this.style.backgroundColor;this.style.backgroundColor='#00A9FF'");
        //當鼠標移開時還原背景色
        e.Row.Attributes.Add("onmouseout","this.style.backgroundColor=color");
        GVAffiche.Attributes.Add("style", "word-break:keep-all;word-wrap:normal");
        //GVAffiche.Attributes.Add("style", "word-break:break-all;word-wrap:break-word");
        if (e.Row.Cells[1].Text == "444")
        {
          e.Row.Cells[1].BackColor = System.Drawing.Color.Red;
        }
      }
    }
  }
  #endregion 

以上就是asp.net中GridView控件如何使用,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

长武县| 石家庄市| 铅山县| 望谟县| 海阳市| 中阳县| 华池县| 云和县| 海晏县| 丹江口市| 邯郸市| 武鸣县| 东海县| 巫溪县| 凤台县| 平南县| 丹寨县| 西宁市| 乌拉特后旗| 阳春市| 定安县| 如皋市| 新昌县| 宣恩县| 犍为县| 澄江县| 民勤县| 花莲市| 永靖县| 酒泉市| 大新县| 东山县| 邳州市| 南木林县| 祁东县| 镶黄旗| 营口市| 含山县| 高台县| 张家界市| 琼结县|