母版页上的FormView无法通过ContentPlaceHolder边界查看数据绑定控件 [英] FormView on a Master Page can't see databound controls through ContentPlaceHolder boundary

查看:68
本文介绍了母版页上的FormView无法通过ContentPlaceHolder边界查看数据绑定控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有许多结构类似的FormView.为了避免重复标记,我创建了一个包含FormView的母版页,并将ContentPlaceHolder放置在FormView内.然后,特定的数据绑定控件(这是唯一在页面之间更改的控件)位于使用该母版页的页面上.

所以我有一个看起来像这样的母版页:

<%@ master ... %>
...
<form runat=server>
...
   <asp:formview runat="server" ... >
      <edititemtemplate>
         ... Lots of common markup ...
         <asp:contentplaceholder id='FormRows' runat='server' />
         ... Lots more common markup ...
       </edititemtemplate>
    </asp:formview>
...
</form>

和使用该母版页的页面看起来像这样:

<%@ page masterpagefile="Form.Master" ... %>
<asp:content contentplaceholderid="FormRows" runat="server" >
   ...
   <p>
     Field One: 
     <asp:textbox runat=server text='<%#Bind("Field1")%>' id='Field1' />
   </p>
   <p>
     Field Two: 
     <asp:textbox runat=server text='<%#Bind("Field2")%>' id='Field2' />
   </p>
   ...
</asp:content>

对于现有记录,FormView会透视数据绑定控件(Field1等),并使用正确的数据填充它们.但是在插入或更新时,看不到它们,并且它们不包含在插入或更新中.在FormView_ItemInserting事件中,e.Values为空;否则为0.同样,在FormView_ItemUpdating事件中,e.NewValues为空.

所以:

  1. 是否可以在母版页上激发FormView,以查看ContentPlaceholder内部的数据绑定控件?

  2. 是否失败,是否存在一种简单的方法来识别与<%#Bind(...)%>绑定数据的控件,以便我可以将它们手动添加到值包中?

解决方案

我认为这将是困难的,如果不可能的话;实际上,我对数据绑定的工作感到惊讶!

您可能想尝试另一种封装FormView控件的方法.

您可以尝试将FormView控件放置在具有PlaceHolder的.ascx控件中,现在您拥有ContentPlaceHolder.

然后在每个ASPX页面上,您都可以拥有一个镜像ASCX页面,其中包含占位符的填充符.您可以给它们使用相同的名称(Page1.aspx使用Page1.ascx),或者设置命名约定,例如Page1-Content.ascx,以便您的FormView ascx可以弄清楚其填充控件的名称,请使用Page.LoadControl()按路径加载控件,然后在Init阶段插入该内容.

现在,您的内容控件具有能够具有公共属性的优点,因此您可以绑定到这些公共属性,并使这些属性将数据往返于填充器.ascx文件中的适当服务器控件之间. /p>

不幸的是,它是文件的两倍(因为每个页面都需要ASPX和ASCX),但是与备用文件(复制所有代码)相比,它的工作量却很大.

当然,您还没有告诉我们所有通用标记是什么,但是您的通用标记也可以放入CommonMarkupHeader.ascx和CommonMarkupFooter.ascx中,并包含在每个页面的唯一FormView中.

I have a number of similarly structured FormViews. In an effort to avoid duplicate markup, I've created a master page that contains the FormView, and placed a ContentPlaceHolder inside the FormView. The specific databound controls - which are the only thing that change from page to page - are then on the page that uses that master page.

So I have a master page that looks something like this:

<%@ master ... %>
...
<form runat=server>
...
   <asp:formview runat="server" ... >
      <edititemtemplate>
         ... Lots of common markup ...
         <asp:contentplaceholder id='FormRows' runat='server' />
         ... Lots more common markup ...
       </edititemtemplate>
    </asp:formview>
...
</form>

and a page using that master page that looks something like this:

<%@ page masterpagefile="Form.Master" ... %>
<asp:content contentplaceholderid="FormRows" runat="server" >
   ...
   <p>
     Field One: 
     <asp:textbox runat=server text='<%#Bind("Field1")%>' id='Field1' />
   </p>
   <p>
     Field Two: 
     <asp:textbox runat=server text='<%#Bind("Field2")%>' id='Field2' />
   </p>
   ...
</asp:content>

With an existing record, the FormView sees through to the databound controls (Field1, etc) and populates them with the correct data. But when inserting or updating, it doesn't see them, and they're not included in the insert or update. In the FormView_ItemInserting event, e.Values is empty; likewise in the FormView_ItemUpdating event, e.NewValues is empty.

So:

  1. Is there a way to provoke the FormView on the master page to see through to the databound controls inside the ContentPlaceholder?

  2. Failing that, is there a straightforward way of identifying controls that are databound with <%#Bind(...)%> so that I can add them manually to the values bag?

解决方案

I think this will prove difficult, if not possible; in fact I'm surprised that the databinding works at all!

You may want to try a different method of encapsulating your FormView control.

You could try placing the FormView control in an .ascx control with a PlaceHolder where you now have the ContentPlaceHolder.

Then on each ASPX page, you could have a mirror ASCX page that contains the filler for the placeholder. You could give them the same names (Page1.aspx uses Page1.ascx) or set up a naming convention like Page1-Content.ascx, so that your FormView ascx would figure out what it's filler control is named, use Page.LoadControl() to load the control by path, and plug that content in during the Init phase.

Now, your content controls have the advantage of being able to have public properties, so you could bind to those public properties, and have the properties shuttle the data to and from the appropriate server controls in the filler .ascx file.

Unfortunately it's double the files (because of the ASPX and ASCX required for each page) but fairly work-unintensive compared to the alternative (duplicating all that code)

Of course, you haven't told us what all your common markup is, but your common markup could go into a CommonMarkupHeader.ascx and CommonMarkupFooter.ascx as well and included on each page's unique FormView.

这篇关于母版页上的FormView无法通过ContentPlaceHolder边界查看数据绑定控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆