2016-03-08 83 views
1

asp.net中的按鈕自動刷新頁面。在調用按鈕事件之前,調用默認的Page_Load函數(我認爲)。按鈕事件後運行功能

我正在尋找調用所有按鈕事件後調用函數的方法。

編輯

我要永遠調用的函數,即使沒有按鈕被按下。所以把這個函數放在按鈕事件中是行不通的。

創建按鈕

ImageButton enc2Button = Hardwarerecorders.DeviceManager.getEncStatusImage(d, "res/", false); 

這是click事件

enc2Button.Click += new ImageClickEventHandler(startEnc2); 

按鈕事件功能

void startEnc2(object sender, EventArgs e) 
{ 
    ImageButton button = (ImageButton)sender; 
    int arrayIndex = Int32.Parse(button.Attributes["index"]); 
    Hardwarerecorders.DeviceManager.startEnc((Hardwarerecorders.Device)devices[arrayIndex], false); 
} 
+0

如果您未添加一些代碼並提高此帖子的質量,則很難爲您提供幫助 – acostela

+0

您是否在談論javascript功能?或者後面的代碼? –

+0

@PiyushKhatri我正在談論一個C#函數 –

回答

0

使用Response.redirect("index.aspx");

爲您的按鈕中的最後一行,這種重新定向頁面index.aspx,而是採用index.aspx使用上的按鈕被放置在文件的名稱,頁面會刷新,它會按預期工作。

所以如下 -

void startEnc2(object sender, EventArgs e) 
    { 
     ImageButton button = (ImageButton)sender; 
     int arrayIndex = Int32.Parse(button.Attributes["index"]); 
     Hardwarerecorders.DeviceManager.startEnc((Hardwarerecorders.Device)devices[arrayIndex], false); 
     Response.redirect("index.aspx"); 
    } 
-1

可以通過調用簡單到底該函數調用按鈕事件之後的任何功能。如果您在此發佈代碼以便了解問題,那將會很好。

1

重寫OnPreRender方法。它在Control-Events之後調用。

protected override void OnPreRender(EventArgs e) 
    { 
     base.OnPreRender(e); 
     //Your stuff 
    } 

https://msdn.microsoft.com/en-us/library/ms178472(v=vs.100).aspx

+0

讓我們繼續討論聊天:http://chat.stackoverflow。COM /房間/ 105667 /討論-之間-M-zeinstra和 - PIYUSH-卡特里 –

-1

你爲什麼不叫在頁面加載函數結束時所需的功能?每當控件進入服務器,頁面加載將被調用,從而調用你的函數。不需要點擊按鈕來調用該功能。