“javascript:_doPostBack('Gridview','Edit$0')”是什么意思意思是,仍然会发生完整的回发吗?
JavaScript 用于纯粹想要在客户端执行某些操作,或者想要以回发无法处理的方式向服务器发送某些内容的情况。
但在 Visual Studio 2008 控件 ASP.NET C# 中,我发现当页面在浏览器中显示时,控件(即 GridView、FormView 和 LINKBUTTON (!))都会显示此 javascript:thing
光标悬停在它们上。为什么?
回发仍然发生。甚至链接按钮也有这个 JavaScript 东西,每当你点击它时,就会发生完整的回发。
更改 label.text
等也在页面加载事件中!
- 那么为什么是 JavaScript 呢?为什么不是一个简单的按钮呢?为什么选择链接按钮?
JavaScript is used where one want to do something on the client side purely, or wants to send something to the server in a manner that postback does not handle.
But in Visual Studio 2008 controls ASP.NET C# I have seen that when the page is displayed in the browser the controls, namely GridView, FormView, and LINKBUTTON (!) all show this javascript:thing
when the cursor is hovered on them. Why?
Post back still occurs. Even the linkbutton has this JavaScript thing and whenever you click on it, full post back occurs.
Changing label.text
, etc. too is on the pageload event!
- so why the JavaScript? Why not a simple button? Why linkbutton?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这种情况下,JavaScript 调用可用于向服务器发送附加数据,例如,为网格保存一些客户端数据(例如调整大小的列的宽度或类似的数据)。
服务器端框架使用这种方法来允许服务器端人员生成所有客户端代码。这是一种快速而肮脏的解决方案(与组织良好、不引人注目的 JavaScript 相比)。
In this case JavaScript calls could be used to send additional data to the server, e.g. save some client data for the grid (like the width of resized columns or something like that).
Server-side frameworks use this approach to allow server-side guys to generate all the client-side code. It's a kind of quick'n'dirty solutions (comparing with well-organized unobtrusive JavaScript).
ASP.NET 是无状态的。也就是说,每次请求一个页面时,服务器实际上都会构建整个页面及其控件和页面。状态,然后响应请求。然后,它呈现适当的 HTML 标记作为对请求的响应。
对于任何控件,如果 autopostback 属性设置为 true,则当该控件导致回发(例如单击链接按钮)时,页面将回发到服务器。
ASP.NET 如何回发页面?
它使用名为 _doPostBack() 的 JavaScript 函数来完成此操作。功能是 -
该功能用于将表单提交回服务器。 _doPostBack 通过使用隐藏变量 __EVENTTARGET 和 __EVENTARGUMENT 接受参数 - 事件目标和事件参数。这告诉服务器哪个控件导致了回发,并将适当的参数传递给服务器。
如果您的 aspx 页面中有此代码 -
相应的生成标记将是 -
因此,假设您单击链接按钮,页面将由 __doPostBack() 函数回发。然后,在服务器上重新创建页面,并在页面上显示相应的控制状态。为了获取页面上每个控件的状态,使用了像 viewstate 这样的机制。加载页面后,服务器将计算并呈现响应标记。
ASP.NET is stateless. That is, every time a page is requested, the server actually constructs the entire page and its controls & state and then responds to the request. It then renders the appropriate HTML markup as response to the request.
For any control, if there is the autopostback property set to true then a page is postback to the server if the control causes a postback (like clicking on a link button).
How does ASP.NET post back the page ?
It does it using a javascript function called _doPostBack(). The function is -
This function is used to submit the form back to the server. _doPostBack accepts arguments - event target and event arguments by using hidden variables __EVENTTARGET and __EVENTARGUMENT. This tells the server which control caused the postback and also passes appropriate arguments to the server.
if you have this code in your aspx page -
The corresponding generated markup will be -
So, say you click on a link button, the page is postback by the __doPostBack() function. Then, the page is recreated at server with the respective control state on the page. To get the state of each control on the page mechanisms like viewstate are used. Once the page is loaded, the server computes and renders the response markup.