2011-11-04 59 views
0

我有一個Custom.ascx文件在多個頁面上使用。 Custom.ascx包含一對控件和一個名爲cmdCustomPageButton的按鈕。來自不同頁面的調用過程

當用戶點擊cmdCustomPageButton時,cmdCustomPageButton執行一個受保護的子組件,該組件從數據庫中獲取一些數據。

使用Custom.ascx的Page1.aspx具有自己的一組控件和它執行的過程。它包含一個名爲cmdPage1Button的按鈕和一個名爲RetriveData的過程,該過程也在Page1.aspx中由其他過程調用。

當cmdPage1Button被點擊時,它會調用RetriveData。 RetriveData僅適用於Page1.aspx。 Page2.aspx和Page3.aspx都有一個類似於RetriveData的過程,但只與它自己的頁面有關。

試圖解釋使用代碼
Custom.ascx

Public Class Custom 
Protected Sub cmdCustomPageButton_Click(Byval sender as Object, ByVal e as EventArgs) Handels cmdCustomPageButton_Click 
    //Code that gets data from the database 
End Class 

Page1.aspx的

Public Class Page1 
Protected Sub cmdPage1Button_Click(Byval sender as Object, ByVal e as EventArgs) Handels cmdPage1Button_Click_Click 
    //Some code  
    RetriveData() 
End Sub 

Sub RetriveData() 
    //Some code 
End Sub 

End Class 

問題。
當cmdCustomPageButton被點擊時,我如何調用不同的RetriveData過程來形成頁面1,頁面2或頁面3的相關頁面?

回答

0

請參考下面的鏈接,其中有一個更好的主意,以類似於你的查詢。

Calling a method in parent page from user control

總之,在你的用戶控件創建一個事件委託並在每個頁面的在使用用戶控件處理該事件。如果單擊用戶控件中的按鈕,則會在相應的父頁面中觸發該事件,並且可以在該事件中調用RetriveData方法。對不起,如果你的查詢被誤解了。

+0

謝謝!!我會給這個嘗試將不得不轉換代碼感謝這個網站[鏈接](http://www.developerfusion.com/tools/convert/csharp-to-vb/)。 – Hav0c

+0

好吧,我已經嘗試了上面的鏈接代碼,但現在我得到'使用'RaiseEvent'語句來提高事件'的代碼行MyUserControl.UserControlButtonClicked + =新的EventHandler(AddressOf MyUserControl_UserControlButtonClicked) – Hav0c

0

MyUserControl.ascx頁面代碼。

Public Class MyUserControl 
Inherits System.Web.UI.UserControl 

Public Event UserControlButtonClicked As EventHandler 

Private Sub OnUserControlButtonClick()   
     RaiseEvent UserControlButtonClicked(Me, EventArgs.Empty) 
End Sub 

Protected Sub TheButton_Click(ByVal sender As Object, ByVal e As EventArgs) 
    ' .... do stuff then fire off the event 
    OnUserControlButtonClick 
End Sub 
End Class 

Default.aspx頁面代碼

Public Class _Default 
Inherits System.Web.UI.Page 

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) 
    ' hook up event handler for exposed user control event 
    AddHandler MyUserControl.UserControlButtonClicked, AddressOf Me.MyUserControl_UserControlButtonClicked 
End Sub 

Private Sub MyUserControl_UserControlButtonClicked(ByVal sender As Object, ByVal e As EventArgs) 
    ' ... do something when event is fired 
End Sub 
End Class 

所有信用卡客戶操作系統clklachu指着我在正確的方向。

轉換通過以下網站進行link

感謝

相關問題