如何在没有客户端事件的情况下从ASP.NET代码中调用jQuery UI对话框?

时间:2022-01-04 15:00:15

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):

我正在尝试根据超出特定范围的值从我的C#ASP.NET代码打开jQuery UI对话框,而不是基于按钮单击或其他客户端事件。这是应该创建对话框的Javascript函数(位于.aspx页面的顶部):

<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本身(在.aspx页面的底部):

<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:

这里是试图显示对话框的C#代码部分:

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?

如果我在调试器中单步执行if块,则会到达并执行if块中的代码,但是没有显示对话框。我错过了什么?

2 个解决方案

#1


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:

我根据我在如何从我的c#代码后面打开一个jQuery UI对话框中找到的问题/答案稍微修改了我的功能,现在它可以工作了。这是修改后的功能:

<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>

#2


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.

此外,您可能想尝试使用startupscript而不是registerclientscriptblock。您必须确保在定义函数之后添加脚本,而不是之前添加。

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);
}

#1


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:

我根据我在如何从我的c#代码后面打开一个jQuery UI对话框中找到的问题/答案稍微修改了我的功能,现在它可以工作了。这是修改后的功能:

<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>

#2


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.

此外,您可能想尝试使用startupscript而不是registerclientscriptblock。您必须确保在定义函数之后添加脚本,而不是之前添加。

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);
}