欢迎光临!

GridView事件大全

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using IOIOT.DataControl;

public partial class Admin_LinksOperation : System.Web.UI.Page

{

       DCAdmin dcadmin=new DCAdmin();

       DCPublic dcpub=new DCPublic();

       protected void Page_Load(object sender, EventArgs e)

       {

           if (!IsPostBack)

           {

               GVBind();

           }

       }

      //绑定

       private void GVBind()

       {

           DataTable dt = dcpub.SelectAllTable("Links", "", "", "CreateDate");

           GridView1.DataSource = dt.DefaultView;

           GridView1.DataBind();

       }

       //分页,需要设置AllowPaging="true"

       protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)

       {

           GridView1.PageIndex = e.NewPageIndex;

           GVBind();

       }

    

       protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)

       {

           GridView1.EditIndex = e.NewEditIndex;

           GVBind();

       }

      //取ID值需要设置DataKeyNames="LinkID"

       protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

       {

           int LID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);

           string LinkText = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("Elinktext")).Text;

           string LinkIMG = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("Elinkimg")).Text;

           string LinkURL = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("Elinkurl")).Text;

           try

           {

               dcadmin.Table_UPDATE("Links","LinkText='" + LinkText + "',LinkImg='" + LinkIMG + "',Address='" + LinkURL + "'", "LinkID=" + LID);

               PublichClass.MsgBox("更新成功", this.Page);

           }

           catch(Exception ex)

           {

               errmessage.Text="更新失败"+ex.Message;

           }

           GridView1.EditIndex = -1;

           GVBind();

       }

       protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)

       {

           GridView1.EditIndex = -1;

           GVBind();

       }

       protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)

       {

           int LID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);

           try

           {

               dcadmin.Table_DELETE("Links", "LinkID=" + LID);

               errmessage.Text = "删除成功";

               PublichClass.MsgBox("删除成功", this.Page);

               GVBind();

           }

           catch (Exception ex)

           {

               errmessage.Text = "删除失败" + ex.Message;

           }

       }

      //排序,需要设置AllowSorting="true",同时在项里需要设置 SortExpression="LinkID"

       protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)

       {

           if ((string)ViewState["Sort"] != e.SortExpression)

           {

               ViewState["Sort"] = e.SortExpression;

           }

           else

           {

               ViewState["Sort"] = e.SortExpression + " " + "DESC";

           }

           DataTable dt = dcpub.SelectAllTable("Links", "", "", "CreateDate");

           dt.DefaultView.Sort = ViewState["Sort"].ToString();

           GridView1.DataSource = dt.DefaultView;

           GridView1.DataBind();

       }

     //全选

       protected void AllLinkSelect_Click(object sender, EventArgs e)

       {

           CheckBox chkEbox;

           if (AllLinkSelect.Text == "全选")

           {

               foreach (GridViewRow gvrow in GridView1.Rows)

               {

                   chkEbox = (CheckBox)gvrow.FindControl("chbk");

                   chkEbox.Checked = true;

               }

               AllLinkSelect.Text = "取消";

           }

           else

           {

               foreach (GridViewRow gvrow in GridView1.Rows)

               {

                   chkEbox = (CheckBox)gvrow.FindControl("chbk");

                   chkEbox.Checked = false;

               }

               AllLinkSelect.Text = "全选";

           }

       }

      //删除选择项

       protected void LinkDel_Click(object sender, EventArgs e)

       {

           //删除其中选择项

           CheckBox chkbox;

           int LID;

           string DelStr = "";

           #region

           try

           {

               foreach (GridViewRow gridview in GridView1.Rows)

               {

                   chkbox = (CheckBox)gridview.FindControl("chbk");

                   if (chkbox.Checked == true)

                   {

                       LID = Convert.ToInt32(((HiddenField)gridview.FindControl("hfd_linkid")).Value);

                       DelStr += "LinkID=" + LID + " or ";

                       //dcadmin.Table_DELETE("News", "NewsID=" + NID);

                   }

               }

               if (DelStr.ToString().Trim().Length == 0)

               {

                   errmessage.Text = "没有选择任何项";

                   PublichClass.MsgBox("没有选择任何项", this.Page);

               }

               else

               {

                   DelStr = DelStr.Substring(0, DelStr.LastIndexOf("or"));

                   //删除选中的数据项

                   dcadmin.Table_DELETE("Links", DelStr);

                   //errmessage.Text = DelStr;

                   errmessage.Text = "选择项删除成功!";

                   PublichClass.MsgBox("选择项删除成功!", this.Page);

                   GVBind();

               }

           }

           catch (Exception ex)

           {

               errmessage.Text = "出错了!" + ex.Message;

           }

           AllLinkSelect.Text = "全选";

           #endregion

       }

       protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)

       {

           int TTID;

           if (e.CommandName.ToString() == "topstep")

           {

               //向上移,Sort+1

               if (e.CommandArgument != null && e.CommandArgument.ToString() != "")

               {

                   //errmessage.Text = e.CommandArgument.ToString();

                   TTID = Convert.ToInt32(GridView1.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);

                   int NowSort = Convert.ToInt32((dcpub.SelectAllTable("Links", "LinkID=" + TTID, "Sort", "")).Rows[0]["Sort"]);

                   int UpSort = NowSort + 1;

                   dcadmin.Table_UPDATE("Links", "Sort=" + UpSort, "LinkID=" + TTID);

                   DataTable dt = dcpub.SelectAllTable("Links", "", "", "Sort desc");

                   GridView1.DataSource = dt.DefaultView;

                   GridView1.DataBind();

               }

               else

                   //errmessage.Text = "没有参数";

                   errmessage.Text = "没有参数";

           }

           if (e.CommandName.ToString() == "bottomstep")

           {

               //向下移,Sort-1

               errmessage.Text = "22";

           }

       }

      //RowCommand事件需要添加CommandArgument

       protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)

       {

           LinkButton totop;

           LinkButton tobottom;

           if (e.Row.RowType == DataControlRowType.DataRow)

           {

               totop = (LinkButton)e.Row.FindControl("totop");

               tobottom = (LinkButton)e.Row.FindControl("tobottom");

               if (totop != null)

               {

                   if (totop.CommandName == "topstep")

                       totop.CommandArgument = e.Row.RowIndex.ToString();

                   if (tobottom.CommandName == "bottomstep")

                       tobottom.CommandArgument = e.Row.RowIndex.ToString();

               }

           }

       }

}

posted on 2008-09-03 15:01  花拉子米  阅读(441)  评论(0编辑  收藏  举报
版权所有