datalist,gridview,repeater空间使用比较之girdview

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
        OnRowDeleting="GridView1_RowDeleting"
        OnPageIndexChanging="GridView1_PageIndexChanging"
        PageSize="4" AllowPaging="True" AllowSorting="True"
         OnRowCancelingEdit="GridView1_RowCancelingEdit"
          OnRowEditing="GridView1_RowEditing"
           OnRowUpdating="GridView1_RowUpdating"
           OnSorting="GridView1_Sorting" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound" CellPadding="4" GridLines="None" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" ForeColor="#333333">
          <Columns>
               <asp:BoundField DataField="id" SortExpression="id" HeaderText="编号" />
               <asp:BoundField DataField="description" SortExpression="description" HeaderText="描述" />
               <asp:BoundField DataField="add_time" SortExpression="add_time" HeaderText="添加时间" />
               <asp:CommandField HeaderText="编辑" ShowEditButton="True"></asp:CommandField>
               <asp:CommandField ShowDeleteButton="True" HeaderText="删除" ></asp:CommandField>
              <asp:ButtonField ButtonType="Button" CommandName="add" HeaderText="按钮列" Text="按钮" />
            </Columns>
            <FooterStyle BackColor="#1C5E55" ForeColor="White" Font-Bold="True" />
            <RowStyle BackColor="#E3EAEB" />
            <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
            <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
            <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#7C6F57" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>

protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            bind();
        }
    }
    private void bind()
    {

        this.GridView1.DataSource = clotheslibrary.SqlData.GetTable("select id,description,add_time from images where luxury=1");
        this.GridView1.DataKeyNames = new string[] { "id" };
        this.GridView1.DataBind();
    }
    private void SortGridView(string sortExpression, string direction)
    {

        DataView dv = clotheslibrary.SqlData.GetTable("select id,description,add_time from images where luxury=1").DefaultView;
        dv.Sort = sortExpression + direction;
        this.GridView1.DataSource = dv;                       //将DataView绑定到GridView上
        this.GridView1.DataBind();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = Convert.ToInt32(this.GridView1.DataKeys[e.RowIndex].Value);
        bool count = clotheslibrary.Image.Delete_Image(id);
        GridView1.EditIndex = -1;
        bind();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridView1.PageIndex = e.NewPageIndex;
        bind();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        this.GridView1.EditIndex = e.NewEditIndex;
        bind();

    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
        string name = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.Trim().ToString();
        bool count = clotheslibrary.Image.Update_Image(id, name);
        GridView1.EditIndex = -1;
        bind();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        this.GridView1.EditIndex = -1;
        bind();
    }
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        string sortExpression = e.SortExpression;
        if (GridViewSortDirection == SortDirection.Ascending)  //设置排序方向
        {
            GridViewSortDirection = SortDirection.Descending;
            SortGridView(sortExpression, " DESC");
        }
        else
        {
            GridViewSortDirection = SortDirection.Ascending;
            SortGridView(sortExpression, " ASC");
        }
    }
    public SortDirection GridViewSortDirection
    {
        get
        {
            if (ViewState["sortDirection"] == null)
                ViewState["sortDirection"] = SortDirection.Ascending;
            return (SortDirection)ViewState["sortDirection"];
        }
        set { ViewState["sortDirection"] = value; }
    }


    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
      
        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[0].Visible = false;
        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("OnMouseOut", "this.style.backgroundColor='White';");
             e.Row.Attributes.Add("OnMouseOver", "this.style.backgroundColor='#6699FF';");
             e.Row.Attributes.Add("OnClick", "ClickEvent('" + e.Row.Cells[0].Text.ToString() + "')");
        }
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Add")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            Response.Write(index.ToString());
        
        }

    }

posted on 2008-07-03 12:59  小顾问  阅读(675)  评论(0)    收藏  举报