302班

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

          GridView 常用方法合集

          Posted on 2007-07-21 17:04 停留的風 閱讀(1617) 評論(0)  編輯  收藏 所屬分類: .NET技巧特輯
          在項目中主要用了gridview來顯示數據,其中的方法都是常用的。
          1、利用sqldatasource動態邦定數據,其中sqldatasource1是托放在頁面的sqldatasource控件,將數據邦定到gridview1上面
              void gridviewband() {
                      
          try
                      {
                          SqlDataSource1.ConnectionString 
          = ConfigurationSettings.AppSettings[ "sqlconn"];
                          SqlDataSource1.SelectCommand 
          = squerysql;
                          GridView1.DataSource 
          = SqlDataSource1;
                          GridView1.DataBind();
                          
          //看有沒有數據 如果沒有數據 提示錯誤  lblresult的text的內容是錯誤信息  
                           int count = GridView1.Rows.Count;
                          
          if (count == 0) { lblresult.Visible  = true; }
                          
          else { lblresult.Visible  = false; }
                      }
                      
          catch   (SqlException ex5)
                      {
                          
          //提示錯誤信息
                      }
                      
          finally {  
                      }
              }

          2、選擇一行,獲取關鍵字,然后傳到其他頁面
              protected void GridView1_SelectedIndexChanged( object sender, EventArgs e)
              {
          //選擇某一行
                  Session[ "selectkey"= GridView1.SelectedDataKey.Value;
                  Response.Redirect(
          "~/employee/employeedetail.aspx");
              }

          3、獲取編輯行的關鍵字
                   protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
              {
                  
          //編輯修改
                  Session[ "editkey"= GridView1.DataKeys[e.NewEditIndex].Value.ToString();
                  Response.Redirect(
          "~/employee/employeeedit.aspx");
              }

          4、刪除一行,獲取關鍵字。這個地方還是用rowdeleting 事件。
              protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
              {
                 
                  String idno111 
          = GridView1.DataKeys[e.RowIndex].Value.ToString();
                  SqlDataSource1.DeleteCommand 
          = "delete from empinfo where empnum=' " + idno111 + "'" 
                      
          + "delete from employee where empnum=' " + idno111 + "'";
                  
          //刪除行
                   if (e.RowIndex >= 0)
                  {

                      String connectstr 
          = ConfigurationSettings.AppSettings[ "sqlconn"];
                      SqlConnection sqlconn 
          = new SqlConnection(connectstr);
                      sqlconn.Open();
                      SqlCommand comm 
          = new SqlCommand(SqlDataSource1.DeleteCommand, sqlconn);
                      comm.ExecuteNonQuery();
                      sqlconn.Close();
                  }
              }

          5、刪除前提示關鍵字,不僅僅是提示“確認刪除嗎?” 而是提示“確認刪除關鍵字是***的數據嗎?”

          首先在aspx文件腳本里面要設置 重要看的是gridview的標簽里面要有
           DatakeyNames="關鍵字" 同時 CommandArgument='<%# Eval("departno") %>'  這兩處要填寫
          <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
                  CellPadding
          ="4" DataKeyNames="departno" DataSourceID="SqlDataSource1" ForeColor="#333333"
                  GridLines
          ="None" OnDataBound="CustomersGridView_DataBound" OnRowDeleting="GridView1_RowDeleting"
                  OnRowEditing
          ="GridView1_RowEditing" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
                  PageSize
          ="15" Width="800px" OnRowDataBound="GridView1_RowDataBound">
                  
          <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                  
          <Columns>
                      
          <asp:BoundField DataField="departno" HeaderText="部門編號" SortExpression="departno" />
                      
          <asp:BoundField DataField="father" HeaderText="父級部門" SortExpression="father" />
                      
          <asp:BoundField DataField="departname" HeaderText="部門名稱" SortExpression="departname" />
                      
          <asp:BoundField DataField="person" HeaderText="聯系人" SortExpression="person" />
                      
          <asp:BoundField DataField="tel" HeaderText="固定電話" SortExpression="tel" />
                      
          <asp:BoundField DataField="mobiletel" HeaderText="手機" SortExpression="mobiletel" />
                      
                      
          <asp:CommandField ButtonType="Image" HeaderText="查看" SelectImageUrl="~/icons/look.gif"
                          SelectText
          ="查看" ShowSelectButton="True">
                          
          <HeaderStyle HorizontalAlign="Center" />
                          
          <ItemStyle HorizontalAlign="Center" />
                      
          </asp:CommandField>
                      
          <asp:CommandField ButtonType="Image" EditImageUrl="~/icons/edit.gif" EditText="編輯"
                          HeaderText
          ="編輯" ShowEditButton="True">
                          
          <HeaderStyle HorizontalAlign="Center" />
                          
          <ItemStyle HorizontalAlign="Center" />
                      
          </asp:CommandField>
                      
          <asp:TemplateField HeaderText="刪除" ShowHeader="False">
                          
          <ItemStyle HorizontalAlign="Center" />
                          
          <HeaderStyle HorizontalAlign="Center" />
                          
          <ItemTemplate>
                              
          <asp:ImageButton ID="ImageButton1" runat="server" CausesValidation="False" CommandName="Delete"
                                  ImageUrl
          ="~/icons/delete.gif" CommandArgument='<%# Eval("departno") %>' Text="刪除" />
                          
          </ItemTemplate>
                      
          </asp:TemplateField>
                  
          </Columns>
                  
                  
          <PagerTemplate>
                      
          <table width="100%">
                          
          <tr>
                              
          <td width="70%">
                                  
          <asp:Label ID="MessageLabel" runat="server" Font-Size="Larger" ForeColor="Black"
                                      Text
          ="頁碼:"></asp:Label>
                                  
          <asp:DropDownList ID="PageDropDownList" runat="server" AutoPostBack="true" OnSelectedIndexChanged="PageDropDownList_SelectedIndexChanged" >
                                  
          </asp:DropDownList>
                                  
          <asp:LinkButton ID="linkBtnFirst" runat="server" CommandArgument="First" CommandName="Page"
                                      ForeColor
          ="Black">首頁</asp:LinkButton>
                                  
          <asp:LinkButton ID="linkBtnPrev" runat="server" CommandArgument="Prev" CommandName="Page"
                                      ForeColor
          ="Black">上一頁</asp:LinkButton>
                                  
          <asp:LinkButton ID="linkBtnNext" runat="server" CommandArgument="Next" CommandName="Page"
                                      ForeColor
          ="Black">下一頁</asp:LinkButton>
                                  
          <asp:LinkButton ID="linkBtnLast" runat="server" CommandArgument="Last" CommandName="Page"
                                      ForeColor
          ="Black">末頁</asp:LinkButton>
                              
          </td>
                              
          <td align="right" width="70%">
                                  
          <asp:Label ID="CurrentPageLabel" runat="server" ForeColor="black"></asp:Label>
                              
          </td>
                          
          </tr>
                      
          </table>
                  
          </PagerTemplate>
                  
                  
          <RowStyle BackColor="#EFF3FB" />
                  
          <EditRowStyle BackColor="#2461BF" />
                  
          <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                  
          <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                  
          <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                  
          <AlternatingRowStyle BackColor="White" />
              
          </asp:GridView>

          然后在cs頁面里填入事件:

              //刪除某一行的時候要提示關鍵字  不僅是提示確認刪除,departno是關鍵字
              protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
              {
                  
          if (e.Row.RowType == DataControlRowType.DataRow)
                  {
                      ImageButton l 
          = (ImageButton)e.Row.FindControl( "ImageButton1" );
                      l.Attributes.Add(
          "onclick""javascript: return " +
                      
          "confirm('確認要刪除部門編號為   " +
                      DataBinder.Eval(e.Row.DataItem, 
          "departno"+ " 的部門嗎?" + "')");
                  }
              }
          }


          這樣刪除的時候就能提示關鍵字了。

          6、顯示頁碼
          顯示頁碼要在aspx代碼里填入幾行數據,上面的代碼已經有了 重新寫一下:
          代碼放在 </Columns>下面

                   <PagerTemplate>
                      
          <table width="100%">
                          
          <tr>
                              
          <td width="70%">
                                  
          <asp:Label ID="MessageLabel" runat="server" Font-Size="Larger" ForeColor="Black"
                                      Text
          ="頁碼:"></asp:Label>
                                  
          <asp:DropDownList ID="PageDropDownList" runat="server" AutoPostBack="true" OnSelectedIndexChanged="PageDropDownList_SelectedIndexChanged" >
                                  
          </asp:DropDownList>
                                  
          <asp:LinkButton ID="linkBtnFirst" runat="server" CommandArgument="First" CommandName="Page"
                                      ForeColor
          ="Black">首頁</asp:LinkButton>
                                  
          <asp:LinkButton ID="linkBtnPrev" runat="server" CommandArgument="Prev" CommandName="Page"
                                      ForeColor
          ="Black">上一頁</asp:LinkButton>
                                  
          <asp:LinkButton ID="linkBtnNext" runat="server" CommandArgument="Next" CommandName="Page"
                                      ForeColor
          ="Black">下一頁</asp:LinkButton>
                                  
          <asp:LinkButton ID="linkBtnLast" runat="server" CommandArgument="Last" CommandName="Page"
                                      ForeColor
          ="Black">末頁</asp:LinkButton>
                              
          </td>
                              
          <td align="right" width="70%">
                                  
          <asp:Label ID="CurrentPageLabel" runat="server" ForeColor="black"></asp:Label>
                              
          </td>
                          
          </tr>
                      
          </table>
                  
          </PagerTemplate>

          然后在gridview的標簽里面填入

          OnDataBound="CustomersGridView_DataBound"

          位置看 5 的代碼就可以了。
          然后再 cs代碼區加入如下代碼

          //顯示頁碼

              
          protected void PageDropDownList_SelectedIndexChanged(Object sender, EventArgs e)
              {
                  GridViewRow pagerRow 
          = GridView1.BottomPagerRow;
                  DropDownList pageList 
          = (DropDownList)pagerRow.Cells[ 0].FindControl("PageDropDownList");
                  GridView1.PageIndex 
          = pageList.SelectedIndex;
              }

              
          protected void CustomersGridView_DataBound(Object sender, EventArgs e)
              {
                  
          try
                  {
                      GridViewRow pagerRow 
          = GridView1.BottomPagerRow;

                      LinkButton linkBtnFirst 
          = (LinkButton)pagerRow.Cells[ 0].FindControl("linkBtnFirst");
                      LinkButton linkBtnPrev 
          = (LinkButton)pagerRow.Cells[ 0].FindControl("linkBtnPrev");
                      LinkButton linkBtnNext 
          = (LinkButton)pagerRow.Cells[ 0].FindControl("linkBtnNext");
                      LinkButton linkBtnLast 
          = (LinkButton)pagerRow.Cells[ 0].FindControl("linkBtnLast");

                      
          if (GridView1.PageIndex  == 0)
                      {
                          linkBtnFirst.Enabled 
          = false;
                          linkBtnPrev.Enabled 
          = false;
                      }
                      
          else if (GridView1.PageIndex  == GridView1.PageCount - 1)
                      {
                          linkBtnLast.Enabled 
          = false;
                          linkBtnNext.Enabled 
          = false;
                      }
                      
          else if (GridView1.PageCount  <= 0)
                      {
                          linkBtnFirst.Enabled 
          = false;
                          linkBtnPrev.Enabled 
          = false;
                          linkBtnNext.Enabled 
          = false;
                          linkBtnLast.Enabled 
          = false;
                      }
                      DropDownList pageList 
          = (DropDownList)pagerRow.Cells[ 0].FindControl("PageDropDownList");
                      Label pageLabel 
          = (Label)pagerRow.Cells[0].FindControl("CurrentPageLabel");

                      
          if (pageList != null)
                      {
                          
          for (int i = 0; i < GridView1.PageCount; i ++)
                          {
                              
          int pageNumber = i + 1;
                              ListItem item 
          = new ListItem(pageNumber.ToString()  + "/" + GridView1.PageCount.ToString(), pageNumber.ToString());
                              
          if (i == GridView1.PageIndex)
                              {
                                  item.Selected 
          = true;
                              }
                              pageList.Items.Add(item);
                          }
                      }
                      
          if (pageLabel != null)
                      {
                          
          int currentPage = GridView1.PageIndex + 1;
                          pageLabel.Text 
          = "當前頁: " + currentPage.ToString()  +
                            
          " / " + GridView1.PageCount.ToString();
                      }
                  }
                  
          catch 
                  {
                      Response.Write(
          "現在還沒有該部門的記錄!!");
                  }
              }

          如果發生異常,是應為沒有數據。這時候可以在catch里面提示沒有數據。這樣就可以綁定頁碼了。
          但是這樣做的條件是沒有把綁定數據放在 (!ispostback)里面,如果放在里面了,就會出問題
          因為頁面的代碼實際上是控制顯示第幾頁。但是綁定數據放在了!ispostback里面了,所以不能重新綁定。
          這樣的情況,要寫那個綁定的方法。
          然后在cs文件里寫下面的代碼:

              protected void PageDropDownList_SelectedIndexChanged(Object sender, EventArgs e)
              {
                  GridViewRow pagerRow 
          = GridView1.BottomPagerRow;
                  DropDownList pageList 
          = (DropDownList)pagerRow.Cells[ 0].FindControl("PageDropDownList");
                  GridView1.PageIndex 
          = pageList.SelectedIndex;
                  gridviewband();
          ///重新綁定數據,這是特殊的地方
              }

              
          protected void CustomersGridView_DataBound(Object sender, EventArgs e)
              {
                  
          try
                  {
                      GridViewRow pagerRow 
          = GridView1.BottomPagerRow;

                      LinkButton linkBtnFirst 
          = (LinkButton)pagerRow.Cells[ 0].FindControl("linkBtnFirst");
                      LinkButton linkBtnPrev 
          = (LinkButton)pagerRow.Cells[ 0].FindControl("linkBtnPrev");
                      LinkButton linkBtnNext 
          = (LinkButton)pagerRow.Cells[ 0].FindControl("linkBtnNext");
                      LinkButton linkBtnLast 
          = (LinkButton)pagerRow.Cells[ 0].FindControl("linkBtnLast");

                      
          if (GridView1.PageIndex  == 0)
                      {
                          linkBtnFirst.Enabled 
          = false;
                          linkBtnPrev.Enabled 
          = false;
                      }
                      
          else if (GridView1.PageIndex  == GridView1.PageCount - 1)
                      {
                          linkBtnLast.Enabled 
          = false;
                          linkBtnNext.Enabled 
          = false;
                      }
                      
          else if (GridView1.PageCount  <= 0)
                      {
                          linkBtnFirst.Enabled 
          = false;
                          linkBtnPrev.Enabled 
          = false;
                          linkBtnNext.Enabled 
          = false;
                          linkBtnLast.Enabled 
          = false;
                      }
                      DropDownList pageList 
          = (DropDownList)pagerRow.Cells[ 0].FindControl("PageDropDownList");
                      Label pageLabel 
          = (Label)pagerRow.Cells[0].FindControl("CurrentPageLabel");

                      
          if (pageList != null)
                      {
                          
          for (int i = 0; i < GridView1.PageCount; i ++)
                          {
                              
          int pageNumber = i + 1;
                              ListItem item 
          = new ListItem(pageNumber.ToString()  + "/" + GridView1.PageCount.ToString(), pageNumber.ToString());
                              
          if (i == GridView1.PageIndex)
                              {
                                  item.Selected 
          = true;
                              }
                              pageList.Items.Add(item);
                          }
                      }
                      
          if (pageLabel != null)
                      {
                          
          int currentPage = GridView1.PageIndex + 1;
                          pageLabel.Text 
          = "當前頁: " + currentPage.ToString()  +
                            
          " / " + GridView1.PageCount.ToString();
                      }
                  }
                  
          catch //(Exception ex5)
                  {
                      Response.Write(
          "現在還沒有該部門的記錄!!");
                  }
              }
                  
          protected void GridView1_PageIndexChanging( object sender, GridViewPageEventArgs e)
              {
                  
                  GridView1.PageIndex 
          = e.NewPageIndex;
                  gridviewband();
          //重新綁定數據
              }

          這樣就可以顯示了 如果按照前面的方法會提示沒有  pageindexchanging事件。

          主站蜘蛛池模板: 营口市| 平度市| 陵川县| 志丹县| 安龙县| 汉寿县| 锦州市| 苍南县| 布尔津县| 昌图县| 敦化市| 拜泉县| 井冈山市| 北碚区| 临沂市| 铜鼓县| 德惠市| 张家港市| 鄂伦春自治旗| 德令哈市| 平南县| 登封市| 沙田区| 资兴市| 屏东市| 衡阳市| 阜新市| 大埔县| 和硕县| 濮阳市| 玉门市| 洞头县| 泰和县| 乌兰县| 扎兰屯市| 乌兰浩特市| 武鸣县| 郁南县| 临夏县| 武川县| 杭锦后旗|