简体   繁体   English

用户单击ASP.NET Web表单上的按钮与计时器运行调用相同按钮单击事件的方法之间的区别?

[英]Difference between when a user clicks button on an ASP.NET web form, and when a timer runs a method that calls the same button click event?

I have created an ASP.NET web form (page?) with a button that when clicked, will open up an Access database that runs an autoexec whose purpose is to run a delete/append sequence so I have data that I need in a table. 我创建了一个带有按钮的ASP.NET Web表单(页面?),单击该按钮将打开一个运行autoexec的Access数据库,该数据库旨在运行删除/追加序列,因此我在表中有所需的数据。 The code in the button on click event is supposed to also refresh the web page to display the updated data. 单击事件中按钮上的代码应该还刷新网页以显示更新的数据。 This works for the most part when a user physically clicks the button. 当用户实际单击按钮时,这在大多数情况下有效。 However, when I create a timer object to run a method that calls the click event I get an error: System.NullReferenceException: 'Object reference not set to an instance of an object.' 但是,当我创建计时器对象以运行调用click事件的方法时,出现错误:System.NullReferenceException:'对象引用未设置为对象的实例。 System.Web.HttpContext.Current.get returned null. System.Web.HttpContext.Current.get返回null。 What gives? 是什么赋予了?

Like I said, it works when there is a physical user clicking the button, but ultimately I want to automate the task of opening the Access db, as well as refreshing the web form (page?) with the new data. 就像我说的那样,当有一个物理用户单击按钮时,它可以工作,但是最终我要自动化打开Access数据库以及用新数据刷新Web表单(页面?)的任务。

This is the button click code. 这是按钮点击代码。 The commented lines are what I've already tried: 注释行是我已经尝试过的:

    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        Process.Start(@"C:\Users\mperea\Desktop\DC8SortMonitor.accdb");           

        HttpContext.Current.Response.Redirect("~/Default.aspx");

        //Response.Redirect("~/Default.aspx");
        //Page.Response.Redirect(Page.Request.Url.ToString(), true);
        //Server.TransferRequest(Request.Url.AbsolutePath, false);

        //Trying open access with this didn't work:
        //Access.Application oAccess = new Access.ApplicationClass();//oAccess.OpenCurrentDatabase(@"C:\Users\mperea\Desktop\DC8SortMonitor.accdb");
    } 

This is the timer object meant to programmatically execute the method that calls the click event: 这是计时器对象,用于以编程方式执行调用click事件的方法:

    protected void Page_Load(object sender, EventArgs e)
    {

        Timer aTimer;

        //Create a timer and set a two second interval.
        aTimer = new System.Timers.Timer();
        aTimer.Interval = 15000;

        //ElapsedEventHandler btnRefresh_Click = null;
        //Hook up the Elapsed event for the timer. 
        aTimer.Elapsed += timer;

        //Have the timer fire repeated events(true is the default)
        aTimer.AutoReset = true;

        //Start the timer
        aTimer.Enabled = true;
     }

and the method which the timer calls: 以及计时器调用的方法:

    public void timer(object sender, EventArgs e)
    {
        btnRefresh_Click(null, null);
    }

First thing that jumps out at me is that the button_click method is expecting a object sender and EventArgs e , which you are passing in as null, because obviously you don't have the information to give it. 让我惊讶的第一件事是button_click方法期望一个object senderEventArgs e ,您将其作为null传入,因为显然您没有提供信息。

You should not call your button event handlers in this way. 您不应该以这种方式调用按钮事件处理程序。 Instead you should move the shared code out from your click method into a different method which doesn't require those parameters that you can call from anywhere. 相反,您应该将共享代码从click方法移到另一个方法,该方法不需要可以从任何地方调用的那些参数。

protected void btnRefresh_Click(object sender, EventArgs e)
{
    Refresh();
} 

Now your Refresh Method 现在您的刷新方法

public void Refresh()
{
    Process.Start(@"C:\Users\mperea\Desktop\DC8SortMonitor.accdb");

    Response.Redirect("~/Default.aspx");

    //HttpContext.Current.Response.Redirect("~/Default.aspx");
    //Page.Response.Redirect(Page.Request.Url.ToString(), true);
    //Server.TransferRequest(Request.Url.AbsolutePath, false);

    //Access.Application oAccess = new Access.ApplicationClass();//oAccess.OpenCurrentDatabase(@"C:\Users\mperea\Desktop\DC8SortMonitor.accdb");
}

And timer 和计时器

 public void timer(object sender, EventArgs e)
{
    Refresh();
}

The click event handler should only really handle the event specifically, and any functionality relating to what the button does should be in separate methods. click事件处理程序应仅真正地专门处理该事件,并且与按钮的功能相关的任何功能应在单独的方法中。 Usually the work of a button is so small that it isn't necessary, but situations like this illustrate why it is generally a good idea. 通常,按钮的工作量很小,没有必要,但是这种情况说明了为什么它通常是一个好主意。

You can't use a timer and code that runs server side to click/run another button/code sever side and expect that the controls values can be used, read, grabbed, or modified. 您不能使用在服务器端运行的计时器和代码来单击/运行另一个按钮/代码服务器端,并期望控件值可以被使用,读取,获取或修改。 This will not work. 这是行不通的。

Remember, when you actually click on the button, YOU START running browser side code, and the VERY first thing that occurs is a full web page post-back to the server. 请记住,当您实际单击该按钮时,就开始运行浏览器端代码,并且发生的第一件事是将完整的网页回发到服务器。 It is then (and only then), that your actual event code written in c# starts to run. 然后(并且只有这样),您才开始运行用c#编写的实际事件代码。

If you just call other code bits, or even a button event, then the code can run, but will run without a full web page post back. 如果您只调用其他代码位,甚至调用按钮事件,那么该代码可以运行,但无需完整的网页回发即可运行。 It is the transfer of the whole web page to the server that has to occur BEFORE any of you code can look at/grab/modify the values of controls. 在您的任何代码都可以查看/抓取/修改控件的值之前,必须将整个网页传输到服务器。

The only way to actually make this work is to setup some JavaScript that runs browser side as a timer, and then when it fires (clicks) the actual button you want to click on the web page (not server side code),then the button click event will: 实际进行这项工作的唯一方法是,将一些运行浏览器端的JavaScript设置为计时器,然后在其触发(单击)您要在网页上单击的实际按钮(而不是服务器端代码)时,再单击该按钮点击事件将:

Do a full web page post back to the server (and transfer the contents of all controls/text boxes etc.). 将完整的网页发布回服务器(并传输所有控件/文本框等的内容)。

Then after the web page transfer has occurred, then the button code for the given click event will now run, and when it does run as noted it will have use of the data in the web page. 然后,在发生网页传输之后,给定的click事件的按钮代码将立即运行,并且当按指定的方式运行时,它将使用网页中的数据。

So, you can't use a timer in c# that will be running server side code, and expect the button code to do work, since you have to first do a full page post back on the client side to transfer the current value(s) in controls. 因此,您不能在将要运行服务器端代码的c#中使用计时器,并且希望按钮代码能够正常工作,因为您必须首先在客户端上做一整页的发布才能传输当前值)。

In your case, the button code via the timer will run, but the web page not been posted (sent back) to the server side. 在您的情况下,通过计时器的按钮代码将运行,但是网页未发布(发送回)到服务器端。 So, your code will run, but without any use of data in the web page. 因此,您的代码将运行,但不会在网页中使用任何数据。

So, say while a user is typing things into the web page, your timer code can run the button event, but the user is still typing away on the web page, and the data on the web page will NOT have yet been transferred to the server. 因此,假设用户在网页上输入内容时,您的计时器代码可以运行button事件,但用户仍在网页上输入内容,并且网页上的数据尚未传输到服务器。 So you need a full post back before that button event written in c# can examine any values on the page. 因此,在用c#编写的按钮事件可以检查页面上的所有值之前,您需要完整的帖子。

As noted, you could write browser side JavaScript and such code is able to view and use and look at any control before a full post back. 如前所述,您可以编写浏览器端JavaScript,这样的代码就可以在完整回发之前查看和使用并查看任何控件。 And such JavaScript code of course can also fire/click that button. 这样的JavaScript代码当然也可以触发/单击该按钮。 So values in/from the web browser have to be transferred to the server side for c# to be able to look at or use current values in the control(s) on that page. 因此,Web浏览器中的/来自Web浏览器的值必须传送到服务器端,以便C#能够查看或使用该页面上控件的当前值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在asp.net页面中按钮点击事件时不显示Div? - Div is not shown when button click event in asp.net page? 用户单击浏览器停止按钮时如何停止服务器端处理(ASP.net C#) - How to stop server side processing when user clicks browser stop button (ASP.net C#) 当用户使用MVC3 C#asp.net单击按钮时显示弹出对话框 - Display popup dialog box when user clicks button using MVC3 C# asp.net ASP.NET MVC3 C# - 当用户单击按钮时,将DB中的记录从1更改为2,反之亦然 - ASP.NET MVC3 C# - when user clicks button, change record in DB from 1 to 2 and viceversa 当用户单击asp.net中的浏览器后退按钮时,如何快速重定向页面 - How to redirect the page quickly when the user clicks the browser back button in asp.net 当用户单击 ASP.NET MVC controller 中的“添加到购物车”按钮时显示消息 - Display message when user clicks on "Add To Cart" button in ASP.NET MVC controller 当按下另一个按钮(asp.net,#C,Web表单)时,更改按钮图像并链接到按钮1中 - Change button Image and link in button 1 , when pressing another button (asp.net, #C, Web form) ASP.NET按钮单击事件未在Web服务器上触发 - asp.net button click event not firing on web server 弹出窗口中放置的Web用户控件的按钮单击事件未触发-Asp.net Webforms - Button Click Event of Web User Control that is placed inside a popup not firing - Asp.net Webforms 用户控件中的ASP.Net按钮的单击事件处理程序不起作用 - Click Event Handler of ASP.Net Button in User Control is not Working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM