8

I understand the difference between Client-side and Server-side scripting. I have a javascript function and variable in my MasterPage:

<script language="JavaScript" type="text/javascript">
    var needToConfirm = false;
          window.onbeforeunload = confirmExit;
          function confirmExit()
          {
            if (needToConfirm)
            {
              needToConfirm = false;
              return "Currently in edit mode. If you leave the page now then you will lose unsaved changes."
            }
          }
</script>

Given the fact that on my ASP.NET (Client-side) I can change the value of my needToConfirm variable to true onClientClick but by default it is false. Here's an example.

 <asp:Button ID="btnEdit" runat="server" Text="  Edit  " onclick="btnEdit_Click" OnClientClick="needToConfirm = true;" />

Now the question here is when on the C# (Server-side) I have to set the needToConfirm to true under an if-statement but not necessarily on Page_Load:

private void SetDefault()

    if (Session[def.ID_CUST] != null)
    {
          //I want to change the variable value here
    }
}

Thanks.

UPDATE

I'm using .NET 2.0 Classic and WebForms

1
  • 2
    Direct call of script from server-side are impossible. However you can use of ClientScript.RegisterStartup() script. Commented May 10, 2013 at 3:24

6 Answers 6

8

in code behind:

ScriptManager.RegisterStartupScript(this, this.GetType(), "", "urFunction('urValHere');", true);

on client-side:

function urFunction(urParam) {
        //do what u want here
        //use urParam
    }
Sign up to request clarification or add additional context in comments.

Comments

8

You can use a hidden input and then set this input to true or false from the server side.

On the client side:

<input type="hidden" id="hdnConfirm" runat="server" value="false"/>

Then on the server side:

 if (Session[def.ID_CUST] != null)
    {
          //I want to change the variable value here
           hdnConfirm.Value = "true";
    }

Then on the client side:

var needToConfirm = $('#hdnConfirm').val();

2 Comments

can you elaborate it more. or put some details (code sample)?
var needToConfirm = false; use this. var needToConfirm = $('#hdnValue').val(); just set the value of hdnValue in codebehind
2

If I understand this correctly, you could register a client script like in the example at http://msdn.microsoft.com/en-us/library/z9h4dk8y.aspx.

ClientScriptManager cs = Page.ClientScript;
if (!cs.IsStartupScriptRegistered(this.GetType(), "EditMode")) {
    cs.RegisterStartupScript(this.GetType(), "EditMode", "needToConfirm = true;", true);
}

That would write a script to the page setting the value of needToConfirm in Javascript.

4 Comments

It worked for the Page_Load but how about when in a separate method in C#?
Should work the say way in any method that inherits System.Web.UI.Page. Button clicks, postbacks, etc, etc.
yes but I don't actually need to trigger the variable change on Button Clicks and postbacks. Thanks though.
Right, I was just using those as possible methods that could be used. You could use them in any method within the class.
1

Based on your update saying it's .NET 2.0, this is how you can set a javascript variable:

Page.RegisterStartupScript("SetVar", "var needToConfirm = true;");

http://msdn.microsoft.com/en-us/library/system.web.ui.page.registerstartupscript(v=vs.80).aspx

Comments

1

Just for reference. Here is the 4.5 way to do something like this:

 // Define the name and type of the client scripts on the page.
 const String csname1 = "MyScriptName";
 Type cstype = this.GetType();

 // Get a ClientScriptManager reference from the Page class.
 ClientScriptManager cs = Page.ClientScript;

 // Check to see if the startup script is already registered.
 if (!cs.IsStartupScriptRegistered(cstype, csname1))
 {
      StringBuilder cstext1 = new StringBuilder();
      cstext1.Append("<script> var myVariable = true; </");
      cstext1.Append("script>");

      cs.RegisterStartupScript(cstype, csname1, cstext1.ToString());
  }

http://msdn.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.registerstartupscript(v=vs.110).aspx

Comments

1

Maby this helps?

<script type="text/javascript">
    function test(confirm){
        var needToConfirm = confirm;
        window.onbeforeunload = confirmExit;
        function confirmExit() {
            if (needToConfirm) {
                needToConfirm = false;
                return "Currently in edit mode. If you leave the page now then you will lose unsaved changes."
            }
        }
    }
</script>



<asp:Button ID="btnEdit" runat="server" Text="Edit" onclick="btnEdit_Click" OnClientClick="javascript:test(true);" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.