asp.net GridView 表格之選中行

來源:互聯網
上載者:User

標籤:hone   checked   als   使用   設定   bsp   button   .net   alt   

一、GridView 表格之選中行

asp.net選中行的功能最初以為只能通過屬性中AllowGenerateSelectButton(運行時是否自動產生選擇按鈕)來實現,需要點擊產生的選擇按鈕來操作,但這樣使用並是很方便。

經尋找找到了改進辦法如下效果

滑鼠經過時背景色會改變,選中後可擷取響應行的資料

實現方法如下:

首先前台設計屬性框中事件綁定RowDataBound(在對時局進行了綁定後激發)事件

後台代碼如下:

   /// <summary>        /// 在對資料進行了綁定後激發        /// 主要實現滑鼠點擊時選中該行        /// </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 方法2            int i;            for (i = 0; i <= GridView1.Rows.Count; i++)            {                //首先判斷是否是資料行                if (e.Row.RowType == DataControlRowType.DataRow)                {                    //當滑鼠停留時更改背景色                    e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor=‘#00A9FF‘");                    //當滑鼠移開時還原背景色                    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");                    //單擊行的任意列會自動選中此行                    e.Row.Attributes.Add("onclick", "__doPostBack(‘GridView1‘,‘Select$" + e.Row.RowIndex + "‘)");                }            }            #endregion

 二、擷取選中行資料

選中某行後擷取資料

在屬性框中事件選項中選擇設定SelectedIndexChanged( 在GridView中選擇行時,在該行選擇完成後激發)事件選項

後台代碼如下

        /// <summary>        /// 選擇某行時在最左側更新顯示資料詳細        /// 在DataGriew選擇行時,在該選擇操作完成後激發        /// </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();            }        }

 

asp.net GridView 表格之選中行

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.