我有一个 ASP.Net detailsview 控件。它的 DataSourceId 不同,我在 Page_load 中相应地设置它......(基于 LLBLgen 子类型,但这不是太重要)
我想这是我没有“得到”的页面生命周期泄漏抽象。
问题是我绑定到可能存在或不存在的字段,具体取决于数据源......
为了在某些条件下“禁用”绑定字段,我尝试将绑定字段包装在我在代码隐藏中设置为可见或不可见的面板中,但我仍然收到以下错误:
Sys.WebForms.PageRequestManagerServerErrorException:DataBinding:实体不包含名为“FilePrefix”的属性。
我在页面加载中更改了 detaislview.datasourceid ...在生命周期中可能为时已晚。
我不想绑定到该字段,因为新数据源不存在它,但它尝试这样做,但我得到了错误。
有任何想法吗?;)
[编辑]:按要求编码...
ASP,detailsview绑定栏:
<asp:TemplateField>
<ItemTemplate>
<asp:Panel ID="pnlNormalAdditionalFields" runat="server" Visible="false">
<asp:textbox id="txtFilePrefix" runat="server" MaxLength="250" Width="180px" text='<%# Bind("FilePrefix") %>'></asp:textbox>
<asp:requiredfieldvalidator id="valFilePrefix" runat="server" errormessage="File Prefix is required." controltovalidate="txtFilePrefix">*</asp:requiredfieldvalidator>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
代码隐藏:(确定数据源,detaislview 仅在回发时可见,因为网格显示在初始页面加载中。)
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack) //initial load
{
}
else //postback
{
//set customdatasource for grid & detailsview
switch (radAccountType.SelectedValue)
{
case "Normal":
dvAccount.DataSourceID = "NormalCollectionDataSource";
AccountRadGrid.customDataSourceId = "NormalCollectionDataSource";
break;
case "Reseller":
dvAccount.DataSourceID = "ResellerCollectionDataSource";
AccountRadGrid.customDataSourceId = "ResellerCollectionDataSource";
break;
...
显示/隐藏面板:
protected void dvAccount_OnPreRender(object sender, EventArgs e)
{
Panel pnlGroupStoreAdditionalFields = ControlHelper.FindControlFromTop(this, "pnlGroupStoreAdditionalFields", null) as Panel;
pnlGroupStoreAdditionalFields.Visible = false;
switch (radAccountType.SelectedValue)
{
...
case "GroupStore":
ddlAccountType.SelectedValue = Constants.Account.Type.GroupStore;
pnlGroupStoreAdditionalFields.Visible = true;
break;
}
}
}