简体   繁体   English

如何在不丢失 PostBack 功能的情况下将我自己的 JavaScript 链接到客户端

[英]How do I chain my own JavaScript into the client side without losing PostBack functionality

So I was reading these Asp.Net interview questions at Scott Hanselman's blog and I came across this question.所以我在 Scott Hanselman 的博客上阅读这些 Asp.Net面试问题,我遇到了这个问题。 Can anyone shed some light of what he's talking about.任何人都可以阐明他在说什么。

<asp:LinkButton ID="lbEdit" CssClass="button" 
    OnClientClick="javascript:alert('do something')" 
    onclick="OnEdit"  runat="server">Edit</asp:LinkButton>

The OnClientClick attribute means you can add some JavaScript without losing PostBack functionality would be my answer in the interview. OnClientClick属性意味着您可以添加一些 JavaScript 而不会丢失 PostBack 功能,这将是我在采访中的回答。

Explain how PostBacks work解释回传如何工作

Postbacks are an abstraction on top of web protocols which emulate stateful behavior over a stateless protocol.回发是 Web 协议之上的抽象,它通过无状态协议模拟有状态行为。

, on both the client-side , 在客户端

On the client side, postbacks are achieved by javascript calls and hidden fields which store the state of all controls on the page.在客户端,回发是通过 javascript 调用和隐藏字段实现的,这些字段存储页面上所有控件的状态。

and server-side.和服务器端。

The server side goes through a life cycle of events, part of that life cycle is the hydration of the viewstate to maintain the state of all the controls on the page, and the raising of events based on the paramaters that were passed into the __doPostBack call on the client side服务器端经历一个事件的生命周期,该生命周期的一部分是视图状态的水化以维护页面上所有控件的状态,以及基于传入 __doPostBack 调用的参数引发事件在客户端

How do I chain my own JavaScript into the client side without losing PostBack functionality?如何在不丢失 PostBack 功能的情况下将我自己的 JavaScript 链接到客户端?

Depends on what is required.取决于需要什么。 The easiest way that works 99% of the time is to use asp:hiddenfield to communicate between client and server side.在 99% 的情况下都有效的最简单方法是使用 asp:hiddenfield 在客户端和服务器端之间进行通信。 For edge cases, you want to get into Exenders and manipulating viewstate/controlstate/clientstate in javascript through the MS ajax APIs.对于边缘情况,您希望通过 MS ajax API 进入 Exenders 并在 javascript 中操作 viewstate/controlstate/clientstate。 This is pretty painful with a huge learning curve and a lot of gotchas, generally using hidden fields and manually calling __doPostBack is enough这对于巨大的学习曲线和很多陷阱来说非常痛苦,通常使用隐藏字段并手动调用 __doPostBack 就足够了


That is how I would answer that bullet point.这就是我将如何回答该要点。 For more information on __doPostBack, a quick google will give you plenty of results (for the lazy, this is the first hit http://aspalliance.com/895 )有关 __doPostBack 的更多信息,快速 google 将为您提供大量结果(对于懒惰的人,这是第一个点击http://aspalliance.com/895

Do you mean something like this:你的意思是这样的:

in your code behind:在你后面的代码中:

protected string GetPostBack()
{
    return ClientScript.GetPostBackEventReference(this, null);
}

and in your aspx:并在您的 aspx 中:

<a href="javascript:<%=GetPostBack() %>">Click here to postback</a>

I think what he's asking here is how you wire up javascript functions to work hand in hand with your ASP.NET postback functionality.我认为他在这里问的是如何连接 javascript 函数以与 ASP.NET 回发功能协同工作。

ie How can I trigger a control's event using my own JavaScript?即如何使用我自己的 JavaScript 触发控件的事件?

The ASP.NET class library contains a ClientScript class - Found in the System.Web.UI.Page class - which enables you to programmatically add JavaScript to your ASP.NET page. ASP.NET 类库包含一个ClientScript类 - 在System.Web.UI.Page类中找到 - 它使您能够以编程方式将 JavaScript 添加到您的 ASP.NET 页面。

This contains a method called GetPostBackEventReference which will generate the __doPostBack script ASP.NET utilises to trigger events wired up to your web controls.这包含一个名为GetPostBackEventReference的方法,它将生成__doPostBack脚本 ASP.NET 用来触发连接到您的 Web 控件的事件。

Hope that makes sense希望这是有道理的

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM