我有一系列控件(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
我希望這有助於澄清一些事情,爲什麼我在遇到麻煩。
代碼提示:Dim(something)As Button = New Button'可以改寫爲Dim(something)As New Button'。 – Ryan 2012-07-19 16:59:26