2010-05-19 55 views
1

我有一個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; 
       } 
      } 

    } 
+0

你可以發佈一些代碼嗎? – 2010-05-19 12:44:46

回答

0

如果該字段不存在,則不能指定<%#Bind(「」)%>語句;如果該值可能存在或可能不存在,那麼您將不得不從程序代碼中分配值...使用findcontrol從特定項目中找到控件。

+0

如何? protected void SomeField_OnDataBinding(object sender,EventArgs e) ((TextBox)sender).Text = Eval(「SomeField」)。ToString(); } 只有Eval()可用,而不是Bind()。 Eval是隻讀的,我需要能夠將文本框中的值自動推回到數據庫中...就像使用<%#Bind(「」)%> – Konrad 2010-05-20 08:22:48

+0

所做的那樣,如果您需要自動推送那麼您需要確保該字段在兩個數據源中都存在,即使它在其他數據源中始終爲空......這是我知道使用DataSourceID的唯一方式。 – 2010-05-20 11:50:42

+0

我會接受你的回答。我將不得不手動綁定在detailsview中的OnItemInserting/Updating事件...在那裏我有更多的控制,並可以禁用這樣的方式不需要的字段。 – Konrad 2010-05-20 14:39:13

相關問題