2012-03-15 48 views
3

我有一個基於DropDownList的值的動態創建的GirdView。 我使用了Itemplate接口產生的字段:從ITemplate內部動態創建的ImageButton中觸發RowCommand事件

public class CreateItemTemplateOrder : ITemplate 
{ 
    ImageButton imgbtn_up; 
    ImageButton imgbtn_down; 

    string s_imgbtn_up_name; 
    string s_imgbtn_up_ImageUrl; 
    string s_imgbtn_up_CommandName; 
    string s_imgbtn_up_CommandArgument; 

    public CreateItemTemplateOrder(string imgbtn_up_name, string imgbtn_up_ImageUrl, string imgbtn_up_CommandName, string imgbtn_up_CommandArgument) 
    { 

     this.s_imgbtn_up_name = imgbtn_up_name; 
     this.s_imgbtn_up_ImageUrl = imgbtn_up_ImageUrl; 
     this.s_imgbtn_up_CommandName = imgbtn_up_CommandName; 
     this.s_imgbtn_up_CommandArgument = imgbtn_up_CommandArgument; 

    } 

    public void InstantiateIn(Control objContainer) 
    { 
     imgbtn_up = new ImageButton(); 
     imgbtn_up.DataBinding += new EventHandler(imgbtn_up_DataBinding); 
     objContainer.Controls.Add(imgbtn_up); 
    } 

    private void imgbtn_up_DataBinding(object sender, EventArgs e) 
    { 
     ImageButton imgbtn_up = (ImageButton)sender; 
     imgbtn_up.ID = s_imgbtn_up_name; 
     imgbtn_up.ImageUrl = s_imgbtn_up_ImageUrl; 
     imgbtn_up.CommandName = s_imgbtn_up_CommandName; 
     imgbtn_up.CommandArgument = s_imgbtn_up_CommandArgument; 
     imgbtn_up.CausesValidation = false; 
    } 
} 

現在我想火RowCommand從命令參數和命令名稱此動態生成列一定到這的ImageButton

代碼得到這個工作是:

Protected void inizializza_gw_tipi(){ 
     TemplateField order_col = new TemplateField; 
     order_col.ItemTemplate = new CreateItemTemplateOrdine("imgbtn_up", "~/imgs/Up.gif", "minus", "order"); 
     order_col.HeaderText = "order"; 
     order_col.SortExpression = "order"; 

     gw_tipi.Columns.Add(order_col); 
} 

所有這些代碼工作正常,但在ImageButton的單擊時沒有從GridView的火災RowCommand

編輯: 我調用DropDownList的SelectedIndexChanged事件中的過程ddl_tipi_SelectedIndexChanged(Object sender,System.EventArgs e) inizializza_gw_tipi(); gw_tipi.DataBind(); }

Protected Sub inizializza_gw_tipi() 
     Using cn As New SqlConnection(shared_foos.connectionString) 
     Using cmd As New SqlCommand("SELECT nome_tabella, nome_campo_id, nome_campo_nome, nome_campo_descrizione, has_order FROM maschere_tipi WHERE id = @id", cn) 
      cmd.Parameters.AddWithValue("id", IIf(Session("sel_val") Is Nothing, "", Session("sel_val"))) 
      cn.Open() 
      Dim rdr As SqlDataReader = cmd.ExecuteReader 
      rdr.Read() 
      If rdr.HasRows Then 
       Dim b_crea_controllo As Boolean = True 
       'controllo se mettere o no la colonna ordine 
       If rdr("has_order") = True Then 
        'controllo che la colonna non sia già stata inserita, 
        'se è già stata inserita la rimuovo e la ricreo 
        For i As Integer = 0 To gw_tipi.Columns.Count - 1 
         If gw_tipi.Columns(i).HeaderText = "ordine" Then 
          'gw_tipi.Columns.Remove(gw_tipi.Columns(i)) 
          b_crea_controllo = False 
         End If 
        Next 

        If b_crea_controllo = True Then 

         Dim ordine_col As New TemplateField() 
         ordine_col.ItemTemplate = New CreateItemTemplateOrdine("lbl_ordine", "ordine", "imgbtn_up", "~/imgs/Up.gif", "meno", "ordine", "imgbtn_down", "~/imgs/Down.gif", "piu", "ordine", AddressOf ImageCommand) 
         ordine_col.HeaderText = "ordine" 
         ordine_col.SortExpression = rdr("nome_campo_nome") 

         gw_tipi.Columns.Add(ordine_col) 

        End If 
       End If 

       b_crea_controllo = True 

       For i As Integer = 0 To gw_tipi.Columns.Count - 1 
        If gw_tipi.Columns(i).HeaderText = rdr("nome_campo_nome") Then 
         b_crea_controllo = False 
        End If 
       Next 

       If b_crea_controllo = True Then 
        Dim nome_col As New TemplateField() 
        nome_col.ItemTemplate = New CreateItemTemplateLabel("lbl_nome", rdr("nome_campo_nome")) 
        nome_col.HeaderText = rdr("nome_campo_nome") 
        nome_col.SortExpression = rdr("nome_campo_nome") 
        gw_tipi.Columns.Add(nome_col) 
       End If 

       b_crea_controllo = True 

       For i As Integer = 0 To gw_tipi.Columns.Count - 1 
        If gw_tipi.Columns(i).HeaderText = rdr("nome_campo_descrizione") Then 
         b_crea_controllo = False 
        End If 
       Next 

       If b_crea_controllo = True Then 
        Dim descrizione_col As New TemplateField() 
        descrizione_col.ItemTemplate = New CreateItemTemplateLabel("lbl_descrizione", rdr("nome_campo_descrizione")) 
        descrizione_col.HeaderText = rdr("nome_campo_descrizione") 
        descrizione_col.SortExpression = rdr("nome_campo_descrizione") 
        gw_tipi.Columns.Add(descrizione_col) 
       End If 

       Dim str_order_by As String = " ORDER BY " 
       Dim str_ordine As String = "" 

       If rdr("has_order") = True Then 
        str_ordine = ", ordine " 
        str_order_by &= "ordine" 
       Else 
        str_order_by &= rdr("nome_campo_nome") 
       End If 

       Dim sqlds_tipi As New SqlDataSource(shared_foos.connectionString, "SELECT " & rdr("nome_campo_id") & ", " & rdr("nome_campo_nome") & ", " & rdr("nome_campo_descrizione") & str_ordine & " FROM " & rdr("nome_tabella") & str_order_by) 

       gw_tipi.DataSource = sqlds_tipi 

      End If 
     End Using 
    End Using 

End Sub 

回答

2

我把你的代碼原樣並測試過。這段代碼沒有錯誤。行命令也被正確觸發。我認爲問題在於你創建的動態gridview的事件綁定。

對於要觸發的行命令,必須在每次回發時創建gridview。並且該事件應該受到約束。

您提供的代碼並不完全是我所需要的。它沒有網格視圖的初始化。

以下應該給你一些見解。

GridView GV = new GridView(); //Make sure to create the grid view on every postback and populate it. Alternatively (i dont know if its a good practice.) you can store a reference of gridview in session. 
GV.RowCommand += new GridViewCommandEventHandler(GV_RowCommand); //bind the row command event. this should solve the problem. 

服務器不記錄動態創建的控件。所以當回發發生時,gridview控件不存在。因此不會觸發任何事件。

+0

eidted崗位 – 2012-03-15 10:28:47

+0

@SimoneFoschi編輯答案 – Gnani 2012-03-15 10:57:36

+0

@SimoneFoschi:gridview動態創建或動態填充?因爲在您選擇的索引更改事件中沒有網格視圖的初始化。在這種情況下可能是唯一缺少的是OnRowCommand =「rowcommandfunction」** aspx頁面上。 – Gnani 2012-03-15 11:10:49

0

我有同樣的問題 - 但我的問題是在母版頁EnableViewState =「false」。 onrowcommand工作正常後,我設置他的母版頁使用EnableViewState =「true」。

相關問題