seanly

 

gridview使用心得

前台

 

GridView 代码
<asp:GridView ID="GridView" runat="server" AllowPaging="True" PageSize="2" onpageindexchanging="GridView_PageIndexChanging" 
         onrowdeleting
="GridView_RowDeleting" onrowediting="GridView_RowEditing" 
        AutoGenerateColumns
="False" onrowupdating="GridView_RowUpdating" 
        OnRowCancelingEdit
="GridView_RowCancelingEdit"
          
>
            
<Columns>
            
                
<asp:BoundField DataField="id" ReadOnly="true" HeaderText="ID" />
            
                
<asp:TemplateField HeaderText="pro_name">
                    
<ItemTemplate>
                        
<%#Eval("pro_name")%>
                    
</ItemTemplate>
                    
<EditItemTemplate>
                        
<asp:TextBox ID="new_pro_name" Text='<%#Eval("pro_name") %>' runat="server"></asp:TextBox>
                    
</EditItemTemplate>
                
</asp:TemplateField>
                
                
<asp:TemplateField HeaderText="title">
                    
<ItemTemplate>
                        
<%#Eval("title")%>
                    
</ItemTemplate>
                    
<EditItemTemplate>
                        
<asp:TextBox ID="new_title" runat="server" Text='<%#Eval("title") %>'></asp:TextBox>
                    
</EditItemTemplate>
                
</asp:TemplateField>
                
                
<asp:TemplateField HeaderText="pub_date">
                    
<EditItemTemplate>
                        
<asp:TextBox ID="new_pub_date" Text='<%#Eval("pub_date") %>' runat="server"></asp:TextBox>
                    
</EditItemTemplate>
                    
<ItemTemplate>
                        
<%#Eval("pub_date")%>
                    
</ItemTemplate>
                
</asp:TemplateField>
                
<asp:CommandField ShowDeleteButton="true" ShowEditButton="true" HeaderText="操作" />
         
</Columns>
         
<PagerTemplate>
                当前第:
                
//((GridView)Container.NamingContainer)就是为了得到当前的控件
                <asp:Label ID="LabelCurrentPage" runat="server" Text="<%# ((GridView)Container.NamingContainer).PageIndex + 1 %>"></asp:Label>
                页
/共:
                
//得到分页页面的总数
                <asp:Label ID="LabelPageCount" runat="server" Text="<%# ((GridView)Container.NamingContainer).PageCount %>"></asp:Label>
                页
                 
//如果该分页是首分页,那么该连接就不会显示了.同时对应了自带识别的命令参数CommandArgument
                <asp:LinkButton ID="LinkButtonFirstPage" runat="server" CommandArgument="First" CommandName="Page"
                    Visible
='<%#((GridView)Container.NamingContainer).PageIndex != 0 %>'>首页</asp:LinkButton>
                
<asp:LinkButton ID="LinkButtonPreviousPage" runat="server" CommandArgument="Prev"
                    CommandName
="Page" Visible='<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>'>上一页</asp:LinkButton>
               
//如果该分页是尾页,那么该连接就不会显示了
                <asp:LinkButton ID="LinkButtonNextPage" runat="server" CommandArgument="Next" CommandName="Page"
                    Visible
='<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>'>下一页</asp:LinkButton>
                
<asp:LinkButton ID="LinkButtonLastPage" runat="server" CommandArgument="Last" CommandName="Page"
                    Visible
='<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>'>尾页</asp:LinkButton>
                转到第
                
<asp:TextBox ID="txtNewPageIndex" runat="server" Width="20px" Text='<%#((GridView)Container.Parent.Parent).PageIndex+1 %>'/>
                
//这里将CommandArgument即使点击该按钮e.newIndex 值为-3
                <asp:LinkButton ID="btnGo" runat="server" CausesValidation="False" 
                    CommandArgument
="-2" CommandName="Page" Text="GO" />

         
</PagerTemplate>
        
</asp:GridView>

 

 

后台

 

后台代码
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace GridView应用
{
    
public partial class _Default : System.Web.UI.Page
    {
        
protected void Page_Load(object sender, EventArgs e)
        {
            
if (!Page.IsPostBack)
            {
                GridViewDataBind();
            }

        }
        
protected void GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView theGridView 
= sender as GridView;
            
int newPageIndex = 0;

            
if (e.NewPageIndex == -3)//点击了GO按钮
            {
                TextBox txtNewPageIndex 
= null;

                
//GridView较DataGrid提供了更多的API,获取分页块可以使用BottomPagerRow 或者TopPagerRow,当然还增加了HeaderRow和FooterRow
                GridViewRow pagerow = theGridView.BottomPagerRow;//获取最地行
                if (pagerow != null)
                {
                    
//得到text控件
                    txtNewPageIndex = pagerow.FindControl("txtNewPageIndex"as TextBox;
                }
                
if (txtNewPageIndex != null)
                {
                    
//HttpContext.Current.Response.Write("<script>alert('" + txtNewPageIndex.Text + "')</script>");
                    newPageIndex = int.Parse(txtNewPageIndex.Text) - 1;
                }
            }
            
else//点击了其他按钮
            {
                newPageIndex 
= e.NewPageIndex;
            }

            
//防止新索引溢出

            newPageIndex 
= newPageIndex < 0 ? 0 : newPageIndex;
            newPageIndex 
= newPageIndex > theGridView.PageCount ? theGridView.PageCount - 1 : newPageIndex;

            
//获得新值
            this.GridView.PageIndex = newPageIndex;
            
//重新绑定
            GridViewDataBind();
        }

        
protected void GridView_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView.EditIndex 
= e.NewEditIndex;
            GridViewDataBind();
        }

        
protected void GridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            
int delectint = int.Parse(GridView.Rows[e.RowIndex].Cells[0].Text);
            
//HttpContext.Current.Response.Write("<script>alert('" + GridView.Rows[e.RowIndex].Cells[0].Text + "')</script>");
            string delectStr = "delete from product where id=" + delectint;
            DelectGridView(delectStr);
            GridViewDataBind(); 
        }
        
protected void GridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView.EditIndex 
= -1;
            GridViewDataBind();
        }

        
protected void GridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            
int ID = int.Parse(GridView.Rows[e.RowIndex].Cells[0].Text);
            
string new_pro_name = ((TextBox)GridView.Rows[e.RowIndex].FindControl("new_pro_name")).Text;
            
string new_title = ((TextBox)GridView.Rows[e.RowIndex].FindControl("new_title")).Text;
            var new_pub_date 
=Convert.ToDateTime(((TextBox)GridView.Rows[e.RowIndex].FindControl("new_pub_date")).Text);
            
string strsql = "update product set pro_name='" + new_pro_name + "',title='" + new_title + "',pub_date='" + new_pub_date + "' where id=" + ID;
            DelectGridView(strsql);
            GridView.EditIndex 
= -1;
            GridViewDataBind();
            
//HttpContext.Current.Response.Write("<script>alert('" + ID + "')</script>");
        }

        
protected void DelectGridView(string delectstr)
        {
            
string strConnecttion = "user id=sa;password=;initial catalog=PrettyWeb;Server=(local);Connect Timeout=30";
            SqlConnection conn 
= new SqlConnection(strConnecttion);
            conn.Open();
            
string strsql = delectstr;
            SqlCommand cmd 
= new SqlCommand(strsql, conn);
            cmd.ExecuteNonQuery();

            conn.Close();
        }
        
protected void GridViewDataBind()
        {
            
string strConnection = "user id=sa;password=;";
            strConnection 
+= "initial catalog=PrettyWeb;Server=(local);";
            strConnection 
+= "Connect Timeout=30";
            SqlConnection conn 
= new SqlConnection(strConnection);
            conn.Open();

            
string strsql = "select * from product";
            SqlDataAdapter mycom 
= new SqlDataAdapter();
            mycom.SelectCommand 
= new SqlCommand(strsql, conn);
            SqlCommandBuilder myCB 
= new SqlCommandBuilder(mycom);

            DataSet ds 
= new DataSet();
            mycom.Fill(ds, 
"产品信息");
            
this.GridView.DataSource = ds.Tables["产品信息"];
            
this.GridView.DataBind();

            conn.Close();
        }

    }
}

 

 

 

 

posted on 2009-12-25 16:34  seanly  阅读(217)  评论(0编辑  收藏  举报

导航