Jump to content

How To Auto Submit An External Javascript Button From C# Behind


bibib

Recommended Posts

Hi,I will try to make my question as simple as possible.How can I submit the following button from C# Behind in ASP.NET?(Note: The following button uses 2 Session Variables that contain information about a selected product and then passes those values to an external Javascript which is my shopping cart - so to summarize this button adds the product to an external shopping cart. I don't wish to manually press it but to automatically press it when an Argument in my C# code is TRUE):<input id="product_name" value='<%Response.Write(Session["Selected_add_to_cart_name"]);%>' class="product-title" type="hidden"><input id="product_price" value='<%Response.Write(Session["Selected_add_to_cart_price"]);%>' class="product-price" type="hidden" ><div title="Add to cart" role="button" tabindex="0" class="add-button" type="hidden">...the external Javascript<script id='cart-script' type='text/javascript' src='https://xx integration='jscart-wizard' post-cart-to-sandbox='false' currency='GBP'></script>What I want to do is automatically call this Javascript from C# behindPlease I need this urgently, any help will be very much appreciated.Thank you

Link to comment
Share on other sites

Because the C# is being executed on the server, it doesn't really make any sense (nor is it possible) to execute client-side script in a code behind.However, you can write out code in the response that executes the client-side script. Option A:

if(myValueIsTrue){	var script = "<script type=\"text/javascript\">alert("howdyado?");</script>	ClientScript.RegisterClientScriptBlock(typeof(string), "someRelativelyUselessKey", script);}

Option B:

<script type="text/javascript"><asp:Literal ID="SomeScriptLiteral" runat="server" /></script>

if(myValueIsTrue){	SomeScriptLiteral.Text = "alert(\"howdyado?\");";}

Option C:

<script type="text/javascript"><%=MyScriptToExecute%></script>

public string MyScriptToExecute = string.Empty;if(myValueIsTrue){	MyScriptToExecute = "alert(\"howdyado?\");";}

Link to comment
Share on other sites

I tried your method and I placed the code that generates the Button in the variable as you pointed out to meThis is the button that I placed in the 'script' variable:<input id="product_name" value='<%Response.Write(Session["Selected_add_to_cart_name"]);%>' class="product-title" type="hidden"><input id="product_price" value='<%Response.Write(Session["Selected_add_to_cart_price"]);%>' class="product-price" type="hidden" ><div title="Add to cart" role="button" tabindex="0" class="add-button" type="hidden">However all this did was to Display the button when a statement is TRUE but not actually execute the button.What I need is I need to convert this button into a code that will submit itself when a statement is TRUE. So I don't want to make this button visible on the website, but instead I want to submit it automatically passing the values of my Session Variables as stated aboveIf you could please point out to me how to do this I would greatly appreciate it :)..this has taken days from me

Because the C# is being executed on the server, it doesn't really make any sense (nor is it possible) to execute client-side script in a code behind.However, you can write out code in the response that executes the client-side script. Option A:
if(myValueIsTrue){	var script = "<script type=\"text/javascript\">alert("howdyado?");</script>	ClientScript.RegisterClientScriptBlock(typeof(string), "someRelativelyUselessKey", script);}

Option B:

<script type="text/javascript"><asp:Literal ID="SomeScriptLiteral" runat="server" /></script>

if(myValueIsTrue){	SomeScriptLiteral.Text = "alert(\"howdyado?\");";}

Option C:

<script type="text/javascript"><%=MyScriptToExecute%></script>

public string MyScriptToExecute = string.Empty;if(myValueIsTrue){	MyScriptToExecute = "alert(\"howdyado?\");";}

Link to comment
Share on other sites

  • 2 weeks later...

Place a button on the page and hide it with CSS and create a javascript function that will click your button for you.CSS

.hidden { display: none; }

ASPX

<asp:Button Text="My Button" ID="MyButton" runat="server" CssClass="hidden" /><!--rest of your code here--><script type="text/javascript">  function clickMyButton() {	var button = document.getElementById('<%= MyButton.ClientID %>');	button.click();  }</script>

Then in your code behind you can do thisASPX.CS

public void DoSomething() {  var script = "<script type=\"text/javascript\">clickMyButton();</script>  ClientScript.RegisterClientScriptBlock(typeof(string), "someRelativelyUselessKey", script);}

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...