dra*_*fly 6 asp.net jquery jquery-ui
我正在尝试利用jQuery UI(或任何其他对话框插件)来替换默认的Confirm对话框.StackOverflow上有很多类似的问题和答案,例如:
然而,在ASP .NET中我需要更多东西.
由于页面约束上的一个表单,在ASP .NET页面上(使用ASP .NET 3.5)我可以有多个按钮提交相同的表单,并根据提交的标题信息页面知道哪个控件(按钮)触发表单提交,并且可以在服务器上调用正确的方法(附加到Button的Click事件的方法).
如果我使用其他StackOverflow答案的解决方案,例如:
buttons: {
'Delete all items': function() {
$(this).dialog('close');
currentForm.submit();
},
'Cancel': function() {
$(this).dialog('close');
}
}
Run Code Online (Sandbox Code Playgroud)
在PostBack上不会调用任何事件处理程序.如果我用以下代替:
buttons: {
'Delete all items': function() {
$(this).dialog('close');
$buttonThatWasConfirmed.click();
},
'Cancel': function() {
$(this).dialog('close');
}
}
Run Code Online (Sandbox Code Playgroud)
它将导致无限的模态对话递归.如何在ASP .NET中解决它?
几个月前我必须解决这个问题。我想在表单上有多个按钮,也许在取消按钮上加上模板化转发器中的按钮,并让所有按钮都正确地请求适当的确认,并根据用户操作提交表单或取消。以下控件可以根据需要多次包含在表单中。它继承System.Web.UI.WebControls.LinkButton并使用控件的 PostbackEventReference 来了解在确认后要提交哪个控件。System.Web.UI.WebControls.Button如果您愿意,可以轻松地继承该控件。我选择使用链接按钮,因为它的操作方式与按钮 Web 控件非常相似,但不会发出无法<input type=submit>在不使用控件适配器的情况下使用 jQuery UI 使用图标设置样式的样式。
/// <summary>
/// A <see cref="T:System.Web.UI.WebControls.LinkButton"/> with added jQuery UI functionality to provide a modal dialog box to cancel the form submit client side.
/// </summary>
/// <remarks>This class requires the inclusion of jQueryUI</remarks>
[DefaultProperty("Text")]
[ToolboxData("<{0}:jQueryUIConfirmedLinkButton runat=\"server\"></{0}:jQueryUIConfirmedLinkButton>")]
public class jQueryUIConfirmedLinkButton : LinkButton
{
/// <summary>
/// Holds the postback event reference data so that the emitted client script can execute the postback if the user confirms the action.
/// </summary>
protected string eventReference = null;
/// <summary>
/// Gets or sets the emitted dialog's ID attribute.
/// </summary>
/// <value>
/// The dialog's ID attribute.
/// </value>
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("dialog")]
[Localizable(true)]
public string DialogCssID
{
get
{
String s = (String)ViewState["DialogCssID"];
return ((s == null) ? String.Empty : s);
}
set
{
ViewState["DialogCssID"] = value;
}
}
internal protected string DialogID
{
get
{
return String.Format("{0}_{1}", this.ClientID, DialogCssID);
}
}
/// <summary>
/// Gets or sets the content of the dialog. This can be plain text or HTML.
/// </summary>
/// <value>
/// The HTML or plain text content of the dialog.
/// </value>
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("<p>Are you sure?</p>")]
[Localizable(true)]
public string DialogContent
{
get
{
String s = (String)ViewState["DialogContent"];
return ((s == null) ? String.Empty : s);
}
set
{
ViewState["DialogContent"] = value;
}
}
/// <summary>
/// Gets or sets the title that will appear on the dialog.
/// </summary>
/// <value>
/// The dialog's title.
/// </value>
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("Confirm Action")]
[Localizable(true)]
public string DialogTitle
{
get
{
String s = (String)ViewState["DialogTitle"];
return ((s == null) ? String.Empty : s);
}
set
{
ViewState["DialogTitle"] = value;
}
}
/// <summary>
/// Gets or sets the text that will appear on the confirmation button.
/// </summary>
/// <value>
/// The text that will appear on dialog's confirmation button.
/// </value>
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("Yes")]
[Localizable(true)]
public string DialogConfirmButtonText
{
get
{
String s = (String)ViewState["DialogConfirmButtonText"];
return ((s == null) ? String.Empty : s);
}
set
{
ViewState["DialogConfirmButtonText"] = value;
}
}
/// <summary>
/// Gets or sets the text that will appear on the dialog's rejection button.
/// </summary>
/// <value>
/// The text that appears on the dialog's rejection button.
/// </value>
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("No")]
[Localizable(true)]
public string DialogRejectButtonText
{
get
{
String s = (String)ViewState["DialogRejectButtonText"];
return ((s == null) ? String.Empty : s);
}
set
{
ViewState["DialogRejectButtonText"] = value;
}
}
/// <summary>
/// Raises the <see cref="E:System.Web.UI.Control.Load" /> event. Emits the necessary client script for the control to function.
/// </summary>
/// <param name="e">The <see cref="T:System.EventArgs" /> object that contains the event data.</param>
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.eventReference = Page.ClientScript.GetPostBackEventReference(this, string.Empty);
Page.ClientScript.RegisterStartupScript(this.GetType(), string.Format("{0}{1}", this.ClientID, "-DialogScript"), this.GetClientScript(), true);
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), string.Format("{0}{1}", this.ClientID, "-DialogShowScript"), string.Format("function {0}Confirm() {{$('#{0}').dialog('open');}}", this.DialogID), true);
this.OnClientClick = String.Format("{0}Confirm();return false;", this.DialogID);
}
/// <summary>
/// Renders the contents of the control to the specified writer. Adds the dialog HTML container to the output stream.
/// </summary>
/// <param name="writer">A <see cref="T:System.Web.UI.HtmlTextWriter" /> object that represents the output stream to render HTML content on the client.</param>
protected override void RenderContents(HtmlTextWriter writer)
{
base.RenderContents(writer);
writer.AddAttribute("id", this.DialogID);
writer.RenderBeginTag("div");
writer.Write(this.DialogContent);
writer.RenderEndTag();
}
public override void RenderEndTag(HtmlTextWriter writer)
{
base.RenderEndTag(writer);
}
/// <summary>
/// Gets the client script.
/// </summary>
/// <returns>A string that will be output to the client as script</returns>
private string GetClientScript()
{
return string.Format(@"$(function () {{
$('#{0}').dialog({{
autoOpen: false,
modal: true,
resizable: false,
buttons: {{
'{1}': function () {{
$(this).dialog('close');
eval({2});
}},
'{3}': function () {{
$(this).dialog('close');
}}
}},
title: '{4}'
}});
}});", this.DialogID, this.DialogConfirmButtonText, this.eventReference, this.DialogRejectButtonText, this.DialogTitle);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5673 次 |
| 最近记录: |