2012-07-19 16 views
1

我有一系列控件(3個標籤,3個文本框和2個按鈕),當用戶單擊我的頁面上的按鈕時創建。該頁面使用將生成這些控件的命令進行回發。但是,當我填寫我的文本框並單擊新生成的按鈕之一(btnCreate)時,沒有任何反應,並且頁面只是重新加載一次。動態生成的代碼隱藏按鈕不會觸發它的事件

我想要發生的是,當用戶點擊btnCreate時,它會觸發它的功能,並將TextBox.Text放到數據庫中。但是,再次,當點擊btnCreate時,沒有任何反應。

下面是生成按鈕(這是生成的文本框,這是我在這裏排除了同樣的功能)的代碼:

Protected Sub createSpecialNotes() 
    Dim btnCreate As Button = New Button 
    Dim btnClear As Button = New Button 

    'Place properties 

    lblSubject.Text = "subject" 
    lblSubject.ID = "lblSubject" 
    lblSubject.Width = 700 
    lblAgenda.Text = "Agenda Note" 
    lblAgenda.ID = "lblAgenda" 
    lblAgenda.Width = 700 
    lblMinutes.Text = "Minutes Note" 
    lblMinutes.ID = "lblMinutes" 
    lblMinutes.Width = 700 

    btnCreate.Text = "Create" 
    btnCreate.ID = "btnCreate" 
    btnClear.Text = "Clear" 
    btnClear.ID = "btnClear" 

    'Add handlers for buttons 
    AddHandler btnCreate.Click, AddressOf btnCreate_Click 
    AddHandler btnClear.Click, AddressOf btnClear_Click 

    plhCreateSpecialNotes.Controls.Add(btnCreate) 
    plhCreateSpecialNotes.Controls.Add(btnClear) 
End Sub 

爲了簡便起見,我們只能說btnCreate需求只顯示文本框的內容。

編輯1:創建特殊筆記的調用位於page_preInit上。它的調用由以下

Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit 
    'Find the control that was fired 
    Dim controlFired As Control = GetPostBackControl(Me.Page) 

    If (controlFired IsNot Nothing) Then 
     If (controlFired.ClientID.ToString() = "btnCreateSpecial") Then 
      Call createSpecialNotes() 
     End If 
     If (controlFired.ClientID.ToString() = "btnCreate") Then 
      'i've tried putting things here to no avail. 
     End If 
    End If 

End Sub 

功能getpostbackcontrol的看起來像這樣

Public Shared Function GetPostBackControl(ByVal thePage As Page) As Control 
    Dim myControl As Control = Nothing 
    Dim ctrlName As String = thePage.Request.Params.Get("__EVENTTARGET") 
    If ((ctrlName IsNot Nothing) And (ctrlName <> String.Empty)) Then 
     myControl = thePage.FindControl(ctrlName) 
    Else 
     For Each Item As String In thePage.Request.Form 
      Dim c As Control = thePage.FindControl(Item) 
      If (TypeOf (c) Is System.Web.UI.WebControls.Button) Then 
       myControl = c 
      End If 
     Next 

    End If 
    Return myControl 
End Function 

我希望這有助於澄清一些事情,爲什麼我在遇到麻煩。

+0

代碼提示:Dim(something)As Button = New Button'可以改寫爲Dim(something)As New Button'。 – Ryan 2012-07-19 16:59:26

回答

2

這裏真正有用的是知道你什麼時候打電話createSpecialNotes()。但很可能你缺少的是頁面的生命週期。

確保createSpecialNotes()被稱爲OnInit的頁面。之後的任何事情都已經晚了,您的事件處理程序將不會被解僱。

如果您的網頁已達到OnLoad,並且您尚未將處理程序綁定到您的控件,那麼它將不會被觸發。

我建議您仔細閱讀本文。 http://msdn.microsoft.com/en-us/library/ms178472.aspx

+0

即使在回發你musst在初始事件添加動態控制。 – 2012-07-19 17:04:44

+0

createSpecialNotes()正常工作。一切正常,只要點擊調用它的按鈕就會顯示出來。 它在Page_PreInit上被調用。這個問題真的是當我點擊btnCreate時,沒有任何反應。 – Lenigod 2012-07-19 17:36:25

+0

附錄:所以我上傳了一張關於我在看[這裏](http://i.imgur.com/pcJ4w.jpg)的圖片。我強調的是這個問題。該按鈕也需要將textbox.text數據輸入到數據庫中。但是,一旦我點擊它,一切都不會發生。 – Lenigod 2012-07-19 17:53:44

相關問題