GridView CheckBox当翻页时保存选中的项

  1 ContractedBlock.gif ExpandedBlockStart.gif    属性 保存全选的项 #region  属性 保存全选的项
  2InBlock.gif
  3InBlock.gif    protected ArrayList SelectedItems
  4ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
  5InBlock.gif        get
  6ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
  7InBlock.gif            if (ViewState["SelectedItems"== null)
  8ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
  9InBlock.gif                ViewState["SelectedItems"= new ArrayList();
 10ExpandedSubBlockEnd.gif            }

 11InBlock.gif
 12InBlock.gif            return ViewState["SelectedItems"as ArrayList;
 13ExpandedSubBlockEnd.gif        }

 14InBlock.gif        set
 15ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 16InBlock.gif            ViewState["SelectedItems"= value;
 17ExpandedSubBlockEnd.gif        }

 18ExpandedSubBlockEnd.gif    }

 19ExpandedSubBlockStart.gifContractedSubBlock.gif  /**//// <summary>
 20InBlock.gif    /// 页面加载
 21InBlock.gif    /// </summary>
 22InBlock.gif    /// <param name="sender"></param>
 23ExpandedSubBlockEnd.gif    /// <param name="e"></param>

 24InBlock.gif    protected void Page_Load(object sender, EventArgs e)
 25ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 26InBlock.gif  SaveSelectedItems();
 27InBlock.gif        if (!IsPostBack)
 28ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 29InBlock.gif            if (myGridView.Attributes["SortExpression"== null)
 30ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 31InBlock.gif                myGridView.Attributes["SortExpression"= "DAID";
 32InBlock.gif                myGridView.Attributes["SortDirection"= "asc";
 33ExpandedSubBlockEnd.gif            }

 34InBlock.gif            BindDynamicAccountFlowInfo();
 35ExpandedSubBlockEnd.gif        }

 36ExpandedSubBlockEnd.gif  }

 37ExpandedSubBlockStart.gifContractedSubBlock.gif  /**//// <summary>
 38InBlock.gif    /// 保存页面选中的项
 39ExpandedSubBlockEnd.gif    /// </summary>

 40InBlock.gif    private void SaveSelectedItems()
 41ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 42InBlock.gif        for (int i = 0; i < myGridView.Rows.Count; i++)
 43ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 44InBlock.gif            CheckBox tempCheckBox = myGridView.Rows[i].FindControl("chkSelect"as CheckBox;
 45InBlock.gif            string id = myGridView.Rows[i].Cells[0].Text;
 46InBlock.gif
 47InBlock.gif            if (SelectedItems.Contains(id) && !tempCheckBox.Checked)
 48ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 49InBlock.gif                SelectedItems.Remove(id);
 50ExpandedSubBlockEnd.gif            }

 51InBlock.gif            if (!SelectedItems.Contains(id) && tempCheckBox.Checked)
 52ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 53InBlock.gif                SelectedItems.Add(id);
 54ExpandedSubBlockEnd.gif            }

 55ExpandedSubBlockEnd.gif        }

 56ExpandedSubBlockEnd.gif    }

 57ExpandedSubBlockStart.gifContractedSubBlock.gif /**//// <summary>
 58InBlock.gif    /// 行绑定
 59InBlock.gif    /// </summary>
 60InBlock.gif    /// <param name="sender"></param>
 61ExpandedSubBlockEnd.gif    /// <param name="e"></param>

 62InBlock.gif    protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
 63ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 64InBlock.gif  if (e.Row.RowType == DataControlRowType.DataRow)
 65ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 66InBlock.gif            if (e.Row.RowIndex > -1 && SelectedItems.Count > 0)
 67ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 68InBlock.gif                CheckBox tempCheckBox = e.Row.FindControl("chkSelect"as CheckBox;
 69InBlock.gif                string id = e.Row.Cells[0].Text;
 70InBlock.gif                if (SelectedItems.Contains(id))
 71ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 72InBlock.gif                    tempCheckBox.Checked = true;
 73ExpandedSubBlockEnd.gif                }

 74InBlock.gif                else
 75ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 76InBlock.gif                    tempCheckBox.Checked = false;
 77ExpandedSubBlockEnd.gif                }

 78ExpandedSubBlockEnd.gif            }

 79ExpandedSubBlockEnd.gif        }

 80InBlock.gif
 81ExpandedSubBlockEnd.gif    }

 82ExpandedSubBlockStart.gifContractedSubBlock.gif /**//// <summary>
 83InBlock.gif    /// 删除按钮
 84InBlock.gif    /// </summary>
 85InBlock.gif    /// <param name="sender"></param>
 86ExpandedSubBlockEnd.gif    /// <param name="e"></param>

 87InBlock.gif    protected void btnDelAll_ServerClick(object sender, EventArgs e)
 88ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 89InBlock.gif        string sID = "";//选中删除所有行的id
 90InBlock.gif
 91InBlock.gif        foreach (string tempString in SelectedItems)
 92ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 93InBlock.gif            sID += tempString + ",";
 94ExpandedSubBlockEnd.gif        }

 95InBlock.gif        if (SelectedItems.Count < 1)
 96ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 97InBlock.gif            ClientScript.RegisterStartupScript(this.GetType(), """<script>alert('你没有选择!!!')</script>");
 98ExpandedSubBlockEnd.gif        }

 99InBlock.gif        else
100ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
101InBlock.gif            daDB.DelDynamicAccountFlowinfo(sID.Substring(0, sID.Length - 1));
102ExpandedSubBlockEnd.gif       }

103InBlock.gif        if (Cache["dataSource"!= null)
104InBlock.gif            Cache.Remove("dataSource");//***先清空原有页面缓存
105InBlock.gif
106InBlock.gif        BindDynamicAccountFlowInfo();
107ExpandedSubBlockEnd.gif    }

108InBlock.gif
下面是删除操作代码(DB):
 1 None.gif   public   bool  DelDynamicAccountFlowinfo( string  sID)
 2 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
 3InBlock.gif        string sqlstr=string.Format("delete from DynamicAccountFlow where DAFID IN({0})", sID);
 4InBlock.gif  conn = Myconn.SqlConnection;
 5InBlock.gif        SqlCommand mycommand = new SqlCommand(sqlstr, conn);
 6InBlock.gif        mycommand.CommandType = CommandType.Text;
 7InBlock.gif        mycommand.CommandText = sqlstr;
 8InBlock.gif        conn.Open();
 9InBlock.gif        mycommand.ExecuteNonQuery();
10InBlock.gif        conn.Close();    
11InBlock.gif    return true;
12ExpandedBlockEnd.gif    }

转载于:https://www.cnblogs.com/niuyy/archive/2007/08/06/844553.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值