gpt4 book ai didi

jquery - 如何将数据发送到代码隐藏的函数

转载 作者:行者123 更新时间:2023-12-01 08:22:47 24 4
gpt4 key购买 nike

我有一个带有两个按钮的模式/对话框,“确定”和取消。当我点击“确定”时,我想将这些数据发送到服务器上的函数。我怎样才能做到这一点?谁能给我一些信息/例子?我相信我必须使用“$.post”,但同样,我将如何将其发送到特定页面的功能?

已更新 ...仍然无法到达函数后面的代码。

$('#dialog').dialog({ 
modal: true,
//autoOpen: false,
bgiframe: false,
closeOnEscape: false,
width: 520,
height: 360,
open: function(event, ui) {
jQuery('.ui-dialog-titlebar-close').hide();
$('#dialog').dialog('option', 'position', 'center'); },
buttons: [
{
text: "Cancel",
click: function() { $(this).dialog("close"); }
},
{
text: "Send",
click: function(){
$.ajax({
type: 'POST',
url: 'test.aspx/GetName',
data: '{name:"' + name + '}',
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false
});
}
}],
draggable: false });
return false;


<System.Web.Services.WebMethod()> _
Public Shared Function GetName(ByVal name As String) As String
Return "Hello " & name & Environment.NewLine & "The Current Time is: " & _
DateTime.Now.ToString()
End Function

最佳答案

您可以使用 jQuery 来实现此目的。我已经实现过几次了,效果非常好。您需要在代码隐藏中定义一个静态 WebMethod,如下所示:

C#

[WebMethod]
public static void SayHello( string name ) {
// say hello
}

VB.NET

<WebMethod()> _
Public Shared Function SayHello(name As String)
' say hello
End Function

现在您可以使用 jQuery 调用它:

$.ajax({
type: "POST",
url: 'yourPage.aspx/SayHello',
data: "{name: '" + aValue + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success : function(data) {
// in case you would use a return value in your webmethod
alert( data.d );
}
});

正如您所看到的,您可以定义您想要的任何页面,它不需要是您的当前页面。只要它包含静态 webmethod,就可以了!

关于jquery - 如何将数据发送到代码隐藏的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6269853/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com