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

时间:2017-11-27 06:57:13

标签: javascript c# jquery asp.net .net

我正尝试使用以下代码

从服务器端使用是或否选项引发警报消息

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

这是我背后的代码

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);
    }

我不知道为什么我无法将确认值发送回服务器端代码。 如果我出错了会非常感谢我

,请帮忙

提前致谢

2 个答案:

答案 0 :(得分:1)

您可以按照以下方式进行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中加载页面后访问字段,而不是执行按钮单击。

答案 1 :(得分:1)

将逻辑拆分为两种不同的方法。第一个(chkboxSelectAll_CheckedChanged)将包含此部分:

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")
{
    ddlClpFriendlyName.Items.Add(new ListItem("Create New"));
    ddlClpFriendlyName.Items.FindByText("Create New");
    FriendlynameCle_panel.Visible = true;
}

我会解释。简而言之,客户端(浏览器)和服务器是两个独立的断开连接的世界。看起来你错过了这一点。

这是实际发生的事情:

  1. 用户点击复选框
  2. 客户端向服务器发送请求,并指示复选框值已更改
  3. 服务器处理请求并调用chkboxSelectAll_CheckedChanged(当然在服务器上运行)
  4. chkboxSelectAll_CheckedChanged要求在响应中包含一个脚本,该脚本将在客户端上执行ConfirmApproval(...)
  5. 服务器尝试检查用户对确认的回答.... 这里有差距 - 这里不可能发生(按照为什么的后续步骤进行操作)
  6. chkboxSelectAll_CheckedChanged方法退出(这很重要
  7. 服务器完成请求的处理,并将响应发送给客户端。
  8. 客户端收到响应并执行脚本(由chkboxSelectAll_CheckedChanged包含)。
  9. 脚本调用ConfirmApproval(...),当然在客户端上执行
  10. 用户回答确认
  11. ConfirmApproval对浏览器的DOM进行了一些更新。
  12. 现在怎样? chkboxSelectAll_CheckedChanged在很久以前就已经结束了它的执行(就网络服务器性能而言)。
  13. 因此,为了让服务器处理用户的响应,您必须从客户端向服务器发出附加请求,并在服务器上处理它:

    1. 客户端向服务器发送请求(可能是AJAX),包含用户的回答
    2. 服务器根据用户的回答处理请求并完成处理。