gpt4 book ai didi

javascript - 无法将客户端消息传递到服务器端

转载 作者:行者123 更新时间:2023-12-01 02:42:02 25 4
gpt4 key购买 nike

我正在尝试使用以下代码从服务器端发出带有"is"或“否”选项的警报消息

如果我选择“确定”按钮,我可以通过“确定”或“取消”按钮收到警报,我需要将相同的消息(“确定”或“取消”)传递回服务器

这是我的代码

protected void chkboxSelectAll_CheckedChanged(object sender,EventArgs e)
{

if(productNames.Any(s => s.Contains("Virtual")))
{
string str = "you have selected this type of do u want to create a new name for this?";
Page.ClientScript.RegisterStartupScript(typeof(Page), "Confirm", "ConfirmApproval('" + str + "');", true);

string confirmationValue = Request.Form["confirm_valuetype"];

if(confirmationValue == "Yes")
{
// No able to come inside this function
ddlClpFriendlyName.Items.Add(new ListItem("Create New"));
ddlClpFriendlyName.Items.FindByText("Create New");
FriendlynameCle_panel.Visible = true;
}

}
}

这是我的客户端功能

function ConfirmApproval(objMsg) {

var confirm_valuetype = document.createElement("INPUT");
confirm_valuetype.type = "hidden";
confirm_valuetype.name = "confirm_valuetype";
if (confirm(objMsg)) {
alert(objMsg); // i am able to get this alert
confirm_valuetype.value = "Yes";
}
else {
confirm_valuetype.value = "No";
}

document.forms[0].appendChild(confirm_valuetype);
}

我不确定为什么我无法将确认值发送回服务器端代码..任何人都可以帮助解决我犯错的情况,我将非常感激

提前致谢

最佳答案

您可以按照以下方式进行 AJAX 调用:Calling Server Side function from Client Side Script

您必须通过ajax回调到服务器页面,这将解决您的问题

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script>
<script type = "text/javascript">

function ConfirmApproval(objMsg) {

var confirm_valuetype = document.createElement("INPUT");
confirm_valuetype.type = "hidden";
confirm_valuetype.name = "confirm_valuetype";
if (confirm(objMsg)) {
alert(objMsg); // i am able to get this alert
confirm_valuetype.value = "Yes";
}
else {
confirm_valuetype.value = "No";
}

callServer(confirm_valuetype);
}

function callServer(val) {
$.ajax({
type: "POST",
url: "PAgeName.aspx/AlertReply",
data: '{name: "' + val + '" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function(response) {
alert(response.d);
}
});
}
function OnSuccess(response) {
alert(response.d);
}
</script>


Server code

[System.Web.Services.WebMethod]
public static string AlertReply(string name)
{
///get value and do processing
}
<小时/>

我认为问题是

   string str = "you have selected this type of  do u want to create a new  name for this?";
Page.ClientScript.RegisterStartupScript(typeof(Page), "Confirm", "ConfirmApproval('" + str + "');", true);

使用这一行,一旦整个页面在客户端加载完毕,代码就会被执行。这意味着隐藏字段是在 C# 代码执行完成后创建的。

这意味着你这部分代码

  string confirmationValue = Request.Form["confirm_valuetype"];

if(confirmationValue == "Yes")
{
// No able to come inside this function
ddlClpFriendlyName.Items.Add(new ListItem("Create New"));
ddlClpFriendlyName.Items.FindByText("Create New");
FriendlynameCle_panel.Visible = true;
}

在 html 页面中创建文件之前执行。

您可以在 html 页面加载后访问字段,然后单击按钮。

关于javascript - 无法将客户端消息传递到服务器端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47505407/

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