asp.net DataGrid GridView 表格之选中行与获取选中行数据
一、GridView 表格之選中行
asp.net選中行的功能最初以為只能通過屬性中AllowGenerateSelectButton(運行時是否自動生成選擇按鈕)來實現(xiàn),需要點擊生成的選擇按鈕來操作,但這樣使用并是很方便。
經(jīng)尋找找到了改進辦法如下效果
鼠標經(jīng)過時背景色會改變,選中后可獲取響應(yīng)行的數(shù)據(jù)
實現(xiàn)方法如下:
首先前臺設(shè)計屬性框中事件綁定RowDataBound(在對時局進行了綁定后激發(fā))事件
后臺代碼如下:
/// <summary>/// 在對數(shù)據(jù)進行了綁定后激發(fā)/// 主要實現(xiàn)鼠標點擊時選中該行/// </summary>/// <param name="sender"></param>/// <param name="e"></param>protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e){#region 方法0 存在bug 暫未改進 供參考//e.Row.Attributes["style"] = "cursor:hand";//PostBackOptions myPostBackOptions = new PostBackOptions(this);//myPostBackOptions.AutoPostBack = false;//myPostBackOptions.PerformValidation = false;//myPostBackOptions.RequiresJavaScriptProtocol = true; //加入javascript:頭//String evt = Page.ClientScript.GetPostBackClientHyperlink(sender as GridView, "Select$" + e.Row.RowIndex.ToString());//e.Row.Attributes.Add("onclick", evt);#endregion#region 方法1//if (e.Row.RowType == DataControlRowType.DataRow)//{// e.Row.Attributes.Add("onClick", "__doPostBack('" + GridView1.UniqueID + "','Select$" + e.Row.RowIndex + "');");//此處為兩個“_”//}#endregion#region 方法2int i;for (i = 0; i <= GridView1.Rows.Count; i++){//首先判斷是否是數(shù)據(jù)行if (e.Row.RowType == DataControlRowType.DataRow){//當(dāng)鼠標停留時更改背景色e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#00A9FF'");//當(dāng)鼠標移開時還原背景色e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");//單擊行的任意列會自動選中此行e.Row.Attributes.Add("onclick", "__doPostBack('GridView1','Select$" + e.Row.RowIndex + "')");}}#endregion?二、獲取選中行數(shù)據(jù)
選中某行后獲取數(shù)據(jù)
在屬性框中事件選項中選擇設(shè)置SelectedIndexChanged(?在GridView中選擇行時,在該行選擇完成后激發(fā))事件選項
后臺代碼如下
/// <summary>/// 選擇某行時在最左側(cè)更新顯示數(shù)據(jù)詳細/// 在DataGriew選擇行時,在該選擇操作完成后激發(fā)/// </summary>/// <param name="sender"></param>/// <param name="e"></param>protected void GridView1_SelectedIndexChanged(object sender, EventArgs e){if (GridView1.SelectedIndex >= 0){ClearTreeNodeChecked(TreeView1.Nodes);txtName.Text = GridView1.SelectedRow.Cells[0].Text;txtPhone.Text = GridView1.SelectedRow.Cells[1].Text;txtSendTime.Text = GridView1.SelectedRow.Cells[2].Text;GetUserNodes();}}?
如果單獨設(shè)置了修改或刪除按鈕,選中某行后,點擊這些按鈕來處理數(shù)據(jù),可通過定義一些頁面屬性來保存當(dāng)前行選中的列數(shù)據(jù),
在每次選中行改變SelectedIndexChanged事件中更改這些定義的表示行列數(shù)據(jù)的屬性,然后利用這些列數(shù)據(jù)進行操作
首先定義頁面屬性
/// <summary>/// 選中行的代碼列/// </summary>private static string Code = "";/// <summary>/// 選中行的名字列/// </summary>private static string Name = "";/// <summary>/// 選中行的描述列/// </summary>private static string Descripe = "";在每次選中行觸發(fā)SelectedIndexChanged事件時更改這些屬性的值
/// <summary>/// 行選擇操作完成后激發(fā)/// </summary>/// <param name="sender"></param>/// <param name="e"></param>protected void grdQualityDoorAndParts_SelectedIndexChanged(object sender, EventArgs e){//當(dāng)選中行時if (grdQualityDoorAndParts.SelectedIndex >= 0){btnEdit.Enabled = true;//啟用編輯按鈕btnDelete.Enabled = true;//啟用刪除按鈕Code = grdQualityDoorAndParts.SelectedRow.Cells[0].Text.Trim().ToString() == " " ? "" : grdQualityDoorAndParts.SelectedRow.Cells[0].Text.Trim().ToString();Name = grdQualityDoorAndParts.SelectedRow.Cells[1].Text.Trim().ToString() == " " ? "" : grdQualityDoorAndParts.SelectedRow.Cells[1].Text.Trim().ToString();Descripe = grdQualityDoorAndParts.SelectedRow.Cells[2].Text.Trim().ToString() == " " ? "" : grdQualityDoorAndParts.SelectedRow.Cells[2].Text.Trim().ToString();//給編輯按鈕添加點擊事件,跳轉(zhuǎn)到編輯頁面,并傳值過去(在這里將名稱列的值傳給編輯界面)string url1 = "Edit.aspx/?Name=" + Name;btnEdit.Attributes.Add("onclick", "window.showModalDialog('" + url1 + "',window,'dialogHeight:550px;dialogWidth:800px'); return false;");}else{btnEdit.Enabled = false;//啟用編輯按鈕btnDelete.Enabled = false;//啟用刪除按鈕 }}?
轉(zhuǎn)載于:https://www.cnblogs.com/ingvner/p/7225473.html
總結(jié)
以上是生活随笔為你收集整理的asp.net DataGrid GridView 表格之选中行与获取选中行数据的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。