动态数据中外键字段的新按钮
在 ASP.NET 动态数据的脚手架页面中,如果实体具有外键字段,并且您查找的值不在主键表中,即不在下拉列表中,则必须放弃编辑实体,将查找的外键值添加到其表中,然后返回到原始实体。
我如何才能向外键字段模板添加“新建”链接/按钮,这将打开一个新窗口(使面板可见),您可以在其中添加所需的值,然后刷新下拉列表?
In a scaffolded page in ASP.NET Dynamic Data, if the entity has a foreign key field, and the value you seek is not in the the primary key table, i.e. is not in the drop-down, you have to abandon your edits to the entity, add the sought foreign key value to its table, and return to your original entity.
How could I go about adding a 'New' link/button to the foreign key field template, that would open a new window (make a Panel visible) where you can add the sought value, and then refresh the drop-down?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你的意思是像 django admin ui 一样;)。
我目前正在尝试实现该功能,如果我能正常工作,我将在此处发布代码。
编辑:
好的,我成功了,完整的 django 风格...解释起来有点长,但实际上很简单。
要创建的文件:
admin_popup.master 具有漂亮的弹出页面(复制不带标题的 admin.master)。
一个 popup_Insert.aspx,其中 admin_popup.master 作为主服务器。 (复制Insert.aspx)
修改
到您的admin.master.cs:
添加以下内容:
在您的 admin_popup.master 中,将这些属性添加到 body 标记(用于调整 poup 的大小)
在您的 admin_popup.master.cs
中 在 popup_Insert.aspx.cs 中,替换这两个函数:
在foreignKey_Edit.ascx 中,添加一个LinkButton (ID=LinkButton1) 和foreignkey_edit.ascx.cs 中,替换该函数
最后是我使用的两个扩展函数(将其放在您想要的位置):
在您的global.asax 中,通过更改该行来注册新路由:
好的我希望我没有忘记任何事情......它当然可以改进,但它有效。
好的,我希望有些人会觉得它很有用,它使 ASP.NET 动态数据变得更好;)。我现在要看看多对多关系。
You mean like in the django admin ui ;).
I'm currently trying to implement that feature, i'll post the code here if I get it to work.
EDIT:
Ok, I got that to work, complete django style... It's kinda long to explain but simple in fact.
Files to create:
A admin_popup.master to have a nice popup page (copy the admin.master without the header).
A popup_Insert.aspx with admin_popup.master as master. (copy the Insert.aspx)
Modifications
To your admin.master.cs:
add this:
In your admin_popup.master, add these attributes to the body tag (it's used to resize the poup)
In your admin_popup.master.cs
In popup_Insert.aspx.cs, replace these two functions:
In ForeignKey_Edit.ascx, add a LinkButton (ID=LinkButton1) and in ForeignKey_Edit.ascx.cs, replace that function
And finally the two extentions functions I use (put it where you want to):
In your global.asax, register the new route by changing that line:
Ok I hope i didn't forgot anything... It certainly could be improved, but it works.
Ok I hope some people will fint that useful, it makes ASP.NET Dynamic Data a lot more better ;). I'm goind to take a look at many-to-many relationships now.
收件人:VB.net 用户
如果您像我一样对动态数据的复杂性和混乱程度感到愤怒,那么这就是为您准备的!我花了 100 多个小时才弄清楚 DD 的基础知识(尽管我非常精通其他数据库编程技术)。
对于我们来说,纪尧姆的解决方案比诺顿的解决方案容易得多。经过 15 个多小时的尝试翻译 Naughton 的代码后,我尝试翻译 Guillaume 的代码,只花了 2 个小时就开始工作。这里也是按照同样的顺序。 (注意:cs 扩展当然会转换为 vb 扩展)
忽略 admin.master.cs 说明。
执行admin_popup.master代码。如果您仍在使用 VS 2008 或更早版本,您会在内联块引用上收到 CSS 错误。忽略该错误。
主文件 OnInit 子:
End Sub
将自定义页面下 (popup_Insert.aspx.vb) 中的事件替换为:
结束子
受保护的子DetailsView1_ItemInserted(ByVal发件人作为对象,ByVal e作为DetailsViewInsertedEventArgs)
如果 e.Exception 是 Nothing OrElse e.ExceptionHandled 那么
System.Web.UI.ScriptManager.RegisterClientScriptBlock(Me, Me.GetType, "Close_Window", "window.opener.location.reload(true); self.close();", True)
结束如果
End Sub
根据ForeignKey_Edit 创建自定义FieldTemplate 并将其命名为ForeignLinkKey_Edit.ascx。在 dropdownlist1 控件后面添加两个空格 ( ),然后按照他的说明创建 asp:LinkButton。输入“Add __”之类的文字。将页面加载函数替换为:
"';window.open('" &foreignKeyColumn.ParentTable.GetActionPath("Insert").Replace("Insert.aspx", "popup_Insert.aspx") & _
“'、'”& “fk_popup_”& ForeignKeyColumn.ParentTable.Name & "', config='" & _
“高度= 400,宽度= 400,工具栏=否,菜单栏=否,滚动条=否,可调整大小=否,位置=否,目录=否,状态=否”& “');返回假;”
结束如果
如果请求(“__eventargument”)=“刷新”那么
DropDownList1.Items.Clear()
如果不是 Column.IsRequired 那么
DropDownList1.Items.Add(New ListItem("[未设置]", ""))
结束如果
填充列表控件(DropDownList1)
DropDownList1.SelectedIndex = DropDownList1.Items.Count - 1
结束如果
End Sub
忽略扩展函数。
执行更新的路由建议。注意:如果您使用自定义路由,则必须对其进行修改,直到路由正确为止。
ATTN: VB.net USERS
If you have been like me and fuming at the mouth over how convoluted and a mess Dynamic Data is, this is for YOU! It has taken me over 100 hours just to figure out the basics of DD (even though I am very versed in other database programming techiniques).
Guillaume's solution is much easier than Naughton's for us. After 15+ hours of trying to translate Naughton's I tried to translate Guillaume's code which took me only 2 hours to get to work. Here it is in the same order. (Note: cs extensions of course translate to vb extensions)
Ignore the admin.master.cs directions.
Do the admin_popup.master code. If you are still in VS 2008 or older you'll get a CSS error on the inline block quote. Ignore the error.
MASTER FILE OnInit Sub:
End Sub
Replace the events in (popup_Insert.aspx.vb) under your custom pages with this:
End Sub
Protected Sub DetailsView1_ItemInserted(ByVal sender As Object, ByVal e As DetailsViewInsertedEventArgs)
If e.Exception Is Nothing OrElse e.ExceptionHandled Then
System.Web.UI.ScriptManager.RegisterClientScriptBlock(Me, Me.GetType, "Close_Window", "window.opener.location.reload(true); self.close();", True)
End If
End Sub
Create a custom FieldTemplate off the ForeignKey_Edit and name it ForeignLinkKey_Edit.ascx. After the dropdownlist1 control, add two spaces ( ) and then create the asp:LinkButton as he stated. Put text like "Add __" or something like that. Replace the Page Load function with this:
"';window.open('" & ForeignKeyColumn.ParentTable.GetActionPath("Insert").Replace("Insert.aspx", "popup_Insert.aspx") & _
"', '" & "fk_popup_" & ForeignKeyColumn.ParentTable.Name & "', config='" & _
"height=400,width=400,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,directories=no,status=no" & "');return false;"
End If
If Request("__eventargument") = "refresh" Then
DropDownList1.Items.Clear()
If Not Column.IsRequired Then
DropDownList1.Items.Add(New ListItem("[Not Set]", ""))
End If
PopulateListControl(DropDownList1)
DropDownList1.SelectedIndex = DropDownList1.Items.Count - 1
End If
End Sub
Ignore the extensions functions.
Do the updated routing suggested. Note: If you are using custom routes, you'll have to tinker with it until the routing is correct.
或者我刚刚在此处创建了两个服务器控件和一篇博客文章:
动态数据的弹出插入控件 其作用几乎相同,但将弹出功能封装在服务器控件中,将值 bak 从弹出窗口传递到主窗口和弹出按钮。
Or I have just created two server controls and a blog post here:
A Popup Insert control for Dynamic Data that does pretty much the same but encapsulates the popup functionlity in the server controls passing a value bak from the popup windows to main windows and popup button.
VS2010 RC 中的动态数据有什么新功能吗?在这个 RC 下,我们是否仍然需要诉诸诸如此类的技巧来获取动态数据中的简单主详细信息?
期待VS2010 RC下DD的一些博文...
Any new features in Dynamic Data in the VS2010 RC? Will we still have to resort to hacks such as these for simple Master-Details in Dynamic Data under this RC?
Looking forward to some blog posts on DD under VS2010 RC...