无法在ControlParameter中找到Control - 前置ContentPlaceHolderID无法正常工作

时间:2013-09-27 18:28:00

标签: c# asp.net gridview

我有经典的“ EditItemTemplate ”/“无法找到控件”错误,因为我的搜索参数引用了 ControlID GridView 中的 EditItemTemplate 内。我已经尝试使用GridView的ID( gridview $ controlID )预先设置我的ControlID,但它无效。如果必须的话,我很乐意使用“代码隐藏”方法,但我更愿意理解为什么前置方法不起作用。任何基于以下代码的提示?

谢谢!

<asp:UpdatePanel ID="upMatchEntry" ChildrenAsTriggers="false" EnableViewState="false" runat="server" UpdateMode="Conditional">
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="ddlGameWeek" EventName="SelectedIndexChanged" />
    </Triggers>
    <ContentTemplate>
        <asp:GridView ID="gvMatchSelect" DataKeyNames="MatchID,HomeTeamID,AwayTeamID" 
            DataSourceID="sdsMatches" runat="server" AutoGenerateEditButton="True"
            AutoGenerateColumns="False" EmptyDataText="No Matches Found" ContentPlaceHolderID="cpMatchSelect">
            <Columns>
                <asp:TemplateField HeaderText="Home Team" SortExpression="HomeTeamName" ItemStyle-HorizontalAlign="left">
                    <ItemTemplate><%# Eval("HomeTeamName") %></ItemTemplate>
                    <EditItemTemplate>
                        <asp:DropDownList runat="server" ID="ddlHomeTeam" DataSourceID="sdsHomeTeams" 
                            DataTextField="HomeTeamName" DataValueField="HomeTeamID" 
                            SelectedValue='<%# Bind("HomeTeamID") %>' AutoPostBack="True" />
                    </EditItemTemplate>
                    <ItemStyle HorizontalAlign="Left" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Away Team" SortExpression="AwayTeamName" ItemStyle-HorizontalAlign="left">
                    <ItemTemplate><%# Eval("AwayTeamName") %></ItemTemplate>
                    <EditItemTemplate>
                        <asp:DropDownList runat="server" ID="ddlAwayTeam" DataSourceID="sdsAwayTeams" 
                            DataTextField="AwayTeamName" DataValueField="AwayTeamID" 
                            SelectedValue='<%# Bind("AwayTeamID") %>' AutoPostBack="True"/>
                    </EditItemTemplate>
                    <ItemStyle HorizontalAlign="Left" />
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="sdsMatches" ConnectionString='<%$ ConnectionStrings:ApplicationServices %>' runat="server" 
            SelectCommand="pGameWeekMatchAdminDisplay" SelectCommandType="StoredProcedure">
            <SelectParameters>
                <asp:ControlParameter Name="GameWeekID" ControlID="ddlGameWeek" PropertyName="SelectedValue" Type="Int32" DefaultValue="-1" />
                <asp:ControlParameter Name="DivisionID" ControlID="ddlDivision" PropertyName="SelectedValue" Type="Int32" DefaultValue="-1" />
            </SelectParameters>
        </asp:SqlDataSource>
        <asp:SqlDataSource ID="sdsHomeTeams" ConnectionString='<%$ ConnectionStrings:ApplicationServices %>' runat="server"
            SelectCommand="SELECT 'Select Home Team' AS HomeTeamName, -1 AS HomeTeamID UNION SELECT Name AS HomeTeamName, TeamID AS HomeTeamID FROM Team WHERE DivisionID = @DivisionID AND TeamID <> @AwayTeamID" >
            <SelectParameters>
                <asp:ControlParameter Name="DivisionID" ControlID="ddlDivision" PropertyName="SelectedValue" Type="Int32" />
                <asp:ControlParameter Name="AwayTeamID" ControlID="gvMatchSelect$ddlAwayTeam" PropertyName="SelectedValue" Type="Int32" />
            </SelectParameters>
        </asp:SqlDataSource>
        <asp:SqlDataSource ID="sdsAwayTeams" ConnectionString='<%$ ConnectionStrings:ApplicationServices %>' runat="server"
            SelectCommand="SELECT 'Select Away Team' AS AwayTeamName, -1 AS AwayTeamID UNION SELECT Name AS AwayTeamName, TeamID AS AwayTeamID FROM Team WHERE DivisionID = @DivisionID AND TeamID <> @HomeTeamID" >
            <SelectParameters>
                <asp:ControlParameter Name="DivisionID" ControlID="ddlDivision" PropertyName="SelectedValue" Type="Int32" />
                <asp:ControlParameter Name="HomeTeamID" ControlID="gvMatchSelect$ddlHomeTeam" PropertyName="SelectedValue" Type="Int32" />
            </SelectParameters>
        </asp:SqlDataSource>
    </ContentTemplate>
</asp:UpdatePanel>

1 个答案:

答案 0 :(得分:0)

控件嵌套在实现GridViewRow接口的INamingContainer内。如果您使用的是最新版本的ASP.NET并且只需要单行编辑,那么您应该可以通过使控件ID静态来解决此问题:

ClientIDMode="Static"