/// <summary> /// 执行多行选择控件的回发事件。 /// </summary> private void DoSelectAllRows() { if (_gv == null) { return; } Control ctrl = null; if (_gv.HeaderRow != null) { ctrl = _gv.HeaderRow.FindControl(this.SelectAllRowsControlID); } else if (_gv.FooterRow != null) { ctrl = _gv.FooterRow.FindControl(this.SelectAllRowsControlID); } if (ctrl != null) { // 如果当前是多行选择模式,则需要根据多行选择控件的 Checked 状态去改变单行选择控件的 Checked 状态。 if (this.RowSelectMode == GridViewRowSelectMode.Multiple) { foreach (GridViewRow aRow in _gv.Rows) { Control ctrlSelectSingleRow = aRow.FindControl(this.SelectSingleRowControlID); if (ctrlSelectSingleRow != null) { Utility.SetControlPropertyValue <bool>(ctrlSelectSingleRow, Constants.ControlProperties.CheckedPropertyName, Utility.GetControlPropertyValue <bool>(ctrl, Constants.ControlProperties.CheckedPropertyName, false)); // 如果当前引发事件的单行选择控件的 Checked 状态为 true,则应用行选中样式,否则去除样式。 if (_selectedRowStyle != null) { if (Utility.GetControlPropertyValue <bool>(ctrlSelectSingleRow, Constants.ControlProperties.CheckedPropertyName, false)) { aRow.ApplyStyle(_selectedRowStyle); } else { aRow.ControlStyle.Reset(); } } } } } // 引发所有行选中的事件。 GridViewRowSelectedIndexChangedEventArgs arg = new GridViewRowSelectedIndexChangedEventArgs(_gv, null, ctrl, Utility.GetControlPropertyValue <bool>(ctrl, Constants.ControlProperties.CheckedPropertyName, false), null); this.OnGridViewRowSelectedIndexChanged(arg); } }
/// <summary> /// 执行行选择变化的事件,该执行方法只在 IsRowSelectControlPostBack 为 true 的时候执行。 /// </summary> private void DoGridViewRowSelectedIndexChanged() { if (_gv == null || string.IsNullOrEmpty(this.Page.Request.Params["__LASTSELECTED_" + this.UniqueID])) { return; } Control ctrl = null; if (_gv.HeaderRow != null) { ctrl = _gv.HeaderRow.FindControl(this.SelectAllRowsControlID); } else if (_gv.FooterRow != null) { ctrl = _gv.FooterRow.FindControl(this.SelectAllRowsControlID); } if (ctrl != null) { if (this.Page.Request.Params["__LASTSELECTED_" + this.UniqueID] == ctrl.UniqueID || this.Page.Request.Params["__LASTSELECTED_" + this.UniqueID] == ctrl.ClientID) { // 引发所有行选中的事件。 GridViewRowSelectedIndexChangedEventArgs arg = new GridViewRowSelectedIndexChangedEventArgs(_gv, null, ctrl, Utility.GetControlPropertyValue <bool>(ctrl, Constants.ControlProperties.CheckedPropertyName, false), null); this.OnGridViewRowSelectedIndexChanged(arg); // 引发完毕之后将不再引发单行选中实践 return; } } foreach (GridViewRow aRow in _gv.Rows) { ctrl = aRow.FindControl(this.SelectSingleRowControlID); if (ctrl != null) { if (this.Page.Request.Params["__LASTSELECTED_" + this.UniqueID] == ctrl.UniqueID || this.Page.Request.Params["__LASTSELECTED_" + this.UniqueID] == ctrl.ClientID) { // 引发单行选择控件的行选择索引改变事件。 GridViewRowSelectedIndexChangedEventArgs arg = new GridViewRowSelectedIndexChangedEventArgs(_gv, aRow, ctrl, Utility.GetControlPropertyValue <bool>(ctrl, Constants.ControlProperties.CheckedPropertyName, false), aRow.RowIndex < _gv.DataKeys.Count ? _gv.DataKeys[aRow.RowIndex] : null); this.OnGridViewRowSelectedIndexChanged(arg); } } } }
/// <summary> /// GridView行选择索引改变之后发生。 /// </summary> protected virtual void OnGridViewRowSelectedIndexChanged(GridViewRowSelectedIndexChangedEventArgs e) { GridViewRowSelectedIndexChangedEventHandler handler = (GridViewRowSelectedIndexChangedEventHandler) Events[EventGridViewRowSelectedIndexChangedKey]; if (handler != null) { handler(this, e); } }
/// <summary> /// 执行单行选择控件的回发事件。 /// </summary> private void DoSelectSingleRow() { if (_gv == null) { return; } // 获得引发该执行动作的目标控件UniqueID。 string __eventTarget = Utility.GetRequestParameterValue(this.Page, Constants.RequestParameter.EventTarget); if (string.IsNullOrEmpty(__eventTarget)) { return; } if (!string.IsNullOrEmpty(this.SelectSingleRowControlID)) { foreach (GridViewRow aRow in _gv.Rows) { Control ctrl = aRow.FindControl(this.SelectSingleRowControlID); if (ctrl != null) { // 如果为当前的行。 if (__eventTarget.Equals(ctrl.UniqueID)) { // 引发单行选择控件的行选择索引改变事件。 GridViewRowSelectedIndexChangedEventArgs arg = new GridViewRowSelectedIndexChangedEventArgs(_gv, aRow, ctrl, Utility.GetControlPropertyValue <bool>(ctrl, Constants.ControlProperties.CheckedPropertyName, false), aRow.RowIndex < _gv.DataKeys.Count ? _gv.DataKeys[aRow.RowIndex] : null); this.OnGridViewRowSelectedIndexChanged(arg); } else { if (this.RowSelectMode == GridViewRowSelectMode.Single) { // 设置非当前行为非选中状态。 Utility.SetControlPropertyValue <bool>(ctrl, Constants.ControlProperties.CheckedPropertyName, false); // 重新设置样式。 aRow.ControlStyle.Reset(); } } // 根据当前行的选中状态设置选中样式或取消选中样式。 if (_selectedRowStyle != null) { if (Utility.GetControlPropertyValue <bool>(ctrl, Constants.ControlProperties.CheckedPropertyName, false)) { aRow.ApplyStyle(_selectedRowStyle); } else { aRow.ControlStyle.Reset(); } } } } } }