2009-10-25 30 views
1

在ASP.NET動態數據的腳手架頁面中,如果實體具有外鍵字段,並且您查找的值不在主鍵表中,即不在主鍵表中下拉菜單中,您必須放棄對實體的編輯,將所需的外鍵值添加到其表中,然後返回到原始實體。動態數據中外鍵字段的新按鈕

我怎麼可以去添加一個'新'鏈接/按鈕到外鍵字段模板,這將打開一個新窗口(使一個面板可見),您可以添加所需的值,然後刷新下拉菜單,下?

+0

我知道它並沒有回答你的問題,但個人而言,這聽起來像你需要一個輸入嚮導,而不是完全依賴DyDa。 – 2009-10-27 19:18:59

回答

3

你的意思是像在Django的管理員用戶界面)。 我目前正在嘗試實現該功能,如果我能夠使用它,我會在這裏發佈代碼。

編輯:

好吧,我得到了工作,完整的Django的風格......它有點時間去解釋,但其實很簡單。

文件創建:

一個admin_popup.master有一個很好的彈出網頁(不復制標題中的admin.master)。

popup_Insert.aspx與admin_popup.master作爲主。 (複製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,添加一個LinkBut​​ton(ID = LinkBut​​ton1)和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動態數據更好;)。我現在要去看看多對多的關係。

1

或者我剛剛創建了兩個服務器控件和一個博客文章: A Popup Insert control for Dynamic Data幾乎相同,但在服務器控件中封裝了彈出窗口functionlity,將彈出窗口的值傳遞給主窗口和彈出按鈕。

1

VS2010 RC中動態數據的新特性?在這個RC下,我們是否還需要使用這些簡單的動態數據Master Master Details?

期待下VS2010 RC上DD一些博客文章...

+0

這周我會試一試。 – ProfK 2010-02-14 07:25:01

2

聯繫人:VB.net USERS

如果你已經和我一樣,在嘴在如何令人費解和亂七八糟的動態復明數據是,這是給你的!爲了弄清楚DD的基本知識(即使我非常熟悉其他數據庫編程技術),花了我100多個小時。

對於我們來說,Guillaume的解決方案比Naughton的解決方案容易得多。在嘗試翻譯Naughton的15個多小時後,我嘗試翻譯Guillaume的代碼,這些代碼花了我2個小時纔開始工作。這裏的順序是一樣的。 (注意:cs擴展名當然會翻譯成vb擴展名)

  1. 忽略admin.master.cs的指示。

  2. 執行admin_popup.master代碼。如果您仍處於VS 2008或更高版本,則會在內聯塊報價上收到CSS錯誤。忽略錯誤。

  3. 主文件是OnInit子:

    Protected Overrides Sub OnInit(ByVal e As EventArgs) 
    MyBase.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 + 120, document.body.clientHeight + 120);   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.location.reload(true);  }", True) 
    

    末次

  4. 這個在你的自定義頁面替換(popup_Insert.aspx.vb)事件:

    Protected Sub DetailsView1_ItemCommand(ByVal sender As Object, ByVal e As DetailsViewCommandEventArgs) 
    If e.CommandName = DataControlCommands.CancelCommandName Then 
        System.Web.UI.ScriptManager.RegisterClientScriptBlock(Me, Me.GetType, "Close_Window", "self.close();", True) 
    End If 
    

    結束Sub 受保護的子細節ViewView1_ItemInserted(ByVal sender As Object,ByVal e As DetailsViewInsertedEventArgs) 如果e.Exception是Nothing OrElse e.ExceptionHandled Then System.Web.UI.ScriptManager.RegisterClientScriptBlock(Me,Me.GetType,「Close_Window」,「window.opener.location.reload(true); self.close();」,真) 結束如果 結束小組

  5. 創建自定義FieldTemplate關閉ForeignKey_Edit並將其命名爲ForeignLinkKey_Edit.ascx的dropdownlist1控制後,加入兩個空格( ),然後創建。 。ASP:LinkBut​​ton的,他說把文本,如 「添加__」 或類似的東西替換該頁面加載功能:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) 
    If DropDownList1.Items.Count = 0 Then 
        If Not Column.IsRequired Then 
         DropDownList1.Items.Add(New ListItem("[Not Set]", "")) 
        End If 
        PopulateListControl(DropDownList1) 
    
        LinkButton1.OnClientClick = "javascript:fk_dropdown_id = '" & DropDownList1.ClientID & _ 
    

    「 '; window.open('」 & ForeignKeyColumn.ParentTable.GetActionPath( 「Insert」)替換(「Insert.aspx」,「popup_Insert.aspx」)& _ 「','」&「fk_popup_」& ForeignKeyColumn.ParentTable。名稱& 「 '配置='」 & _ 「HEIGHT = 400,寬度= 400,工具欄=沒有,菜單=沒有,滾動條=沒有,可調整大小=沒有,位置=沒有,目錄=沒有,地位=無」 & 「'); return false;」 結束如果 如果請求( 「__ eventargument」)= 「刷新」。然後 DropDownList1.Items.Clear() 如果不Column.IsRequired然後 DropDownList1.Items.Add(新列表項( 「[未設置]」, 「」 )) 結束如果 PopulateListControl(DropDownList1) DropDownList1.SelectedIndex = DropDownList1.Items.Count - 1 結束如果結束 子

  6. 忽略擴展功能。

  7. 是否建議更新的路由。注意:如果您使用自定義路線,則必須修改它,直到路線正確。