你的意思就像在 django admin ui 中一样;)。
我目前正在尝试实现该功能,如果可以使用,我会在此处发布代码。
编辑:
好的,我明白了,完整的 django 风格......解释起来有点长,但实际上很简单。
要创建的文件:
一个 admin_popup.master 有一个漂亮的弹出页面(复制没有标题的 admin.master)。
以 admin_popup.master 作为主控的 popup_Insert.aspx。 (复制Insert.aspx)
修改
致您的 admin.master.cs:
添加这个:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
System.Web.UI.ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "refresh_fks", @"
var fk_dropdown_id;
function refresh() {
__doPostBack(fk_dropdown_id,'refresh');
};", true);
}
在您的 admin_popup.master 中,将这些属性添加到 body 标记(用于调整 poup 的大小)
<body style="display: inline-block;" onload="javascript:resizeWindow();">
在您的 admin_popup.master.cs 中
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
System.Web.UI.ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "refresh_fks", @"
var fk_dropdown_id;
function refresh() {
__doPostBack(fk_dropdown_id,'refresh');
};", true);
System.Web.UI.ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "resize_window", @"function resizeWindow() {
window.resizeTo(document.body.clientWidth + 20, document.body.clientHeight + 40);
window.innerHeight = document.body.clientHeight + 5;
window.innerWidth = document.body.clientWidth;
}", true);
System.Web.UI.ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "update_parent", @"function updateParent() {
window.opener.refresh();
}", true);
}
在popup_Insert.aspx.cs中,替换这两个函数:
protected void DetailsView1_ItemCommand(object sender, DetailsViewCommandEventArgs e) {
if (e.CommandName == DataControlCommands.CancelCommandName)
System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Close_Window", "self.close();", true);
}
protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e) {
if (e.Exception == null || e.ExceptionHandled) {
System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Close_Window", "window.opener.refresh(); self.close();", true);
}
}
在 ForeignKey_Edit.ascx 中,添加一个 LinkButton (ID=LinkButton1) 并在 ForeignKey_Edit.ascx.cs 中,替换该函数
protected void Page_Load(object sender, EventArgs e) {
if (DropDownList1.Items.Count == 0)
{
if (!Column.IsRequired) {
DropDownList1.Items.Add(new ListItem("[Not Set]", ""));
}
PopulateListControl(DropDownList1);
LinkButton1.OnClientClick = @"javascript:fk_dropdown_id = '{0}';window.open('{1}', '{2}', config='{3}');return false;".FormatWith(
DropDownList1.ClientID,
ForeignKeyColumn.ParentTable.GetPopupActionPath(PageAction.Insert),
"fk_popup_" + ForeignKeyColumn.ParentTable.Name, "height=400,width=600,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,directories=no,status=no");
}
if (Request["__eventargument"] == "refresh")
{
DropDownList1.Items.Clear();
if (!Column.IsRequired)
{
DropDownList1.Items.Add(new ListItem("[Not Set]", ""));
}
PopulateListControl(DropDownList1);
DropDownList1.SelectedIndex = DropDownList1.Items.Count - 1;
}
}
最后是我使用的两个扩展函数(放在你想放的地方):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Web.DynamicData;
using System.Web.UI;
public static class Utils
{
[DebuggerStepThrough]
public static string FormatWith(this string s, params object[] args)
{
return string.Format(s, args);
}
public static string GetPopupActionPath(this MetaTable mt, string action)
{
return new Control().ResolveUrl("~/{0}/popup_{1}.aspx".FormatWith(mt.Name, action));
}
}
在您的 global.asax 中,通过更改该行来注册新路由:
Constraints = new RouteValueDictionary(new { action = "List|Details|Edit|Insert|popup_Insert" }),
好吧,我希望我没有忘记任何东西......它当然可以改进,但它确实有效。
好的,我希望有些人会觉得这很有用,它使 ASP.NET 动态数据变得更好;)。我现在要看看多对多关系。