简体   繁体   English

ASP.net Javascript Postback

[英]ASP.net Javascript Postback

I'm learning javacript, and I'm about at the point where I want to start doing some asychronous stuff. 我正在学习javacript,而我正想要开始做一些非同步的事情。 Here is the gist of my quest: 这是我的追求的要点:

I have HTML code that looks like this: 我有HTML代码,如下所示:

<a id='nine' onclick="SendToServer('nine');">Nine</a>

I then want a way to, in Javascript, send back to the server that a#nine was clicked. 然后,我想要一种方法,在Javascript中,发送回服务器,点击#9。

function SendToServer(id)
{
   //Send that a#nine was clicked back to server
}

I then want some sort of handler function in c# to update an entry in a database. 然后,我想在c#中使用某种处理函数来更新数据库中的条目。

public void GetMessageFromJSHandler(int id)
{
     Item[x][y] = id;
}         

I don't want the page to reload, I don't want anything to change on the page, and I want to do this in straight javascript so that I can learn how it works. 我不希望页面重新加载,我不希望在页面上进行任何更改,我想在直接的javascript中执行此操作,以便我可以了解它是如何工作的。

This is a GREAT resource: http://msdn.microsoft.com/en-us/library/ms178210.aspx I ended up altering this example for my needs. 这是一个很好的资源: http//msdn.microsoft.com/en-us/library/ms178210.aspx我最终根据我的需要改变了这个例子。 All Js and very straightforward. 所有Js都非常直截了当。

Well, inside the context of your question, you're on the right track. 那么,在你的问题的背景下,你走在正确的轨道上。 You can use this example: http://www.devx.com/DevX/Tip/17500 to see how to populate your SendToServer function 您可以使用此示例: http//www.devx.com/DevX/Tip/17500查看如何填充SendToServer函数

GetMessageFromJSHandler would probably be called by the ProcessRequest method of an implementer of IHttpHandler, at least in the easiest scenario. GetMessageFromJSHandler可能会被IHttpHandler的实现者的ProcessRequest方法调用,至少在最简单的场景中。

but that way of doing things is really for academic purposes, learning the plumbing as it were. 但这种做事方式实际上是出于学术目的,学习管道。 In a production environment, you're much better off using a framework. 在生产环境中,使用框架会更好。 My personal crack of choices is jquery on the client and ASP.NET MVC on the server, but if you like "regular" ASP.NET (aka webforms) then check out the MS Ajax libraries to see some easier ways of doign things in a scalable & maintainable fashion. 我个人的选择是客户端上的jquery和服务器上的ASP.NET MVC,但是如果你喜欢“常规”ASP.NET(也就是webforms),那么请查看MS Ajax库以查看更简单的方法。可扩展和可维护的时尚。

You can mark your page methods with a WebMethod attribute to create stubs for them on the client-side. 您可以使用WebMethod属性标记页面方法,以便在客户端为它们创建存根。 Here is an example . 这是一个例子 Another thing you could do is make your page method static and call it using jQuery's ajax() function. 你可以做的另一件事是使你的页面方法静态,并使用jQuery的ajax()函数调用它。 Here is how this way works . 以下是这种方式的工作原理

jQuery: jQuery的:

function SendToServer(id)
{
  $.ajax({
                        url: "/GetMessageFromJSHandler",
                        type: "POST",
                        data: { 'id': id }
          }):

} }

function SendToServer(id){ var pageUrl = "SamplePage.aspx?callback=true&ItemId=" +item if (window.XMLHttpRequest) { var xmlRequest = new XMLHttpRequest(); function SendToServer(id){var pageUrl =“SamplePage.aspx?callback = true&ItemId =”+ item if(window.XMLHttpRequest){var xmlRequest = new XMLHttpRequest(); } else { var xmlRequest = new ActiveXObject("Microsoft.XMLHTTP"); } else {var xmlRequest = new ActiveXObject(“Microsoft.XMLHTTP”); } xmlRequest.open("POST", pageUrl, false); } xmlRequest.open(“POST”,pageUrl,false); xmlRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); xmlRequest.setRequestHeader(“Content-Type”,“application / x-www-form-urlencoded”); xmlRequest.send(null); xmlRequest.send(NULL); } }

On pageLoad you just check the callback flag and call your function



 if(Request.QueryString["callback"].ToString() == "true")
   {
    string id = Request.QueryString["ItemId"].ToString()
    GetMessageFromJSHandler(int id);
   }

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

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