<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    302班

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

    GridView 常用方法合集

    Posted on 2007-07-21 17:04 停留的風 閱讀(1615) 評論(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事件。

    主站蜘蛛池模板: 亚洲AV无码男人的天堂 | 95老司机免费福利| 亚洲色成人WWW永久网站| 美女黄频a美女大全免费皮| 日韩成全视频观看免费观看高清| 亚洲入口无毒网址你懂的| 精品久久久久成人码免费动漫| 亚洲电影唐人社一区二区| 国产高清不卡免费在线| 亚洲AV综合色区无码二区偷拍 | 国外成人免费高清激情视频| 一区二区亚洲精品精华液| 午夜高清免费在线观看| 精品视频免费在线| 久久亚洲色一区二区三区| 成在人线av无码免费高潮喷水| 午夜影视日本亚洲欧洲精品一区| 96免费精品视频在线观看| 亚洲首页国产精品丝袜| 男女交性永久免费视频播放| 日韩色日韩视频亚洲网站| 亚洲一区二区三区自拍公司| 8x网站免费入口在线观看| 亚洲午夜成人精品无码色欲| 免费人成在线观看视频播放| 水蜜桃视频在线观看免费播放高清| 亚洲激情中文字幕| 成人毛片免费观看视频在线| 九九九精品视频免费| 国产亚洲一区二区三区在线| 国产成人精品免费视| 亚洲av综合av一区二区三区| 亚洲自偷自偷在线制服| 91精品免费久久久久久久久| 久久亚洲精品无码av| 亚洲av无码片在线播放| 好大好深好猛好爽视频免费| 中文字幕视频免费在线观看 | 国产亚洲精品欧洲在线观看| 亚洲成a人片在线观看无码| 免费毛片在线播放|