自從我以爲「我怎麼能從usercontrol的按鈕點擊觸發我的事件?答案非常迅速:「我可以抓住按鈕的點擊事件並從那裏提起我!」經過4個小時的調試和調試以及更多的調試和頁面生命週期以及更多的調試和更多的斷點等等,我轉而尋求GREAT STACKOVERFLOW(弓)的幫助。所以這個問題很簡單,爲什麼我不能捕獲放置在用戶控件內的按鈕的onclick事件,所以我可以觸發我的事件? 至於代碼: 這裏是ASCX:無法捕獲usercontrol上的按鈕onclick事件
背後的用戶控件<%@ Control Language="vb" AutoEventWireup="true" CodeBehind="CosmaButton.ascx.vb" Inherits="Comanda.CosmaButton" %>
代碼:
Public Class CosmaButton
Inherits System.Web.UI.UserControl
Public Event ClickBtn As EventHandler
Private _title As String = "test"
Private _width As Integer
Private WithEvents _button As New Button
Protected Overrides Sub OnInit(e As EventArgs)
MyBase.OnInit(e)
End Sub
Protected Overrides Sub OnLoad(e As EventArgs)
MyBase.OnLoad(e)
End Sub
Protected Overrides Sub OnPreRender(e As EventArgs)
_button.CssClass = "styled"
_button.Text = _title
_button.Attributes("runat") = "server"
If _width > 0 Then
_button.Width = _width
End If
Me.Controls.Add(_button)
MyBase.OnPreRender(e)
End Sub
Protected Sub _button_Clicked(sender As Object, e As EventArgs)
RaiseEvent ClickBtn(Nothing, Nothing)
End Sub
End Class
,並終於在這裏爲aspx頁面的代碼隱藏:
Public Class Login1
Inherits System.Web.UI.Page
Protected Sub Page_Init(sender As Object, e As EventArgs) Handles Me.Init
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
AddHandler btn.ClickBtn, AddressOf btn_Click
End Sub
Protected Sub btn_Click(sender As Object, e As EventArgs) Handles btn.ClickBtn
End Sub
End Class
PS做了我的功課,並搜索了與此相關的每篇文章,並發現: http://www.codeproject.com/Articles/33874/Capture-Button-s-Click-Event-of-User-Control-in-th 這是有點令人沮喪,因爲我遵循和refollowed,並一步一步在這傢伙的職位,但效果相同。沒有嚇人的clickevent!
編輯1:
添加處理程序,以點擊事件,以便更新的代碼隱藏的版本是這樣的:
Public Class CosmaButton
Inherits System.Web.UI.UserControl
Public Event ClickBtn As EventHandler
Private _title As String = "test"
Private _width As Integer
Private WithEvents _button As New Button
Protected Overrides Sub OnInit(e As EventArgs)
Me.Controls.Add(_button)
AddHandler _button.Click, AddressOf _button_Clicked
MyBase.OnInit(e)
End Sub
Protected Overrides Sub OnLoad(e As EventArgs)
MyBase.OnLoad(e)
End Sub
Protected Overrides Sub OnPreRender(e As EventArgs)
_button.CssClass = "styled"
_button.Text = _title
_button.Attributes("runat") = "server"
If _width > 0 Then
_button.Width = _width
End If
MyBase.OnPreRender(e)
End Sub
Protected Sub _button_Clicked(sender As Object, e As EventArgs)
RaiseEvent ClickBtn(Nothing, Nothing)
End Sub
End Class
,仍然一無所獲。 我之前多次添加處理程序,但在閱讀並嘗試後提到的後,我添加了一個asp:button標記到我的用戶控件,但沒有成功,之後,我發佈了問題。
我編輯了自己的冠軍。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 –