這是我第一次使用jQuery對話框,所以這個問題可能有點簡單,但我還沒有找到答案。從服務器端顯示jQuery模態對話框
我使用ASP.Net Ajax執行異步回發時,用戶輸入的東西到一個文本框(實際上掃描條形碼)。這是一種魅力。 現在我需要讓用戶在特定條件下決定兩個選項。因此,我需要回發來決定是否必須顯示此對話框以及它具有哪些內容。
問:如何從服務器端打開一個jQuery UI(模態)對話框,讓用戶選擇一個選項,再次回發到服務器並處理這個決定。
我想我需要AjaxControlToolkit.ToolkitScriptManager.RegisterClientScriptBlock
註冊打開對話框的腳本,對不對?如果是,那麼哪個腳本會打開onload對話框?
我可以再由對話框內的控制(f.e。單選按鈕或者一個DropDownList)設置AutoPostback=true
和處理相應的事件回發到服務器?
什麼是最好/最簡單的方法來完成我想要的?
編輯:我認爲將對話框封裝到一個ASP.Net UserControl並顯示/隱藏它。這種方法是值得推薦的嗎?還是會以任何方式與jQuery發生衝突?
您對任何這些問題的幫助都非常感激。
我的解決方案(感謝卡帕):
也是有幫助的是此鏈接:Using JQuery UI Dialog with ASP.Net and AJAX Update Panel和註釋有來自理查德。
我創建了一個用戶控件作爲容器的jQuery的對話框:
對話框的ASCX:
<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="DialogRecurringRMA.ascx.vb" Inherits="ERP.DialogRecurringRMA" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<script type="text/javascript">
function createDialog() {
//setup dialog
var source = $('#Dialog_RecurringRMA').parent();
$('#Dialog_RecurringRMA').dialog({
modal: true,
resizable: false,
autoOpen: true,
draggable: true,
width: 420,
height: 200,
title: '<%= Title %>',
open: function (type, data) {
$(this).parent().appendTo(source);
}
});
}
function showDialog() {
$('#Dialog_RecurringRMA').dialog("open");
}
function closeDialog() {
$('#Dialog_RecurringRMA').dialog("close");
}
</script>
<asp:UpdatePanel ID="UpdDialog" UpdateMode="Conditional" ChildrenAsTriggers="true" runat="server">
<ContentTemplate>
<div id='Dialog_RecurringRMA' style="display:none">
<asp:RadioButtonList ID="RblApplyRmaToCharge" ValidationGroup="Dialog" runat="server">
<asp:ListItem Value="1">Create new RMA and add it to current Charge</asp:ListItem>
<asp:ListItem Value="2">Do not create new RMA</asp:ListItem>
</asp:RadioButtonList>
<asp:RequiredFieldValidator ID="NothingSelected" Display="None" ControlToValidate="RblApplyRmaToCharge" ValidationGroup="Dialog" runat="server" ErrorMessage="You have to decide whether RMA should be applied to current charge or not">*</asp:RequiredFieldValidator>
<asp:ValidatorCalloutExtender ID="NothingSelectedExtender" BehaviorID="NothingSelectedExtender" runat="server" TargetControlID="NothingSelected" Width="250px" PopupPosition="BottomRight" HighlightCssClass="highlight" WarningIconImageUrl="~/images/warning.gif" CloseImageUrl="~/images/close.gif" />
<br /><br /><br /><br />
<asp:Button ID="BtnApply" runat="server" OnClientClick="javascript:Page_ClientValidate();if(Page_IsValid)closeDialog();" CausesValidation="true" ValidationGroup="Dialog" Text="apply selection" />
</div>
</ContentTemplate>
</asp:UpdatePanel>
對話框的Codebind:
Public Class DialogRecurringRMA
Inherits System.Web.UI.UserControl
Public Enum DialogResult As Int32
NoResult = 0
CreateRma = 1
DoNotCreateRma = 2
End Enum
Public Event DialogClosed(ByVal dialog As DialogRecurringRMA)
Public Property Title As String
Get
If ViewState("Title") Is Nothing Then
ViewState("Title") = "RMA with this IMEI found, create anyway?"
End If
Return DirectCast(ViewState("Title"), String)
End Get
Set(ByVal value As String)
ViewState("Title") = value
End Set
End Property
Public Property CreateRmaText As String
Get
Return Me.RblApplyRmaToCharge.Items(0).Text
End Get
Set(ByVal value As String)
Me.RblApplyRmaToCharge.Items(0).Text = value
End Set
End Property
Public Property DoNotCreateRmaText As String
Get
Return Me.RblApplyRmaToCharge.Items(1).Text
End Get
Set(ByVal value As String)
Me.RblApplyRmaToCharge.Items(1).Text = value
End Set
End Property
Public ReadOnly Property Result() As DialogResult
Get
If Me.RblApplyRmaToCharge.SelectedIndex <> -1 Then
Return If(Me.RblApplyRmaToCharge.SelectedIndex = 0, DialogResult.CreateRma, DialogResult.DoNotCreateRma)
Else
Return DialogResult.NoResult
End If
End Get
End Property
Public Sub Reset()
Me.RblApplyRmaToCharge.SelectedIndex = -1
End Sub
Private Sub CloseDialog()
ScriptManager.RegisterClientScriptBlock(Me, GetType(Page), UniqueID, "closeDialog()", True)
End Sub
Private Sub ShowDialog()
ScriptManager.RegisterStartupScript(Me, GetType(Page), UniqueID, "createDialog();showDialog()", True)
End Sub
Public Sub OpenDialog()
Reset()
ShowDialog()
Me.UpdDialog.Update()
End Sub
Protected Sub BtnApply_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BtnApply.Click
Page.Validate()
If Me.Page.IsValid Then
CloseDialog()
RaiseEvent DialogClosed(Me)
End If
End Sub
End Class
它儘可能的簡單展示從現在開始使用的對話框:
Me.DialogRecurringRMA1.OpenDialog()
在我的情況有點複雜,因爲對話框的ASCX用在這裏:
MasterPage -> Page -> TabContainer-Panel -> UserControl -> UserControl -> Dialog
但是,這工作就像一個魅力:)
謝謝你的建議。我會記住這一點,但沒有經驗。我會調查並報告。 – 2011-03-02 16:43:40
對於其他人,以下鏈接也非常有幫助:http://blog.roonga.com.au/2009/07/using-jquery-ui-dialog-with-aspnet-and.html – 2011-03-08 13:01:20