Search code examples
c#asp.netgridviewobjectdatasourceformview

ObjectDataSource can not find control parameter sometimes


I have a formview that is populated with an ObjectDataSource and it works great. Nested in that is a gridview also populated with a nested ODS that works perfect 99% of the time. Also nested in the formview is another ODS populated from the same control that populates the other nested datasource that works perfect all of the time. Code below to make that make sense.

The problem is if you change pages it all works fine most of the time. There are 2 search features built into the page. That is where the error occurs. If you use either of them without changing pages it works great. If you use one of them and then change pages and try to use either one of them the page crashes with an error that says something like Control Parameter 'X' is not found in 'Y'. The problem is that it is not a true error because there is a button that displays the bootstrap model which displays its gridview with no issues and the ODS is based on exactly the same control so it is obviously there. I imagine that it is related to the events, but they step through just fine. I am not sure what could be causing it.

Here is a little test project that is basically the same setup only paging is setup in the formview and the ODS1 in the real project and it works perfectly.

<asp:FormView ID="FormView1" runat="server" DataSourceID="ObjectDataSource1">
    <ItemTemplate>
        ImportantID:
        <asp:Label Text='<%# Bind("ImportantID") %>' runat="server" ID="ImportantIDLabel" Visible="false" /><br />
        Name:
        <asp:Label Text='<%# Bind("Name") %>' runat="server" ID="NameLabel" /><br />
        <div id="nestedContent">
            <asp:ObjectDataSource ID="ObjectDataSource2" runat="server" SelectMethod="listNested" TypeName="TestProject.DataSourceClass">
                <SelectParameters>
                    <asp:ControlParameter ControlID="ImportantIDLabel" PropertyName="Text" DefaultValue="-1" Name="ImportantIDFromOnject1ForNestedObject" Type="Int32"></asp:ControlParameter>
                </SelectParameters>
            </asp:ObjectDataSource>
            <asp:GridView ID="GridView1" runat="server" DataSourceID="ObjectDataSource2" AutoGenerateColumns="False">
                <Columns>
                    <asp:BoundField DataField="YetAnotherProperty" HeaderText="YetAnotherProperty" SortExpression="YetAnotherProperty"></asp:BoundField>
                    <asp:BoundField DataField="OtherPropertiesGoInThisClass" HeaderText="OtherPropertiesGoInThisClass" SortExpression="OtherPropertiesGoInThisClass"></asp:BoundField>
                </Columns>
            </asp:GridView>
        </div>
        SomeProperty:
        <asp:Label Text='<%# Bind("SomeProperty") %>' runat="server" ID="SomePropertyLabel" /><br />
        SomeOtherProperty:
        <asp:Label Text='<%# Bind("SomeOtherProperty") %>' runat="server" ID="SomeOtherPropertyLabel" /><br />
        <asp:ObjectDataSource ID="ObjectDataSource3" runat="server" SelectMethod="GetDataObject2" TypeName="TestProject.DataSourceClass">
            <SelectParameters>
                <asp:ControlParameter ControlID="ImportantIDLabel" PropertyName="Text" DefaultValue="-1" Name="ImportantID" Type="Int32"></asp:ControlParameter>
            </SelectParameters>
        </asp:ObjectDataSource>
        <div id="BootStrapModalPopup">
            This displays the data with no problems
        </div>
    </ItemTemplate>
</asp:FormView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetDataObject1" TypeName="TestProject.DataSourceClass">
    <SelectParameters>
        <asp:Parameter DefaultValue="-1" Name="ImportantID" Type="Int32"></asp:Parameter>
        <asp:Parameter DefaultValue="" Name="Name" Type="String"></asp:Parameter>
        <asp:Parameter Name="SomeProperty" Type="String"></asp:Parameter>
    </SelectParameters>
</asp:ObjectDataSource>

I would appreciate any ideas of where to look next. By the way I have used a textbox, hiddencontrol and Session variable for the ID field and nothing makes a difference for it. If I disable the ODS2 it goes back to working perfectly also but I need that data.

Thanks

Jimmy


Solution

  • I actually lucked into the answer for this. It was not my nesting it was my paging. I will leave the question for anyone that has a similar problem since I don't see any successful errors to this. The issue I faced was I needed to reset my pageindex on search. It worked great searching from page one, but searching from any other page is an empty return due to the indexing so the controls did not exist. Hopefully it will help someone to search that if you run into this issue. Just set pageindex on the formview back to 0.