【发布时间】:2010-05-19 12:30:53
【问题描述】:
我有一个 ASP.Net detailsview 控件。它的 DataSourceId 不同,我在 Page_load 中进行了相应的设置...(基于 LLBLgen 子类型,但这不是太重要)
我猜这是一个页面生命周期leaky abstraction我没有“得到”。
问题是我绑定到的字段可能存在也可能不存在,具体取决于数据源...
为了在某些条件下“禁用”绑定字段,我尝试将绑定字段包装在一个面板中,该面板在代码隐藏中设置为可见或不可见,但我仍然收到以下错误:
Sys.WebForms.PageRequestManagerServerErrorException:DataBinding:实体不包含名为“FilePrefix”的属性。
我在 pageload 中更改了 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;
}
}
}
【问题讨论】:
-
你能发布一些代码吗?
标签: asp.net datasource detailsview