Parameter value for javascript variable from code (C #)

I understand the difference between client and server scenarios. I have a javascript function and a 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 the default value is false. Here is an example.

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

Now the question is here, when in C # (server side) I have to set needToConfirm to true with an if-statement , but not necessarily to Page_Load :

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

Thanks.

UPDATE

I am using .NET 2.0 Classic and WebForms

+7
source share
6 answers

in the code behind:

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

on the client side:

 function urFunction(urParam) { //do what u want here //use urParam } 
+7
source

You can use hidden input and then set this input to true or false on 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(); 
+5
source

If I understand this correctly, you can register the client script, as 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); } 

This would create a script on the page by setting the value needToConfirm in Javascript.

+2
source

Based on your update talking about .NET 2.0, you can set the javascript variable:

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

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

+1
source

Just for reference. Here is 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.registerstartupupscript(v=vs.110).aspx

+1
source

Does maby help?

 <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);" /> 
0
source

All Articles