简体   繁体   中英

Not able to pass client side message to server side

I am trying to raise the alert message with yes or no option from sever side with the below code

I am able to get the alert with OK or cancel button if i select ok button i need to pass same message(OK or Cancel) back to server

this is my code behind

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

    }
  }

this is my client side function

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

I am not sure why i am not able to send confirm value back to server side code .. Could any please help on this scenario where i am getting wrong that would be very grateful to me

thanks in advance

You can follow this for AJAX call : Calling Server Side function from Client Side Script

you have to do call back via ajax to server page , that will resolve your issue

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

I think problem is

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

with this line , code get executed once your full page get loaded at client side. That means hidden field got created after your C# code execution done.

That means you this part of code

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

got executed before filed got created in html page.

You can access field after page got loaded fist in html, and than you perform button click.

Split you logic into two different methods. First one ( chkboxSelectAll_CheckedChanged ) will contain this part:

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

And the second one (a new method you have to write) will contain this:

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

I'll explain. To put it simply, the client (browser) and the server are two separate disconnected worlds. It looks like you are missing this point.

This is what actually happens:

  1. User clicks checkbox
  2. Client sends request to server, with indication that checkbox value has changed
  3. Server handles the request and calls chkboxSelectAll_CheckedChanged (which of course runs on the server)
  4. chkboxSelectAll_CheckedChanged asks to include a script in the response, which would execute ConfirmApproval(...) on the client.
  5. The server attempts to check the user's answer to the confirmation.... here is the gap -- it cannot happen here (follow through the next steps for why )
  6. chkboxSelectAll_CheckedChanged method exits ( this is important )
  7. The server completes processing of the request and the response is sent to the client.
  8. The client receives the response and executes the script (which was included by chkboxSelectAll_CheckedChanged ).
  9. The script calls ConfirmApproval(...) , which of course executes on the client
  10. The user answers the confirmation
  11. ConfirmApproval makes some updates to browser's DOM.
  12. Now what? The chkboxSelectAll_CheckedChanged ended its execution ages ago (speaking in terms of web server performance).

So in order for the server to process user's response, you have to make an additional request from client to server, and handle it on the server:

  1. Client sends request to server (probably AJAX), containing user's answer
  2. The server handles the request and completes processing based on user's answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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