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

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

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

    302班

    java突擊隊(duì)
    posts - 151, comments - 74, trackbacks - 0, articles - 14
      BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

    GridView 常用方法合集

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

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

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

    4、刪除一行,獲取關(guān)鍵字。這個地方還是用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、刪除前提示關(guān)鍵字,不僅僅是提示“確認(rèn)刪除嗎?” 而是提示“確認(rèn)刪除關(guān)鍵字是***的數(shù)據(jù)嗎?”

    首先在aspx文件腳本里面要設(shè)置 重要看的是gridview的標(biāo)簽里面要有
     DatakeyNames="關(guān)鍵字" 同時 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="聯(lián)系人" SortExpression="person" />
                
    <asp:BoundField DataField="tel" HeaderText="固定電話" SortExpression="tel" />
                
    <asp:BoundField DataField="mobiletel" HeaderText="手機(jī)" 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頁面里填入事件:

        //刪除某一行的時候要提示關(guān)鍵字  不僅是提示確認(rèn)刪除,departno是關(guān)鍵字
        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('確認(rèn)要刪除部門編號為   " +
                DataBinder.Eval(e.Row.DataItem, 
    "departno"+ " 的部門嗎?" + "')");
            }
        }
    }


    這樣刪除的時候就能提示關(guān)鍵字了。

    6、顯示頁碼
    顯示頁碼要在aspx代碼里填入幾行數(shù)據(jù),上面的代碼已經(jīng)有了 重新寫一下:
    代碼放在 </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的標(biāo)簽里面填入

    OnDataBound="CustomersGridView_DataBound"

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

    //顯示頁碼

        
    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 
    = "當(dāng)前頁: " + currentPage.ToString()  +
                      
    " / " + GridView1.PageCount.ToString();
                }
            }
            
    catch 
            {
                Response.Write(
    "現(xiàn)在還沒有該部門的記錄!!");
            }
        }

    如果發(fā)生異常,是應(yīng)為沒有數(shù)據(jù)。這時候可以在catch里面提示沒有數(shù)據(jù)。這樣就可以綁定頁碼了。
    但是這樣做的條件是沒有把綁定數(shù)據(jù)放在 (!ispostback)里面,如果放在里面了,就會出問題
    因?yàn)轫撁娴拇a實(shí)際上是控制顯示第幾頁。但是綁定數(shù)據(jù)放在了!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();
    ///重新綁定數(shù)據(jù),這是特殊的地方
        }

        
    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 
    = "當(dāng)前頁: " + currentPage.ToString()  +
                      
    " / " + GridView1.PageCount.ToString();
                }
            }
            
    catch //(Exception ex5)
            {
                Response.Write(
    "現(xiàn)在還沒有該部門的記錄!!");
            }
        }
            
    protected void GridView1_PageIndexChanging( object sender, GridViewPageEventArgs e)
        {
            
            GridView1.PageIndex 
    = e.NewPageIndex;
            gridviewband();
    //重新綁定數(shù)據(jù)
        }

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

    主站蜘蛛池模板: 成人五级毛片免费播放| 四虎影视精品永久免费| 涩涩色中文综合亚洲| 精品国产麻豆免费网站| 成人精品视频99在线观看免费| 日韩精品亚洲人成在线观看| 精品国产麻豆免费网站| 可以免费观看的国产视频| 亚洲依依成人亚洲社区| 亚洲精品无码成人AAA片| 毛片高清视频在线看免费观看| 一区二区三区在线免费观看视频| 亚洲久本草在线中文字幕| 免费人成在线观看网站品爱网日本| 中文字幕乱理片免费完整的| 亚洲AV成人无码天堂| 亚洲日韩精品无码一区二区三区| 999国内精品永久免费观看| 国产大片免费天天看| 亚洲av无码电影网| 亚洲国产精品成人久久| 国产一级淫片a免费播放口之| 88av免费观看| 特级毛片在线大全免费播放| 亚洲日产2021三区| 亚洲熟妇丰满多毛XXXX| 国产成人免费a在线资源| 日韩av无码久久精品免费| 一区二区三区在线免费| 中文日韩亚洲欧美制服| 亚洲综合在线成人一区| 亚洲色偷偷综合亚洲AVYP| 女人18毛片水真多免费看| 最近中文字幕mv免费高清视频8 | 美女免费视频一区二区| 亚洲精品视频久久| 亚洲成人在线网站| 亚洲精品成人片在线观看精品字幕 | 在线看片免费人成视频久网下载| 亚洲av无码一区二区三区天堂| 亚洲欧洲精品久久|