如何在服务器端识别按钮单击事件 [英] How the server side identifies button click event

查看:171
本文介绍了如何在服务器端识别按钮单击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我已在asp.net点击按钮的一个疑问,我把按钮控件与OnClick事件类似,并在.aspx页面中其他控件(按钮,文本框)。当我运行该页面,该按钮将显示在喜欢这里的浏览器的网页源代码,它不会显示onclick事件,该页面那么如何调用服务器端的按钮点击,服务器端如何识别按钮导致提交以及如何当前页移动到服务器侧

hi i have one doubt of button click in asp.net, i have placed the button control with the OnClick event like and some other controls(buttons, textboxes) in .aspx page. When i run the page, the button displays in the page source of the browser like here, it does not display the onclick event, then how the page calls the button click on the server side, how the server side identify which button cause the submit, and how this page moves to the server side.

推荐答案

如果它是关于< ASP:按钮/> ,该控件呈现默认的输入中的类型=提交的,提交表单,使用浏览器的默认机制。 ASP.NET的标识哪个按钮被点击,通过检查发布的​​值。当浏览器提交它在POST写一个形式要求的名称和点击按钮的值与其他输入的名称和值中,不包括其他提交的投入。因此,一个单一的输入提交的名称被发送到服务器,并以这种方式哪个按钮被点击ASP.NET的检查。

If it is about <asp:Button />, this control renders by default an input of type="submit", which submits the form, using the browser's default mechanism. ASP .Net identifies which button was clicked, by checking the posted values. When a browser submits a form it writes in the POST request the name and value of the clicked button among with the names and values of the other inputs, excluding the other submit inputs. So the name of one single submit input is sent to the server and in this way ASP .Net checks which button was clicked.

这服务器控件也有物业的 UseSubmitBehavior ,如果这是设置为假时,将生成类型按钮的输入。由于按钮不提交表单,ASP净生成一些JavaScript将做的工作。

This server control has also the property UseSubmitBehavior and if this is set to false it will generate an input of type button. Since buttons don't submit the forms, ASP .Net generates some javascript which will do the job

<script type="text/javascript">    
//<![CDATA[    
var theForm = document.forms['form1'];    
if (!theForm) {    
    theForm = document.form1;    
}

function __doPostBack(eventTarget, eventArgument) {    
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {    
        theForm.__EVENTTARGET.value = eventTarget;    
        theForm.__EVENTARGUMENT.value = eventArgument;    
        theForm.submit();    
    }    
}    
//]]>    
</script>

通知 _ EVENTTARGET 的_EVENTARGUMENT 字段。这些字段设置这样的ASP.NET就会知道被点击客户端的按钮。

Notice _EVENTTARGET and _EVENTARGUMENT fields. These fields are set so ASP .Net will know which button was clicked on client.

<input type="button" 
      name="rptMain$ctl02$DeleteButton" 
      value="Delete" onclick="__doPostBack('rptMain$ctl02$DeleteButton','')"         
      id="rptMain_ctl02_DeleteButton" />

注意传递到__doPostBack函数的参数。输入的名称被传递并设置为EVENTTARGET。 EVENTTARGET的价值将通过ASP.NET的读取和在此基础上将会触发点击的事件&LT; ASP:按钮/&GT; 控制

EVENTARGUMENT使用由ItemCommand事件,通常放置在数据绑定控件并包含有关该操作的具体数据,去处理该事件时将在后面使用。这个数据可以是例如行的ID在数据库表中。

EVENTARGUMENT is used by ItemCommand events, usually placed in the DataBound controls and contains specific data about that action, going to be used later when handling the event. This data can be for example an ID of row in a database table.

这篇关于如何在服务器端识别按钮单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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