2010-11-04 69 views
0

我有一個用戶控件已被動態添加到頁面。當我點擊用戶控制按鈕:用戶控件按鈕單擊事件未提升

  1. 的button_click事件沒有被提出
  2. 用戶控件從頁面

這裏移除頁面職位的按鈕在我的用戶控件上點擊事件:

Protected Sub btnAddAttribute_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddAttribute.Click 
     Try 
      Dim attrName As String = txtAddAttributeName.Text 
      Dim attrValue As String = txtAddAttributeValue.Text 

      'ADD ATTRIBUTE TO ATTRIBUTE TABLE 
      putSQLData("INSERT INTO OD_Attribute_Values (AttributeName, AttributeValue) VALUES('" & attrValue & "', '" & attrName & "'") 

      'ADD ATTRIBUTE TO PRODUCT DATA FOR THIS PRODUCT 
      putSQLData("UPDATE OD_Product_Data SET VariantMapping = VariantMapping + ' | " & attrName & ":" & attrValue & "' WHERE SKU = '" & SelectedSKU & "'") 

      'ADD NEW ctrlAttribute TO PARENT PLACEHOLDER AND SET VALUES 
      Dim newAttr As New AttributeControl 
      newAttr.AttributeName = attrName 
      newAttr.AttributeValue = attrValue 
      Page.Controls.AddAt(Page.FindControl("phAttributes").ClientID, newAttr) 

      'REMOVE THIS CONTROL FROM PARENT PLACEHOLDER 
      Me.Dispose() 

     Catch ex As Exception 
      Common.SendError(ex.Message, "AttributeControl.btnAddAttribute_Click") 
     End Try 
End Sub 

這裏是動態添加控件:

Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init 
     'WEB TAB 
     getAttributes() 
End Sub 

    Protected Sub getAttributes() 
     Try 
      Dim attr As String = "" 
      Dim attrName As String = "" 
      Dim attrValue As String = "" 

      Dim ds As DataSet = getSQLData("SELECT VariantMapping FROM OD_Product_Data WHERE SKU='" & selectedSKU & "'") 
      For Each dr As DataRow In ds.Tables(0).Rows() 
       attr = dr(0).ToString 
      Next 
      ds = Nothing 
      Dim attrArr As Array = attr.Split("|") 
      For Each item As String In attrArr 
       Dim attrDetail As Array = item.Split(":") 
       attrName = attrDetail(0) 
       attrValue = attrDetail(1) 
       Dim ctrlAttributes As AttributeControl = LoadControl("ctrlAttribute.ascx") 
       ctrlAttributes.AttributeName = attrName 
       ctrlAttributes.AttributeValue = attrValue 
       ctrlAttributes.ID = "ctrlAttribute-" & attrName 
       phAttributes.Controls.Add(ctrlAttributes) 
      Next 
     Catch ex As Exception 
      SendError(ex.Message, "Default.getAttributes") 
     End Try 
End Sub 

Protected Sub btnAddAttribute_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddAttribute.Click 
     Dim ctrlAttributes As AttributeControl = LoadControl("ctrlAttribute.ascx") 
     ctrlAttributes.ID = "ctrlAttribute" & phAttributes.Controls.Count + 1 
     phAttributes.Controls.Add(ctrlAttributes) 
End Sub 

Protected Sub btnCreateAttribute_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCreateAttribute.Click 
     Dim ctrlAddAttribute As AddAttributeControl = LoadControl("ctrlAddAttribute.ascx") 
     ctrlAddAttribute.SelectedSKU = selectedSKU 
     ctrlAddAttribute.ID = "ctrlAddAttribute" & phAttributes.Controls.Count + 1 
     phAttributes.Controls.Add(ctrlAddAttribute) 
End Sub 
+0

發佈你的代碼會有幫助,這好像是事件處理程序沒有正確設置或PostBack導致意外的行爲 – kd7 2010-11-04 18:29:02

+0

出於好奇,你爲什麼在代碼註釋中尖叫? – 2010-11-04 18:40:44

+0

您是否確定您的控件在頁面重新加載後仍然存在。就像Random說的那樣,看到控件被調用的代碼會更有幫助。 – Lareau 2010-11-04 18:45:41

回答

2

當添加控件動態的頁面,您必須一定要控制添加到頁面上的每一個訪問。在第一次訪問時添加動態控件是一個常見的錯誤,但在回發上添加而不是 - 這是行不通的!您需要在每次訪問時將動態控件添加到控件層次結構中。

這裏有一對夫婦的資源我動態創建控件工作之前推薦閱讀:

快樂編程!

+0

謝謝!我讀了你鏈接的信息。 但是,我沒有找到答案。 下面是發生了什麼事情,也許你可以指向正確的方向。 1.頁面加載 2.用戶單擊頁面上的一個按鈕,將頁面上的用戶控件添加到頁面 上的佔位符3。然後,用戶單擊該用戶控件上的按鈕。 回發時,觸發回發的用戶控件將丟失,並且該特定事件的事件處理程序(在用戶控件上)不會被觸發。 如何告訴頁面重新添加出生在#2中的控件並在該控件上提高按鈕單擊事件? – s15199d 2010-11-04 20:58:34

+0

@ s15199d:您將不得不記住,添加了哪些控件。你可以在頁面上保存一些標誌狀態,這可能是最好的。我沒有爲你準備好乾的答案,因爲我從來沒有以這種方式處理過動態控件。我以前在這個領域的所有經驗都是根據數據庫中的配置動態加載控件。 – 2010-11-04 22:19:44

+0

謝謝斯科特,我遵循你的建議,並且主要爲我工作。每次我動態地添加一個控件時,我將它的control.ID添加到ViewState變量,然後在Page_Load上解析該viewstate變量並重新添加我的動態添加控件。似乎主要是工作。我還有其他幾個元素可以解決這個問題... – s15199d 2010-11-08 16:46:48