gridview使用技巧
1、CheckBox模板选择事件
//多选业务员事件 protected void cbSelect_CheckedChanged(object sender, EventArgs e) { string pNames=lblPersonNames.Text; personList = new List<string>(pNames.Split(new string[]{","},StringSplitOptions.RemoveEmptyEntries)); CheckBox cbSelect = (CheckBox)sender; DataControlFieldCell dcf = (DataControlFieldCell)cbSelect.Parent; //父对象Cell GridViewRow gr = (GridViewRow)dcf.Parent; //父对象row if (cbSelect.Checked) { personList.Add(gr.Cells[0].Text); } else { personList.Remove(gr.Cells[0].Text); } lblPersonNames.Text = string.Join(",", personList.ToArray()); }
2、行绑定事件
protected void gvPerson_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { string pNames = lblPersonNames.Text; personList = new List<string>(pNames.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries)); string personName=e.Row.Cells[0].Text; if (personList.Contains(personName)) { CheckBox cbSelect=(CheckBox)e.Row.FindControl("cbSelect"); cbSelect.Checked = true; } } }
附加:javascript数组扩展方法
Array.prototype.remove = function(val) { var index = this.indexOf(val); if (index > -1) { this.splice(index, 1); } }