天天看點

gridview中嵌套Button控件

這個是在網上找的一片文章,自己試着調試了一下,但并未調試成功,以後有時間了在調試吧!大家誰調試出來也可以釋出出來分享。

  protected   void  GridView1_RowCreated( object  sender, GridViewRowEventArgs e)

     ... {

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

         ... {

             if  (e.Row.FindControl("Button1") !=  null )

             ... {

                Button btn = (Button)e.Row.FindControl("Button1");

                btn.Click +=  new  EventHandler(btn_Click);

            }

        }

    }

     private   void  btn_Click( object  sender, EventArgs e)

     ... {

        Button btn = (Button)sender;

        GridViewRow gvr = (GridViewRow)btn.Parent.Parent;

         string  pk = GridView1.DataKeys[gvr.RowIndex].Value.ToString();

         this .Label1.Text = pk;

http://topic.csdn.net/u/20080401/17/690f559d-7dcc-457e-bdec-6e01351bdbfe.html

<asp:TemplateField>   

           <ItemTemplate>   

                   <asp:Button    ID="Button1"    runat="server"    CommandName="MyCommand"    Text="Button"    />   

           </ItemTemplate>   

   </asp:TemplateField>   

   ===============================================   

   protected    void    GridView1_RowCommand(object    sender,    GridViewCommandEventArgs    e)   

   {   

           if    (e.CommandName    ==    "MyCommand")   

           {   

                  Button button = (Button)e.CommandSource;

                  GridViewRow row = (GridViewRow)button.Parent.Parent;

                  string a = row.Cells[1].Text.ToString();// 獲得第一個單元格的值    

                  string    b    =    this.GridView1.DataKeys[row.DataItemIndex].Values[0];// 獲得 DataKeys 的值    

           }   

   }