0

I'm in need of a synchronous call from code-behind ASP.NET (C#) to a Javascript function.

I've tried the ScriptManager.Registerclient, but as this is asynchronous this is no good. I'm in need of some testing in javascript before returning a value (updating an UpdatePanel) to check whether or not the operation is valid.

For a short code example:

myButton_click(object sender, EventArgs args)
{
  <callJavascriptSynchronous>
  if (MyHiddenField.Value == "true")
     //Do
else
   //Don't
}

MyHiddenField is within an UpdatePanel. For the javascript function:

function javascriptFoo(myInteger)
{
  var returnValue = window.external.TestInteger(myInteger);
  document.getElementById('<%=MyHiddenField.ClientID%>').value = returnValue;
}

The UpdatePanel looks like this:

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
    <asp:HiddenField ID="MyHiddenField" runat="server" ClientIDMode="Static" Value="" Visible="false"/>
</ContentTemplate>

Any suggestions on the synchronous call?

Thanks!

Edit: Added full code:

MyPage.aspx:

function javascriptFoo(myInteger)
{
  var returnValue = window.external.TestInteger(myInteger);
  document.getElementById('<%=MyHiddenField.ClientID%>').value = returnValue;
}

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
    <asp:HiddenField ID="MyHiddenField" runat="server" ClientIDMode="Static" Value="" Visible="false"/>
</ContentTemplate>

<asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand">

MyPage.aspx.cs:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
   ScriptManager.RegisterStartupScript(UpdatePanelContext, UpdatePanelContext.GetType(), "AlertTest", "AlertTest()", true);

   string sVal = HiddenFieldContext.Value;

   if (sVal == "true") //do
   else //don't
}

1 Answer 1

1

When you're in your button's click-event, you've already left the frontend and cannot jump back for a function call. Execute the function first, clientside, upon the button-click (read about the OnClientClick event), then return your result with the postback.

This link addresses the same issue.

Sign up to request clarification or add additional context in comments.

11 Comments

Postback will do me no good, as this is happening within an operation. I'm actually doing this within a GridView.RowCommand event.
Where is myButton_click?
Where it is? <asp:GridView ID="GridView1" OnRowCommand="GridView1_RowCommand"> I'm in need of testing the data within the Gridview_rowCommand event. I am able to do this, and it works, but the call is not synchronous.
Is myButton_click in your frontend JavaScript or in your backend C#?
It does not call a JavaScript function asynchronous. It puts the JavaScript into the page, which is then executed after the postback. You think you are executing JavaScript and you are exepcting results from it before it even reaches the client. That won't work at all. You cannot call frontend JavaScript while you are in the backend.
|

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.