简体   繁体   English

来自aspx页面的jQuery对话框内容

[英]jQuery dialog content from aspx page

I am not well versed in jQuery widgets, so need some directions here please.我不太精通 jQuery 小部件,所以请在这里提供一些指导。 We need to show a modal dialog with jQuery.我们需要用 jQuery 显示一个模态对话框。 Data for controls should be loaded on load and as a result of manipulations inside dialog.控件数据应在加载时加载,并作为对话框内操作的结果。 Easy way to load content I found is to specify aspx page for it:我发现加载内容的简单方法是为其指定 aspx 页面:

$(cnt).dialog({
  autoOpen: false,
  modal: true,
  ...
  open: function () {
    $(this).load('myDialog.aspx');
  }
 });

And I guess data could be retrieved by defining Ajax methods in that page as well.我猜也可以通过在该页面中定义 Ajax 方法来检索数据。

I also heard about some jQuery html templates, but didn't use them.我也听说过一些 jQuery html 模板,但没有使用它们。

Also from official doc it can be done也可以从官方文档中完成

:ooks like content can be simply specified inside container tag. :ooks 内容可以简单地在容器标签内指定。

  <p>This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>
</div>

Site itself is in ASP.NET WebForms.站点本身在 ASP.NET WebForms 中。

So my questions are所以我的问题是

  • what would be a preferred way to construct html layout inside dialog and more importantly why?在对话框中构建 html 布局的首选方法是什么,更重要的是为什么?
  • what options would be to use as data endpoints?哪些选项将用作数据端点? I know http handlers are quite obsolete.我知道 http 处理程序已经过时了。 Creating separate WebApi service might be an overkill - there is no such practice in this project.创建单独的 WebApi 服务可能有点矫枉过正——在这个项目中没有这样的做法。 And then, how about authentication?然后,身份验证怎么样?

Ok, a few things: We assume jQuery.UI and the jquery.ui dialog.好的,有几件事:我们假设 jQuery.UI 和 jquery.ui 对话框。 So, we assume both jQuery and jQuery.UI are referanced for this page.因此,我们假设此页面引用了 jQuery 和 jQuery.UI。

First up: The dialog will as a general rule be triggered (opened) as a result of a client side button click.首先:作为一般规则,对话框将作为客户端按钮单击的结果被触发(打开)。 (no post back). (没有回邮)。

Next: The content in the dialog can be: Another whole web page.下一个:对话框中的内容可以是: 另一个完整的网页。 A div from the current page.当前页面的 div。

So, depending on your needs, the "content" for the dialog can supplied be another web page, or the content can be supplied local.因此,根据您的需要,对话框的“内容”可以是另一个网页,也可以在本地提供。

next up: how do deal with the user response.接下来:如何处理用户响应。 Typical response could be: Ok (perhaps submit), or Ok, and Cancel.典型的响应可能是:Ok(也许提交),或者 Ok,然后取消。

Now NOTE AGAIN very carefull: That dialog cannot do a post back.现在再次注意:该对话框不能回发。 If you post back in that dialog, then all h breaks loose here.如果您在该对话框中回发,那么所有 h 都会在此处松散。

So, for starters, lets setup a dialog and do TWO VERY important things (things that 99% of the examples on the internet leave out - and thus is MUCH the cause of world poverty and suffering).所以,对于初学者来说,让我们建立一个对话并做两件非常重要的事情(互联网上 99% 的例子都遗漏了这些事情 - 因此这在很大程度上是世界贫困和苦难的原因)。

We will place two buttons in the dialog.我们将在对话框中放置两个按钮。 And we ALSO use the dialog feature that adds/places built in custom buttons on the dialog.我们还使用对话框功能,在对话框上添加/放置内置的自定义按钮。 What we looking for is how can you deal with either say a Ok, Cancel button as part of the dialog.我们要寻找的是您如何处理作为对话框一部分的“确定”或“取消”按钮。 And how can we deal with Ok, cancel as your OWN custom buttons.以及我们如何处理 Ok,cancel 作为您自己的自定义按钮。 With these two Rosetta stones laid out, then you are quite much free and off to the races for the next 10 years of code you need to do darn near any use case you can cook up.布置好这两个 Rosetta 石碑后,您就可以自由地参加接下来 10 年的代码竞赛了,您需要在可以编写的任何用例附近完成该任务。

one more VERY important concept: Even if the source of the dialog is a "div" in the local page, or that if you load a WHOLE page for the dialog?还有一个非常重要的概念:即使对话框的来源是本地页面中的“div”,或者如果您为对话框加载整个页面? The dialog STILL runs in the context of the CURRENT page.对话框仍然在当前页面的上下文中运行。 That means the button click events will run in the current page (script) and not the page you loaded!!!这意味着按钮点击事件将在当前页面(脚本)而不是你加载的页面中运行!!!

And you are 100% free to drop into that content asp.net buttons.您可以 100% 自由地进入该内容的 asp.net 按钮。 Just make sure that you prevent the post back of that button with a return false in the js.只需确保在 js 中使用 return false 阻止该按钮的回传。

So, lets wire this up.所以,让我们把它连起来。

So, we thus need this flow:所以,我们因此需要这个流程:

Display dialog on button click (client side js).单击按钮时显示对话框(客户端 js)。

User does whatever in that dialog.用户在该对话框中执行任何操作。

User now clicks on a button (say ok, or cancel).用户现在点击一个按钮(说确定,或取消)。

We CAN and will for the sake of going crazy?我们可以并且会为了发疯吗? We will drop on the form and use standard asp.net buttons.我们将删除表单并使用标准的 asp.net 按钮。 There is really no need to adopt HTML “input” buttons.真的没有必要采用 HTML 的“输入”按钮。 (You can, but no real need here). (你可以,但这里没有真正的需要)。

I am going to use BOTH built in jQuery buttons and ALSO two standard buttons in that dialog (the reason is many, but that way YOU can choose either way, and learn this. (boy, do I with someone had laid out how this works for me!!!). So, I going to save you much pain and suffering here.我将在该对话框中同时使用内置的 jQuery 按钮和两个标准按钮(原因很多,但你可以选择任何一种方式,然后学习这个。(男孩,我和某人一起制定了它是如何工作的)对我来说!!!)所以,我会在这里为你省去很多痛苦和痛苦。

So, lets start from the top: Our button code to pop the dialog.所以,让我们从顶部开始:我们弹出对话框的按钮代码。

The asp button will be this: asp按钮将是这样的:

<asp:Button ID="showdialog" runat="server" Text="Show the dialog" 
    OnClientClick="showpop();return false;"/>

Note close in above!!!!注意在上面!!!! We use OnClientClick=showpop();return false.我们使用 OnClientClick=showpop();return false。 This will thus run js code, and NOT do a post back.这将因此运行 js 代码,而不是回发。 The return = false is VERY important here, since if you leave that out, then the standard asp button will post back like it “normally” does. return = false 在这里非常重要,因为如果您忽略它,那么标准 asp 按钮将像“正常”那样回发。 And we break our new rule!!我们打破了我们的新规则!! – no post backs inside the dialog!!. – 对话框内没有回帖!!。

Ok, so that is the button to launch the dialog.好的,这就是启动对话框的按钮。

The js code to launch the dialog can look like this:用于启动对话框的 js 代码如下所示:

function showpop() { var mydiv = $('#popdialog'); function showpop() { var mydiv = $('#popdialog'); mydiv.dialog({ autoOpen: false, modal: true, title: 'My cool other page', width: '30%', position: { my: 'top', at: 'top+150' }, buttons: { 'ok': mycustomok, 'cancel': mycustomcancel } mydiv.dialog({ autoOpen: false, modal: true, title: '我很酷的其他页面', width: '30%', position: { my: 'top', at: 'top+150' }, buttons: { “确定”:mycustomok,“取消”:mycustomcancel }

    });
    // Open the dialog
    mydiv.dialog('open');
}

As noted, the content for the dialog can be another web page, or a "div" Lets do the div example first.如前所述,对话框的内容可以是另一个网页,也可以是“div”让我们先做 div 示例。 Now in above, we have two built in buttons (a feature of the dialog), and as noted, we going to drop in two asp buttons that do the SAME thing.现在在上面,我们有两个内置按钮(对话框的一个功能),并且如前所述,我们将放入两个执行相同操作的 asp 按钮。

Our div will look like this:我们的 div 看起来像这样:

<div id="popdialog" runat="server" style="display:none">
<h2>My cool pop dialog</h2>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />

<asp:Button ID="MyOk" runat="server" Text="My custom ok" 
    OnClientClick="mycustomok();return false;"/>
<br />

<asp:Button ID="MyCancel" runat="server" Text="My custom cancel" 
    OnClientClick="mycustomcancel();return false;"/>

</div>

Note again, we have two standard asp.net buttons.再次注意,我们有两个标准的 asp.net 按钮。 But NOTE the client side code stubs we specified for both of the above (mycustomok, and mycustomcancel).但是请注意我们为上述两个(mycustomok 和 mycustomcancel)指定的客户端代码存根。

The only real thing to note in above is the style=”display:none” for the WHOLE “div”.上面唯一需要注意的是整个“div”的 style="display:none"。 This will thus hide the div, and it will not display when the page loads.这将因此隐藏 div,并且它不会在页面加载时显示。

So, when we click on the button, it will show the dialog.所以,当我们点击按钮时,它会显示对话框。 You will see this:你会看到这个:

在此处输入图片说明

So, we have our two custom buttons, and the two jQuery but in buttons.所以,我们有两个自定义按钮和两个 jQuery 但在按钮中。 If you don't want the built in ones then simply remove the above "buttons" part of the dialog.如果您不想要内置的,那么只需删除对话框的上述“按钮”部分。

So, the two js stubs we have for this are:因此,我们为此拥有的两个 js 存根是:

function mycustomok() {
    // first, close the dialog
    $('#popdialog').dialog('close');
    #('#HiddenOk').click();
}

function mycustomcancel() {
    // first, close the dialog
    $('#popdialog').dialog('close');
    $('#HiddenCancel').click();
}

I can WELL NOTE that you do NOT need the .dialog('close') WHEN ONLY using the built in buttons (they will close dialog for you).我可以很好地注意,仅使用内置按钮(它们会为您关闭对话框)时,您不需要 .dialog('close') 。 But if you going to drop/place custom buttons in the content of the dialog then you do need to close the dialog.但是,如果您要在对话框的内容中放置/放置自定义按钮,那么您确实需要关闭对话框。

Now, the next trick?现在,下一个技巧? Well, you can start to wire up some web methods, and ajax calls.好吧,您可以开始连接一些 Web 方法和 ajax 调用。 But that is quite a bit of pain.但那是相当痛苦的。 So, we want code behind to run for ok, and code behind to run for cancel.所以,我们希望后面的代码运行正常,而后面的代码运行取消。

And as noted, based on either choice, we will do a post back and run code behind.如前所述,基于任一选择,我们将进行回发并在后面运行代码。 As I stated, the short way, easy way is to drop two hidden buttons on the form, they will look like this, and of course our outside the above div正如我所说的,简单的方法是在表单上放置两个隐藏的按钮,它们看起来像这样,当然我们在上面的 div 之外

<asp:Button ID="HiddenOk" runat="server" Text="hidden ok" style="display:normal"/> <asp:Button ID="HiddenOk" runat="server" Text="hidden ok" style="display:normal"/>

<asp:Button ID="HiddenCancel" runat="server" Text="hidden cancel" 
    style="display:normal"/>

Note careful (note BEYOND careful here).注意注意(注意这里要小心)。 I have display = normal for the two buttons.我有两个按钮的显示 = 正常。 The reason is that then with the designer you can double click on the button, and write your code behind.原因是,您可以使用设计器双击按钮,然后在后面编写代码。 When you are DONE and have both code behind stubs written, then change the above display:normal to display:none to hide them.当您完成并编写了存根后面的两个代码后,将上面的 display:normal 更改为 display:none 以隐藏它们。 So we left the buttons visible for easy development.所以我们让按钮可见以方便开发。 Once you wired up the code behind (simply double click on the buttons in the designer), then you are jumped to the code behind editor, and can write that code.一旦您连接了背后的代码(只需双击设计器中的按钮),您就会跳转到背后的代码编辑器,并可以编写该代码。 You then hide them.然后你隐藏它们。

Your two server side event code buttons (code behind would look like this:)您的两个服务器端事件代码按钮(后面的代码如下所示:)

Protected Sub HiddenOk_Click(sender As Object, e As EventArgs) Handles HiddenOk.Click
    Debug.Print("dialog ok code run")
End Sub

Protected Sub HiddenCancel_Click(sender As Object, e As EventArgs) Handles HiddenCancel.Click
    Debug.Print("dialgo cancel code")
End Sub

The above two are vb.net, but it really much the same with C#.上面两个是vb.net,但其实和C#大同小异。

The “click” button trick as noted is rather nice.如上所述的“点击”按钮技巧相当不错。 It solves a LONG list of issues, and does so with great ease.它解决了一长串问题,而且非常容易。 You get the needed post back.你会得到所需的帖子。 You get to run your own cute little code stub behind.你可以在后面运行你自己可爱的小代码存根。 You don't have to write up ajax calls to do this!!!您不必编写 ajax 调用来执行此操作!!!

So this follows the whole asp.net design pattern in which you drop buttons on a form, click them, and you get to run that nice little short code behind stub.因此,这遵循了整个 ASP.NET 设计模式,在该模式中,您将按钮放在表单上,​​单击它们,然后您就可以在 stub 后面运行那个漂亮的短代码。 And it all wired up automatic for you.这一切都为您自动连接。

Thus in summary:因此总结如下:

Don't try + attempt post backs in the dialog – you REALLY can't do this.不要在对话框中尝试+尝试回发——你真的不能这样做。 And you find they don't work anyway!你发现它们无论如何都不起作用! So buttons on that dialog WILL run “js” code.因此该对话框上的按钮将运行“js”代码。 You can use asp buttons if you want, just remember the extra return=false part.如果需要,您可以使用 asp 按钮,只需记住额外的 return=false 部分。 Once the dialog is closed and all good, then and ONLY then do you want the post back to occur.一旦对话框关闭并且一切正常,那么并且只有这样您才希望回帖发生。

So, no postbacks while that dialog is open - simply don't try or do it.因此,在该对话框打开时没有回发 - 只是不要尝试或这样做。 that includes trying to use a up-date panel.这包括尝试使用最新的面板。

Last but not least?最后但并非最不重要的? In above we loaded content from a div, and in most cases that is quite much the suggested road here.在上面我们从一个 div 加载内容,在大多数情况下,这是这里建议的方法。

however, you CAN in place of a "div" change the above to load a whole different page.但是,您可以代替“div”更改上述内容以加载整个不同的页面。

eg like this:例如像这样:

function showpop() { var mydiv = $('#popdialog'); function showpop() { var mydiv = $('#popdialog'); mydiv.dialog({ autoOpen: false, modal: true, title: 'My cool other page', width: '30%', position: { my: 'top', at: 'top+150' }, buttons: { 'ok': mycustomok, 'cancel': mycustomcancel } mydiv.dialog({ autoOpen: false, modal: true, title: '我很酷的其他页面', width: '30%', position: { my: 'top', at: 'top+150' }, buttons: { “确定”:mycustomok,“取消”:mycustomcancel }

    });
    // Open the dialog
    mydiv.load('WebForm6.aspx');
    mydiv.dialog('open');
}

So note we STILL needed a div on the page.所以请注意,我们仍然需要页面上的 div。 It will be "empty", and we NOW use the .load() to load a whole different/separate web page into that div.它将是“空的”,我们现在使用 .load() 将整个不同/单独的网页加载到该 div 中。 This can be any other web page you have.这可以是您拥有的任何其他网页。 Or maybe some dialog you re-use over and over.或者,也许您会一遍又一遍地重复使用某些对话框。 (and that web page will in fact be loaded into that div on the current web page for you). (并且该网页实际上会为您加载到当前网页上的该 div 中)。

Note again:再次注意:

Our rules about no post backs in those pages still applies.我们关于在这些页面中不回发的规则仍然适用。 So if there is to updates or edits or interaction inside of that page?那么,该页面内是否有更新、编辑或交互? Then you have to start adopting aJax calls and use no post back code to update data.然后你必须开始采用 aJax 调用并且不使用回发代码来更新数据。 However, if the dialog is a confirmation, or even that the user does select some check boxes, enters some data etc?但是,如果对话框是确认,甚至用户确实选择了一些复选框,输入了一些数据等? Well then no post-backs are required until the final ok, or cancel anyway.那么在最终确定之前不需要回传,或者无论如何取消。

As noted, if you don't want to do anything for the cancel button(s) (built in or custom), then you can just run that code to close the dialog and take no further actions.如前所述,如果您不想对取消按钮(内置或自定义)执行任何操作,则只需运行该代码即可关闭对话框,而无需采取进一步操作。 And if it is a built in dialog button, then you don't even need the code stub to close the dialog as that is done for you automatic with built in buttons.如果它是一个内置对话框按钮,那么您甚至不需要代码存根来关闭对话框,因为使用内置按钮自动完成。 But both sets of buttons do the exact same thing, and thus are you are free to use built in button feature, or simply drop your own buttons into that div (or other web page).但是两组按钮都做完全相同的事情,因此您可以自由使用内置按钮功能,或者只是将自己的按钮放入该 div(或其他网页)中。

So, in summary:所以,总结一下:

You need a div on the page for the dialog (EVEN if you going to load another page into that div as per last example).您需要在对话框的页面上有一个 div(即使您要按照上一个示例将另一个页面加载到该 div 中)。

It is typical to place, build and create that div + content on the same page.通常在同一页面上放置、构建和创建 div + 内容。 Or as noted, can pull/load a whole page, but you still need that div on the page.或者如前所述,可以拉/加载整个页面,但您仍然需要页面上的 div。

If you not pulling a page, don't forget to set with style="display:none" to hide the content.如果你不拉页面,不要忘记设置 style="display:none" 来隐藏内容。 (jQquery.ui will hide/show for you, but still need to have it display none from the start to hide it). (jQquery.ui 将为您隐藏/显示,但仍然需要从一开始就让它不显示以隐藏它)。

You can't have post backs occur, since that dialog is now part of your whole web page DOM, and any post back will quite much blow up the whole dialog process.您不能进行回发,因为该对话框现在是整个网页 DOM 的一部分,任何回发都会大大破坏整个对话过程。

If you do need post backs upon that ok, cancel or whatever?如果您确实需要回帖,可以取消还是其他什么? Well again to save world poverty's and having to wire up a whole bunch of AJAX code, or say use js _do postback() with some parameters that requires additional logic in the web form's on-load?再次拯救世界贫困,不得不连接一大堆 AJAX 代码,或者说使用 js _do postback() 和一些需要在 web 表单加载时附加逻辑的参数?

Then use the cute click();然后使用可爱的 click(); trick for hidden buttons.隐藏按钮的技巧。 You save tons of time, tons of js code, and wire up, and you get the classic click a button and run code behind development model anyway.您可以节省大量时间、大量 js 代码并进行连接,并且无论如何您都会获得经典的单击按钮并运行开发模型背后的代码。 of couse in this case, those buttons do give us the required and needed postback and run of code behind, and that's quite much what we want after the dialog is all said and done.当然,在这种情况下,这些按钮确实为我们提供了所需的和需要的回发和后台代码运行,这正是我们在对话完成后所想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM