0

This is probably another case where I am going about it all wrong. I blame myself.

But I am confused by the behavior of FindControl in the DetailsView as shown below.

FindControl works until I remove a field from the DataControlFieldCollection. If it matters, this is in a MasterPage.

I've experimented with moving the code that removes the DataControlField in Page_Load, Page_PreRender as well as the ModeChanged and ModeChanging events of dvCategories.

I've also tried calling dvCategories.Databind() from many places to no avail.

Is this expected behavior, and is there a work around?

protected void ldsCategories_OnInserting(object sender, LinqDataSourceInsertEventArgs e)
{
    //Here findcontrol works unless the commented code from the button event handler is fired.
    TextBox tb = (TextBox) dvCategories.FindControl("txtInsertParentId");
    string ParentName = tb.Text;

}

protected void btnNew_click(object sender, EventArgs e)
{
    dvCategories.ChangeMode(DetailsViewMode.Insert);
    //dvCategories.Fields.RemoveAt(3);

}

ASP Code:

    <asp:DetailsView ID="dvCategories" runat="server" AutoGenerateEditButton="True" AutoGenerateInsertButton="True" DataSourceID="ldsCategories" AutoGenerateRows="False" DataKeyNames="CategoryId" DefaultMode="ReadOnly">
        <Fields>
            <asp:BoundField DataField="CategoryId" HeaderText="CategoryId" InsertVisible="False" ReadOnly="True" SortExpression="CategoryId" />
            <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
            <asp:TemplateField HeaderText="Parent Name">
                <InsertItemTemplate>
                    <asp:TextBox ID="txtInsertParentId" runat="server"></asp:TextBox>
                </InsertItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="ContentPath" HeaderText="ContentPath" SortExpression="ContentPath" />
            <asp:BoundField DataField="DisplayUrl" HeaderText="DisplayUrl" SortExpression="DisplayUrl" />
            <asp:BoundField DataField="MetaDesc" HeaderText="MetaDesc" SortExpression="MetaDesc" />
        </Fields>    
</asp:DetailsView>

<asp:LinqDataSource ID="ldsCategories" runat="server" ContextTypeName="ProductsDataContext" TableName="Categories" EnableInsert="True" EnableUpdate="True" OnInserting="ldsCategories_OnInserting" EntityTypeName="" Where="CategoryId == @CategoryId &amp;&amp; Name == @Name" OnInserted="ldsCategories_OnInserted">
    <WhereParameters>        
        <asp:ControlParameter ControlID="txtCategory" Name="Name" PropertyName="Text" Type="String" DefaultValue="" />
    </WhereParameters>
    </asp:LinqDataSource>
personaelit
  • 1,633
  • 3
  • 31
  • 59

1 Answers1

0

I really am a dummy. I don't need to remove the field, just hide it.

So this works:

dvCategories.Fields[3].Visible = false;

And FindControl works as well with this technique. I still don't know why modifying the collection breaks findcontrol...

personaelit
  • 1,633
  • 3
  • 31
  • 59