1

I'm trying to open a jQuery UI dialog from my C# ASP.NET code based on a value being outside a certain range, rather than based on a button click or other client-side event. Here's the Javascript function that should create the dialog (at the top of the .aspx page):

<script type="text/javascript">
  //Total out of range dialog
  function ShowRangeDialog() {
    $('#rangeDialog').dialog({
      modal: true,
      width: 'auto',
      resizable: false,
      draggable: false,
      close: function (event, ui) {
        $('body').find('#rangeDialog').remove();
      },
      buttons:
      {
        'OK': function () {
          $(this).dialog('close');
        }
      }
    });
  }
</script>

Here's the dialog div itself (at the bottom of the .aspx page):

<div id="rangeDialog" style="display: none;" title="Total out of range">
  <p>
    Your line items total is out of the range allowed by the approval level you chose.
    Please check the approval range and adjust the line items or quantities.
  </p>
</div>

And here's the section of the C# code behind that attempts to display the dialog:

if (currTotal < lowerLim || currTotal > upperLim)
{
  //Show jQuery dialog telling user that their line items total is out of range
  Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "dlgOutOfRange",
    "ShowRangeDialog();", true);
}

The code in the if block is being reached and executed if I step through it in the debugger, but the dialog isn't being displayed. What am I missing?

1
  • I found a typo in my dialog script - it was missing the '#' in front of the "rangeDialog" selector, so there's no way it could have worked because it couldn't have found my dialog div. However, that didn't change anything, it still doesn't work. Commented Jun 4, 2013 at 19:18

2 Answers 2

1

I modified my function slightly, based on a question/answer I found at How do I open a jQuery UI Dialog from my c# code behind?, and now it works. Here's the modified function:

<script type="text/javascript">
  //Total out of range dialog
  function ShowRangeDialog() {
    $(function() {
      $('#rangeDialog').dialog({
        modal: true,
        width: 'auto',
        resizable: false,
        draggable: false,
        close: function (event, ui) { $('body').find('#rangeDialog').remove(); },
        buttons: { 'OK': function () { $(this).dialog('close'); }
        }
      })
    }).dialog("open");
  }
</script>
Sign up to request clarification or add additional context in comments.

Comments

0

try this

if (currTotal < lowerLim || currTotal > upperLim)
{
  //Show jQuery dialog telling user that their line items total is out of range
  Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "dlgOutOfRange",
    "ShowRangeDialog();", true);
}

You should just be calling the function name.

Also, you may want to try startupscript instead of registerclientscriptblock. You have to be sure that your script gets added AFTER the function is defined, and not before.

if (currTotal < lowerLim || currTotal > upperLim)
{
  //Show jQuery dialog telling user that their line items total is out of range
  Page.ClientScript.RegisterStartupScript(this.GetType(), "dlgOutOfRange",
    "ShowRangeDialog();", true);
}

5 Comments

Isn't that the same as what I had, just telling ASP.NET to render the "<script>" tags instead of doing it myself? I tried it anyway, with both RegisterClientScriptBlock and RegisterStartupScript, but it made no difference.
Did you check to see where the javascript was being rendered in the generated source? As I mentioned, it has to be rendered after the function definition.
There are a few reasons why it won't fire. The big 2 are 1) It's being called too early and 2) There is a syntax error in the function. To test #2 I would just call the function on $(document)ready(function(){ShowRangeDialog();}); If it doesn't fire when you call it on document ready, then there's something wrong in the actual function. Not the function call
I looked at the generated source, and it definitely gets inserted after my function. And when I put it in $(document).ready(function(){}) the dialog pops right up. So I don't know what else to try from here.
Yeah, that's odd. Try doing a simple alert statement and see if that works.

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.