302班

          java突擊隊
          posts - 151, comments - 74, trackbacks - 0, articles - 14
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          Datalist自定義分頁【A】

          Posted on 2007-07-21 17:31 停留的風 閱讀(561) 評論(0)  編輯  收藏 所屬分類: .NET技巧特輯
          前臺代碼:
          <%@ Page Language="C#" MasterPageFile="~/admin/header_footer.master" AutoEventWireup="true" CodeFile="editPrice.aspx.cs" Inherits="admin_editPrice" Title="Untitled Page" %>
          <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
              <table border="0" cellpadding="0" cellspacing="0" align="center">
                  <tr>
                      <td style="text-align: center;">
                          價格信息</td>
                  </tr>
                  <tr>
                      <td style="text-align: center; height: 139px;">
                          <asp:DataList ID="DataList1" runat="server" OnCancelCommand="DataList1_CancelCommand" OnEditCommand="DataList1_EditCommand" OnUpdateCommand="DataList1_UpdateCommand" OnDeleteCommand="DataList1_DeleteCommand">
                              <ItemTemplate>
                                  <table border="0" cellpadding="0" cellspacing="0">
                                      <tr>
                                          <td style="width: 100px; height: 16px">
                                              <asp:Label ID="Label1" runat="server" Text='<%# Bind("type_Id") %>'></asp:Label></td>
                                          <td style="width: 100px; height: 16px">
                                              <asp:Label ID="Label2" runat="server" Text='<%# Bind("type") %>'></asp:Label></td>
                                          <td style="width: 100px; height: 16px">
                                              <asp:Label ID="Label3" runat="server" Text='<%# Bind("price") %>'></asp:Label></td>
                                          <td style="width: 100px; height: 16px">
                                              <asp:Button ID="btnEdit" runat="server" Text="修改" OnClientClick="return confirm('確認要編輯嗎?');"  CommandName="Edit" CommandArgument=' <%# Eval( "price") %>   ' /></td>
                                          <td style="width: 100px; height: 16px">
                                              <asp:Button ID="Button2" runat="server" Text="刪除"   OnClientClick="return confirm('確認要刪除嗎?');"  CommandArgument='<%#Eval("type_Id") %>' CommandName="Delete" /></td>
                                      </tr>
                                  </table>
                              </ItemTemplate>
                              <HeaderTemplate>
                                  <table border="0" cellpadding="0" cellspacing="0">
                                      <tr>
                                          <td style="width: 100px; height: 12px;">
                                              編號</td>
                                          <td style="width: 100px; height: 12px;">
                                              類型</td>
                                          <td style="width: 100px; height: 12px;">
                                              價格</td>
                                          <td style="width: 100px; height: 12px;">
                                              編輯</td>
                                          <td style="width: 100px; height: 12px">
                                              刪除</td>
                                      </tr>
                                  </table>
                              </HeaderTemplate>
                              <EditItemTemplate>
                                  <table border="0" cellpadding="0" cellspacing="0">
                                      <tr>
                                          <td style="width: 100px; height: 16px">
                                              <asp:Label ID="Label1" runat="server" Text='<%# Bind("type_Id") %>'></asp:Label></td>
                                          <td style="width: 100px; height: 16px">
                                              <asp:Label ID="Label2" runat="server" Text='<%# Bind("type") %>'></asp:Label></td>
                                          <td style="width: 100px; height: 16px">
                                              <asp:TextBox ID="tbPrice" runat="server" Text='<%# Bind("price") %>' Width="50px"></asp:TextBox></td>
                                          <td style="width: 100px; height: 16px">
                                              <asp:Button ID="btnUpdate" runat="server" Text="更新" CommandArgument='<%# Eval("type_Id") %>   ' CommandName="Update" /></td>
                                          <td style="width: 100px; height: 16px">
                                              <asp:Button ID="btnCancel" runat="server" Text="取消" CommandName="Cancel" /></td>
                                      </tr>
                                  </table>
                              </EditItemTemplate>
                          </asp:DataList>
                          <asp:Label ID="lbCount" runat="server" Text="lbCount"></asp:Label>
                          <asp:Label ID="lbTotalPage" runat="server" Text="lbTotalPage"></asp:Label>&nbsp;
                          <asp:Label ID="lbCurPage" runat="server" Text="lbCurPage"></asp:Label>&nbsp;<asp:HyperLink
                              ID="hlnkHead" runat="server">首頁</asp:HyperLink>
                          <asp:HyperLink ID="hlnkPrev" runat="server">上一頁</asp:HyperLink>
                          <asp:HyperLink ID="hlnkNext" runat="server">下一頁</asp:HyperLink>
                          <asp:HyperLink ID="hlnkEnd" runat="server">尾頁</asp:HyperLink>
                          </td>
                  </tr>
                  <tr>
                      <td style="text-align: left">
                          在這里你也可以添加相關的類型,并對其進行價格錄入。</td>
                  </tr>
                  <tr>
                      <td style="text-align: center">
                          類型添加:<asp:TextBox ID="tbType" runat="server"></asp:TextBox>
                         &nbsp;單價:<asp:TextBox ID="tbPrice" runat="server"></asp:TextBox>
                          <asp:Button ID="btnAdd" runat="server" OnClick="btnAdd_Click" Text="添加" Width="58px" /></td>
                  </tr>
                  <tr>
                      <td>
                      </td>
                  </tr>
              </table> 
          </asp:Content>  
          后臺綁定:
            private void dListBindData()
              {
                  string strCon = ConfigurationManager.AppSettings["SqlConnectionString"].ToString();
                  SqlConnection con = new SqlConnection(strCon);
                  SqlDataAdapter da = new SqlDataAdapter("select * from type", con);
                  DataSet ds = new DataSet();
                  try
                  {
                      con.Open();
                      //填充數據
                      da.Fill(ds, "type");
                      // 創建分頁類
                      PagedDataSource objPage = new PagedDataSource();           
                      objPage.DataSource = ds.Tables["type"].DefaultView;
                      //設置總的查詢結果
                      lbCount.Text = "總共有" + ds.Tables["type"].Rows.Count.ToString() + "條記錄";           
                      //設置可以分頁以及每頁的行數
                      objPage.AllowPaging = true;
                      objPage.PageSize = 6;
                      //總頁數
                      lbTotalPage.Text = "總頁數:" + objPage.PageCount.ToString() + "頁";     
                      //定義變量來保存當前頁
                      int CurPage;
                      //判斷是否具有頁面跳轉的請求
                      if (Request.QueryString["Page"] != null)
                      {
                          CurPage = Convert.ToInt32(Request.QueryString["Page"]);
                      }
                      else
                      {
                          CurPage = 1;
                      }
                      //設置當前頁的索引
                      objPage.CurrentPageIndex = CurPage - 1;
                      lbCurPage.Text = "當前頁 : 第" + CurPage.ToString() + "頁";
                      //如果不是首頁
                      if (!objPage.IsFirstPage)
                      {
                          //定義上一頁超鏈接的url為: 當前執行頁面的虛擬路徑,并傳遞下一頁面的索引值
                          hlnkHead.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(1);
                          hlnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1); 
                      }
                      //如果不是最后一頁
                      if (!objPage.IsLastPage)
                      {
                          //定義“下一頁”超鏈接url為:當前執行頁面的虛擬路徑,并傳遞下一頁面的索引值
                          hlnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1);
                          hlnkEnd.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + objPage.PageCount.ToString();
                      }
                      DataList1.DataSource = objPage;
                      DataList1.DataBind();           
                      con.Close();
                  }
                  catch(Exception error)
                  {
                      Response.Write(error.ToString());
                  }
              }
          主站蜘蛛池模板: 庆安县| 潼关县| 银川市| 日喀则市| 乃东县| 固安县| 鹿邑县| 德钦县| 措勤县| 沅陵县| 益阳市| 当涂县| 淮安市| 保定市| 会东县| 光山县| 循化| 明水县| 巴马| 句容市| 林西县| 乐至县| 临泉县| 新野县| 边坝县| 普洱| 吉水县| 盈江县| 吉木萨尔县| 澎湖县| 甘南县| 尤溪县| 台山市| 阳春市| 澄迈县| 登封市| 江山市| 秦皇岛市| 乌苏市| 堆龙德庆县| 正宁县|