开发者

How to conditionally disable a detailsview databound field

开发者 https://www.devze.com 2022-12-30 16:36 出处:网络
I have an ASP.Net detailsview control. Its DataSourceId varies and I set it in Page_load accordingly... (based on LLBLgen sub types, but this is not too important)

I have an ASP.Net detailsview control. Its DataSourceId varies and I set it in Page_load accordingly... (based on LLBLgen sub types, but this is not too important)

I guess this is a page lifecycle leaky abstraction I am not "getting".

The problem is that I bind to fields that may or may no be there depending on the datasource...

To "disable" a bound field under certain conditions I've tried wrapping the bound field in a panel which I set to visible or invisible in code-behind, but I still get the following error:

Sys.WebForms.PageRequestManagerServerErrorException: DataBinding: Entity does not contain a property with the name 'FilePrefix'.

I change the detaislview.datasourceid in pageload...might be too late in lifecycle.

I don't want to bind to that field, as it doesn't exist for the new datasource, but it tries to do so none-the-less and I get the error.

Any ideas? ;)

[Edited]: Code as requested...

ASP, detailsview bound column:

<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>

Code-behind: (determine datasource, detaislview is visible on postback only as grid is displayed in initial page load.)

    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";
                  开发者_如何学JAVA  break;

...

Show/hide panel:

        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;
                }
            }

    }


You can't assign a <%# Bind("") %> statement if the field isn't there; you would have to programmbly assign the value from code-behind if the value may or may not be there... using findcontrol to find the control from the specific item.

0

精彩评论

暂无评论...
验证码 换一张
取 消