如何使用c#触发按钮单击 [英] how to fire a button click using c#

查看:417
本文介绍了如何使用c#触发按钮单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一个说200行的数据表。我想将200行值插入表中。但我想在4页加载中执行它。然后插入前50,然后我想返回页面,然后在下一个50后暂停,依此类推。所以我被困在这里是如何实现这样的功能。在插入前50行后返回页面时,我必须将它们的状态显示为已插入。所以我想要做的是在插入50后打电话查看页面。但我怎样才能实现呢?



我正在使用c#+ html和虽然不是传统的MVC,但使用它的概念。所以我必须从Model打电话才能刷新页面。



我这样做了

Hi,

I have a data table which have say 200 rows. I want to insert that 200 row values into a table. But i want to execute it in 4 page loads. i.e first 50 are inserted then after that i want to return to the page and then after a pause next 50 and so on. So where i am stuck here is how to implement such function. While returning to the page after inserting the first 50 rows i have to show the status of them as inserted. SO what i am trying to do is to make a call to view page after inserting 50. But how can i achieve it?

I am using c#+html and though not the conventional MVC, but using its concept. so i have to make a call from Model so that page is refreshed.

I have done this

Button btnRefresh = new Button();                
btnRefresh.OnClientClick = "SetMVC('ConfigureMultipleDeviceView','view','DeviceController','frmMVC','0','" + companyId + "','" + companyType + "')";





但我怎么能解雇



but how can i make it fire

推荐答案

首先,请看我对这个问题的评论。



触发按钮点击事件或任何其他事件,这是错误的想法。它并不总是可能的,因为在.NET中,按照设计,除了声明类之外,不能从任何地方调用事件。如果一个类暴露了一些事件调用方法,那么你可以这样做,但它通常没有意义。为什么你认为.NET事件具有这样的限制功能(与常规委托实例相反)。因为它可能是危险的,同时也不是真的需要。



如果你按照自己的要求思考,你会发现,事实上,你不会我想要解雇点击。相反,你想要与用户点击它相同的效果。不是这样吗?所以,做到这一点:

  • 创建一个单独的方法来实现你想要实现的目标。
  • 从事件处理程序中调用此方法;在那个处理程序中什么都不做。
  • 从其他地方调用相同的方法,你想点击一下按钮。


  • 利润!
  • : - )
First, please see my comment to the question.

Firing a button click event, or any other event, is just wrong idea. It is not always possible, because in .NET, by design, an event cannot be invoked from anywhere except declaring class. If a class expose some event-invoking method, then you can do it, but it generally makes no sense. Why to you think .NET events have such a limiting feature (in contrast to "regular" delegate instances). Because at can be dangerous, and, at the same time, not really needed.

If you think at your requirements, you will see that, in fact, you don't want to fire Click. Instead, you want "the same very effect as the user clicked it". Isn't that so? So, do exactly that:
  • Create a separate method implementing what you want to achieve.
  • Call this method from your event handler; do nothing else in that handler.
  • Call the same method from elsewhere, where you wanted to "fire a button click".

  • PROFIT!
  • :-)


你的设计目标不明确:



一方面,你暗示你要自动化插入200的过程行分四步。通过自动化我的意思是插入将自动继续,而无需用户在UI中采取任何直接操作。



另一方面,你暗示用户应该有一些控制是否在完成每个步骤后继续加载



如果您的目标是加载四个步骤,但要暂停并通知用户每个步骤都已成功完成,这是一个你基本上给用户一个状态报告:的情况,为什么用户应该能够点击一个按钮?



但是,如果你想在每个加载步骤完成后给用户一些控制:继续?停在当前步骤?重新开始?然后,这是一个你想要某种界面让用户与之交互的场景。



有一种.NET方法(WinForms,自FrameWork 1.1起)模拟按钮上的Click事件:[ ^ ]。除了在软件开发中自动测试UI之外,我认为使用这种方法并不是一个好的软件设计:为什么不简单地将任何代码在Button内执行,并使其成为Button调用的方法,你可以调用它作为需要?



虽然您可以在每个插入步骤后激活一个计时器,并提供一个允许用户取消或重新启动等的按钮。 ,对于特定的时间间隔,我认为这通常是一个坏主意。
Your design goal here is not clear:

On the one hand, you imply you want to "automate" the process of inserting 200 "rows" in four steps. By "automate" I mean that the insertion will continue automatically without the user taking any direct action in the UI.

On the other hand, you imply the user should have some control over whether to continue loading after each step is complete.

If your goal is to load in four steps, but to pause and notify the user that each step is finished successfully, that is a scenario where you are essentially giving the user a "status report:" why should the user be able to click a Button ?

But, if you want to give the user some control after each loading step is complete: to continue ? to stop at the current step ? to start over ? Then, that's a scenario where you do want some kind of interface for the user to interact with.

There is a .NET method (WinForms, since FrameWork 1.1) for simulating a Click Event on a Button: [^]. Except for automated testing of a UI in software development, I think using this method is not good software design: why not simply take whatever code is executed inside the Button and make it into a method which the Button calls, and which you can call as needed ?

While you could activate a Timer after each insertion step, and present a Button(s) that allowed the user to cancel, or re-start, etc., for a specific interval, I think that's generally a bad idea.


这篇关于如何使用c#触发按钮单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆