Asp.net GridView使用大全(分頁實現)_.Net教程

      編輯Tag賺U幣

      推薦:彈出窗口,點擊確定在刪除數據的實現方法
      彈出窗口,點擊確定在刪除數據的實現方法,需要的朋友可以參考一下

      GridView自帶的分頁功能實現:

      要實現GrdView分頁的功能
      操作如下:
      1、更改GrdView控件的AllowPaging屬性為true。
      2、更改GrdView控件的PageSize屬性為 任意數值(默認為10)
      3、更改GrdView控件的PageSetting->Mode為Numeric等(默認為Numeric)該屬性為分頁樣式。
      GridView屬性設置好了,從頁面上也能看到分頁樣式。

      現在開始實現分頁的功能:

      1、在<<asp:GridView ID=......>后添加,OnPageIndexChanging="GridView1_PageIndexChanging"
      2、在對應的aspx.cs中添加:
      protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
      {
      GridView1.PageIndex = e.NewPageIndex;
      InitPage(); //重新綁定GridView數據的函數
      }
      3、
      GridView1.PageIndex = e.NewPageIndex;
      完了之后再重新綁定一下GridView。

      GridView添加CheckBox列實現全選/全部取消功能

      首先GridView編輯模版,在模板上添加CheckBox控件,之后將新添加列字段轉換為TemplateFiled



      后臺代碼

      復制代碼 代碼如下:www.wf0088.com

      using System;
      using System.Data;
      using System.Configuration;
      using System.Web;
      using System.Web.Security;
      using System.Web.UI;
      using System.Web.UI.WebControls;
      using System.Web.UI.WebControls.WebParts;
      using System.Web.UI.HtmlControls;
      using System.Data.SqlClient;

      public partial class Default5 : System.Web.UI.Page
      {
      SqlConnection sqlcon;
      string strCon = "Data Source=(local);Database=北風貿易;Uid=sa;Pwd=sa";
      protected void Page_Load(object sender, EventArgs e)
      {
      if (!IsPostBack)
      {
      bind();
      }
      }
      protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
      {
      for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
      {
      CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
      if (CheckBox2.Checked == true)
      {
      cbox.Checked = true;
      }
      else
      {
      cbox.Checked = false;
      }
      }
      }
      protected void Button2_Click(object sender, EventArgs e)
      {
      sqlcon = new SqlConnection(strCon);
      SqlCommand sqlcom;
      for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
      {
      CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
      if (cbox.Checked == true)
      {

      string sqlstr = "delete from 飛狐工作室 where 身份證號碼='" + GridView1.DataKeys[i].Value + "'";
      sqlcom = new SqlCommand(sqlstr, sqlcon);
      sqlcon.Open();
      sqlcom.ExecuteNonQuery();
      sqlcon.Close();
      }
      }
      bind();
      }
      protected void Button1_Click(object sender, EventArgs e)
      {
      CheckBox2.Checked = false;
      for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
      {
      CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
      cbox.Checked = false;
      }
      }
      public void bind()
      {
      string sqlstr = "select top 5 * from 飛狐工作室";
      sqlcon = new SqlConnection(strCon);
      SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
      DataSet myds = new DataSet();
      sqlcon.Open();
      myda.Fill(myds, "tb_Member");
      GridView1.DataSource = myds;
      GridView1.DataKeyNames = new string[] { "身份證號碼" };
      GridView1.DataBind();
      sqlcon.Close();
      }
      }


      前臺主要代碼:
      復制代碼 代碼如下:www.wf0088.com

      <asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False"
      CellPadding="3" Font-Size="9pt" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px">
      <FooterStyle BackColor="White" ForeColor="#000066" />
      <Columns>
      <asp:TemplateField>
      <ItemTemplate>
      <asp:CheckBox ID="CheckBox1" runat="server" />
      </ItemTemplate>
      </asp:TemplateField>
      <asp:BoundField DataField="身份證號碼" HeaderText="用戶ID" SortExpression="身份證號碼" />
      <asp:BoundField DataField="姓名" HeaderText="用戶姓名" SortExpression="姓名"/>

      <asp:BoundField DataField="家庭住址" HeaderText="家庭住址" SortExpression="家庭住址"/>

      </Columns>
      <RowStyle ForeColor="#000066" />
      <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
      <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
      <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
      </asp:GridView>
      <asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="True" Font-Size="9pt" OnCheckedChanged="CheckBox2_CheckedChanged"
      Text="全選" />
      <asp:Button ID="Button1" runat="server" Font-Size="9pt" Text="取消" onClick="Button1_Click" />
      <asp:Button ID="Button2" runat="server" Font-Size="9pt" Text="刪除" onClick="Button2_Click" />

      分享:注冊頁實現激活郵箱驗證(asp.net c#)
      在填寫注冊提交后,大一點的網站會有郵箱激活驗證這一步,本文也是實現了一下,感興趣的朋友可以參考下哈,希望可以幫助到你

      來源:模板無憂//所屬分類:.Net教程/更新時間:2013-04-27
      相關.Net教程