我有一個ASP.Net detailsview控件。它的DataSourceId變化,我相應地設置在Page_load ...(基於LLBLgen子類型,但這不是太重要)如何有條件地禁用detailsview數據綁定字段
我想這是一個頁面生命週期leaky abstraction我不是「得到」。
的問題是,我綁定到字段可能會或可能不會是有依據的數據源...
爲「Disable」在一定條件下綁定的字段我已經試過包裝在一個綁定字段面板,我設置爲可見或不可見的代碼隱藏,但我仍然得到以下錯誤:
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;
}
}
}
你可以發佈一些代碼嗎? – 2010-05-19 12:44:46