2016-03-08 185 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

你为什么不叫在页面加载函数结束时所需的功能?每当控件进入服务器,页面加载将被调用,从而调用你的函数。不需要点击按钮来调用该功能。