【问题标题】:Run function after button events按钮事件后运行函数
【发布时间】:2016-06-22 05:14:10
【问题描述】:

asp.net 中的按钮会自动刷新页面。在调用按钮事件之前,会调用默认的 Page_Load 函数(我认为)。

我正在寻找一种在调用所有按钮事件后调用函数的方法。

编辑

我想始终调用该函数,即使没有按下任何按钮。所以把函数放在按钮事件中是行不通的。

创建按钮

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

这是点击事件

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);
}

【问题讨论】:

  • 如果你不添加一些代码和提高这篇文章的质量,很难帮助你
  • 你说的是javascript函数吗?还是背后的代码?
  • @PiyushKhatri 我说的是 C# 函数
  • 如果您想在按钮点击事件后调用任何 c# 函数,您可以在按钮点击事件方法的最后一行调用该函数。我想,你有这样做的想法。如果您指的是其他内容,请发布一些代码。
  • @PiyushKhatri 查看编辑

标签: c# asp.net events button pageload


【解决方案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

【讨论】:

【解决方案2】:

使用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");
    }

【讨论】:

    【解决方案3】:

    您可以在按钮事件之后调用任何函数,方法是最后调用简单的函数。如果您在此处发布代码以了解问题,那就太好了。

    【讨论】:

      【解决方案4】:

      为什么不在页面加载函数的末尾调用所需的函数?每当控件到达服务器时,就会调用页面加载,从而调用您的函数。不需要点击按钮来调用函数。

      【讨论】:

        猜你喜欢
        • 2014-03-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多