2013-01-15 19 views
0

我創建了一個用戶控件,在我的網站的每個頁面上顯示標題(不是< head>,我的意思是標題,日期時間頁面已創建,等等)。在那個用戶控件中,我也有一個鏈接。鏈接將顯示在頁面IF (user = admin)上。如何設置調用usercontrol鏈接的方法

目前這個鏈接是一個純粹的鏈接,我沒有問題將「navigateUrl」更改爲每個頁面的正確值。 (每個頁面都包含此用戶控件,並且從每個頁面我爲navigateUrl設置值。)

但是!

在我的一些頁面上,我使用了鏈接按鈕而不是超鏈接。但後來我必須從page.aspx而不是usercontrol.ascx添加linkbutton

我的問題是,我想改變我的用戶控件中的超鏈接,而不是鏈接,所以我可以調用具有該鏈接的方法。 (方法在page.aspx上,不在用戶控件中)。

不同的頁面調用方法不同,所以我想設置每次包含用戶控件時調用的方法。

如果我有我的用戶控制

<asp:LinkButton ID="LinkButton1" runat="server">LinkButton</asp:LinkButton> 

內,現在我在用戶控件中設置值:

mainPageHeader1.headTitle = "text"; 

如何設置調用什麼方法LinkBut​​ton的?

回答

1

更新

在你的用戶控件的標記,指定的處理程序中單擊事件

<asp:LinkButton OnClick="LinkButton1_Clicked" runat="server" ID="LinkButton1" /> 

在你的用戶控件

public class MyUserControl 
{ 
    public event System.EventHandler LinkButtonClicked; 


    //add handler for your LinkButton 
    protected void LinkButton1_Clicked(object sender, EventArgs e) 
    { 
     //Raise your custom event here that can be handled in any page that use your control 
     LinkButtonClicked(sender, e); 
    } 
} 

在你的頁面聲明一個自定義事件。 aspx,爲您的自定義事件添加處理程序

protected void MyUserControl2_LinkButtonClicked(object sender, EventArgs e) 
{ 
    //handle the event here 
} 

更新

在你的頁面,你把你的控制,

<custom:MyUserControl ID="MyUserControl2" runat="server"  
         LinkButtonClicked="MyUserControl_LinkButtonClicked" /> 

這是所有

更新

訂閱的事件在代碼隱藏做工作。我還沒有弄清楚爲什麼它不能從標記中運行。

從該用戶控件的頁面的Page_Load中,做到這一點

MyUserControl2.LinkButtonClicked += new EventHandler(MyUserControl_LinkButtonClicked); 

,它應該工作。

檢查,看是否該事件已訂閱要麼代碼隱藏(eventhandler += EventHandler(sender, e)或ASPX標記OnClick="EventHandlerMethodName") - this would be null if it wasn't subscribed to somewhere

+0

我不知道,我知道這將如何工作。如果我在usercontrol中聲明瞭「linkbutton_clicked」,並在page.aspx.c中聲明瞭「myusercontrol_linkbutton_clicked」。一切正常嗎?或者有什麼需要在頁面創建時「設置」?我明白myusercontrol_linkbuttonClicked中的我必須調用我想要執行的方法。 – Easyrider

+1

查看我的更新回答 – codingbiz

+0

非常感謝,明天早上我會先試一試! – Easyrider