如何检查ASP.NET按钮是否被点击或没有在页面加载 [英] How to check whether ASP.NET button is clicked or not on page load
问题描述
如何检查是否某个特殊按钮被点击或者不ASP.NET?
How can I check whether a particular button was clicked or not in ASP.NET?
我想我需要执行对的Page_Load
一些操作。这不应该是进入到 Button_Click
事件找到。有什么办法,我可以找到它被点击或没有在客户端,并把它的Page_Load
?
I think I need to perform some operation on Page_Load
. This shouldn't be entering to Button_Click
event to find. Is there any way that I can find where it was clicked or not on Client Side and take it to Page_Load
?
推荐答案
背景:基本上 __ EVENTTARGET
和 __ EVENTARGUMENT
,这两个隐藏控件添加到HTML源代码,当过任何的AutoPostBack属性设置为true的任何网络的控制。
Background: Basically __EVENTTARGET
and __EVENTARGUMENT
, These two Hidden controls are added to the HTML source, when ever any autopostback attribute is set to true for any of the web control.
的 __ EVENTTARGET
隐变量会告诉服务器,它实际控制做服务器端事件触发,使这个框架可以触发该控制服务器端事件。
The __EVENTTARGET
hidden variable will tell the server ,which control actually does the server side event firing so that the framework can fire the server side event for that control.
__ EVENTARGUMENT
变量用于如果需要由应用程序,它可以在服务器进行访问,以提供额外的事件信息。
The __ EVENTARGUMENT
variable is used to provide additional event information if needed by the application, which can be accessed in the server.
因此,我们可以很容易地得到控制使用造成回传: Request.Params.Get(__ EVENTTARGET);
So we can easily get the control causing postback using:Request.Params.Get("__EVENTTARGET");
问题:的
PROBLEM:
的方法: Request.Params.Get(__ EVENTTARGET);
将为的CheckBox $ C工作$ C>,
和 DropDownLists
, LinkButtons
,等等。但这并不Button控件,如<$ C工作$ C>按钮 ImageButtons
的
The method: Request.Params.Get("__EVENTTARGET");
will work for CheckBoxes
, DropDownLists
, LinkButtons
, etc.. but this does not work for Button controls such as Buttons
and ImageButtons
的按钮
控制和的ImageButton
控制不叫 __ doPostBack
功能。正因为如此,在 _EVENTTARGET
将永远是空的。然而,其他控件使用javascript函数 __ doPostBack
触发回发。
The Button
controls and ImageButton
controls does not call the __doPostBack
function. Because of this, the _EVENTTARGET
will always be empty. However, other controls uses javascript function __doPostBack
to trigger postback.
所以,我会建议如下做一些事情。一个的OnClientClick
属性添加到按钮。此外,在您的标记,其价值将包含实际按钮导致回发定义hiddenField。
So, I will suggest to do something as below. Add an OnClientClick
property to the buttons. Also, define a hiddenField in your Markup, whose value will contain the actual button causing postback.
<asp:Button ID="Button1" runat="server" Text="Button"
OnClientClick = "SetSource(this.id)" />
<asp:ImageButton ID="ImageButton1" runat="server"
OnClientClick = "SetSource(this.id)" />
<asp:HiddenField ID="hidSourceID" runat="server" />
在按钮的
和的OnClientClick
属性的ImageButton
调用的SetSource
JavaScript函数
On the OnClientClick
property of the Button
and ImageButton
Call the SetSource
JavaScript function
<script type = "text/javascript">
function SetSource(SourceID)
{
var hidSourceID =
document.getElementById("<%=hidSourceID.ClientID%>");
hidSourceID.value = SourceID;
}
</script>
在这里起,你可以非常容易地查看您的Page_Load
去哪家控制造成回发:
if (IsPostBack)
{
string CtrlName;
CtrlName=hidSourceID.Value;
}
这篇关于如何检查ASP.NET按钮是否被点击或没有在页面加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!