GridViewRow可以任意位置單擊引發事件的方法!

來源:互聯網
上載者:User
GridView 是 ASP.NET 2.0 中應用最為廣泛一個控制項,幾乎所有的資料操作都需要它,正如我們平常所應用的,可以編輯、刪除、選擇等等,但如果客戶有需要通過單擊行而引發超連結或者進入行編輯狀態時,我們該如何?,這裡介紹了一種方法來實現此功能。它將允許你通過點擊行的任何一個位置而引發你所需要的事件。

首先為 GridView  填充資料 private void BindData()
    {

        SqlConnection myConnection = new SqlConnection(ConnectionString);

        SqlCommand myCommand = new SqlCommand("SELECT * FROM Users", myConnection);

        SqlDataAdapter ad = new SqlDataAdapter(myCommand);

        DataSet ds = new DataSet();

        ad.Fill(ds);

        GridView1.DataSource = ds;

        GridView1.DataBind();

    }

接下來我們在 GridView_RowDataBound  事件中為 GridViewRow 賦予單擊屬性  protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        string alertBox = "alert('";

        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            alertBox += e.Row.RowIndex;

            alertBox += "')";

            e.Row.Attributes.Add("onclick", alertBox);

        }

    }

好了,很簡單的方法,希望對你有用!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.