新增了自定義的編輯和刪除按鈕

c979170768發表於2011-11-23
01.protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)   
02.  
03.    if (e.CommandName == "ibEdit")   
04.    {   
05.       GridViewRow drv = ((GridViewRow)(((ImageButton)(e.CommandSource)).Parent.Parent)); //此得出的值是表示那行被選中的索引值       
06.        GridView gv = ((GridView)(((ImageButton)(e.CommandSource)).Parent.Parent.Parent.Parent));   
07.        gv.EditIndex = drv.RowIndex;   
08.        isEdit =true;   
09.        TestCon();   
10.  
11.    }   
12.    if (e.CommandName == "ibCancel")   
13.    {   
14.       GridView gv = ((GridView)(((ImageButton)(e.CommandSource)).Parent.Parent.Parent.Parent));   
15.       gv.EditIndex = -1;   
16.       editRow = -1;   
17.       isEdit =false;   
18.  
19.       TestCon();   
20.    }   
21.    if (e.CommandName == "ibOk")   
22.    {   
23.          GridViewRow drv = ((GridViewRow)(((ImageButton)(e.CommandSource)).Parent.Parent));   
24.           string tb1 =  ((TextBox)drv.FindControl("tbPublishTime")).Text;   
25.        Response.Write("<script>alert('TextBox的文字為:" + tb1 + "')</script>)");   
26.測試作用,這裡可以改成SQL更新語句。   
27.  
28.        gv.EditIndex = -1;   
29.        editRow = -1;   
30.        isEdit =false;   
31.  
32.        TestCon();   
33.             
34.    }   
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
        if (e.CommandName == "ibEdit")
        {
           GridViewRow drv = ((GridViewRow)(((ImageButton)(e.CommandSource)).Parent.Parent)); //此得出的值是表示那行被選中的索引值    
            GridView gv = ((GridView)(((ImageButton)(e.CommandSource)).Parent.Parent.Parent.Parent));
            gv.EditIndex = drv.RowIndex;
            isEdit =true;
            TestCon();
 
        }
        if (e.CommandName == "ibCancel")
        {
           GridView gv = ((GridView)(((ImageButton)(e.CommandSource)).Parent.Parent.Parent.Parent));
           gv.EditIndex = -1;
           editRow = -1;
           isEdit =false;

           TestCon();
        }
        if (e.CommandName == "ibOk")
        {
              GridViewRow drv = ((GridViewRow)(((ImageButton)(e.CommandSource)).Parent.Parent));
               string tb1 =  ((TextBox)drv.FindControl("tbPublishTime")).Text;
            Response.Write("<script>alert('TextBox的文字為:" + tb1 + "')</script>)");
  //測試作用,這裡可以改成SQL更新語句。

            gv.EditIndex = -1;
            editRow = -1;
            isEdit =false;

            TestCon();
              
        }
} 

 然後在GridView1_RowDataBound中新增程式碼

view plaincopy to clipboardprint?
01.    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)   
02.    {   
03.        if (isEdit)   
04.        {   
05.            if (e.Row.RowIndex == editRow)   
06.            {   
07.                ((ImageButton)e.Row.FindControl("ibEdit")).Visible = false;   
08.                   
09.                ((ImageButton)e.Row.FindControl("ibOk")).Visible = true;   
10.                ((ImageButton)e.Row.FindControl("ibCancel")).Visible = true;   
11.           //     isEdit = "";   
12.            }   
13.        }   
14.}  
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (isEdit)
        {
            if (e.Row.RowIndex == editRow)
            {
                ((ImageButton)e.Row.FindControl("ibEdit")).Visible = false;
                
                ((ImageButton)e.Row.FindControl("ibOk")).Visible = true;
                ((ImageButton)e.Row.FindControl("ibCancel")).Visible = true;
           //     isEdit = "";
            }
        }
} 
  
 

 

相關文章