Nathanus Nathanus - 1 month ago 8
ASP.NET (C#) Question

Is it possible to use a control in a different content tag as a parameter?

I currently have two Web Controls, one being a CheckBoxList, and the other a GridView. The GridView's data configuration has a parameter in it's query which is linked to the selected values from the CheckBoxList. However, when the two controls are in separate content tags like so:

<asp:Content ID="ListPanel" runat="server" ContentPlaceHolderID="LeftContent">
<h3>Pick Info Here</h3>
<asp:CheckBoxList ID="cbList1" runat="server"
DataSourceID="TestDataSource"
DataTextField="St" DataValueField="St" RepeatColumns="2">
</asp:CheckBoxList>
</asp:Content>

<asp:Content ID="ResultsPanel" runat="server" ContentPlaceHolderID="RightContent">
<asp:GridView ID="gView1" runat="server"
AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" DataSourceID="TestDS1">
<Columns>
...
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="TestDS1" runat="server"
...
SelectCommand="SELECT ST FROM [Table] where ST = ?">
<SelectParameters>
<asp:ControlParameter ControlID="cbList1" Name="?"
PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
</asp:Content>


It works fine when I put both controls in the same
<asp:Content>
tag, but things call apart with an error of
System.InvalidOperationException: Could not find control 'cbList1' in ControlParameter '?'
. Is there a way to keep the controls separate, or do they have to be inside the same content tag?

Answer

Apparently this can be done by prefixing the ContentPlaceHolderID to the ControlID of the parameter entry. In my case, the code would be:

SelectParameters>
        <asp:ControlParameter ControlID="LeftContent:cbList1" Name="?" 
            PropertyName="SelectedValue" />
    </SelectParameters>  

since the Control the parameter reads from is in the LeftContent tag.