无法在TemplateField SELECT语句中从GridView获取/使用ControlParameter

时间:2013-10-02 10:56:51

标签: asp.net

我有一个简单的GridView(ID为'GridViewAttribs'),它显示数据库中的一些值。

在我的GridViewAttribs中,我希望有一个(嵌套?)ListView,但出于某种原因,我不能使用asp:ControlParameter ControlID =“GridViewAttribs”PropertyName,因为我的(嵌套)ListView中没有返回任何结果。

如果我不使用ControlParameter并将我的SELECT语句硬编码为ListView,一切都按预期工作。

这是我的代码:

<asp:SqlDataSource ID="SqlDataSourceAttribHeadings" runat="server" 
    ConnectionString="<%$ ConnectionStrings:customer_support_devConnectionString %>" 
    ProviderName="<%$ ConnectionStrings:customer_support_devConnectionString.ProviderName %>" 
    SelectCommand="SELECT equipment_attrib_heading_id, equipment_model_id, equipment_attrib_name FROM equipment_new_attrib_headings WHERE (equipment_model_id = @equipment_model_id) AND (equipment_attrib_heading_deleted = 0) ORDER BY equipment_attrib_name">
    <SelectParameters>
        <asp:SessionParameter DefaultValue="" Name="equipment_model_id" 
            SessionField="EquipmentModelID" Type="Int32" />
    </SelectParameters>
</asp:SqlDataSource>

<asp:GridView ID="GridViewAttribs" runat="server" AutoGenerateColumns="False" 
    CellPadding="4" DataKeyNames="equipment_attrib_heading_id" 
    DataSourceID="SqlDataSourceAttribHeadings" ForeColor="#333333" GridLines="None" 
    style="margin-right: 0px">
    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    <Columns>
        <asp:BoundField DataField="equipment_attrib_heading_id" 
            HeaderText="equipment_attrib_heading_id" ReadOnly="True" 
            SortExpression="equipment_attrib_heading_id" />
        <asp:BoundField DataField="equipment_model_id" HeaderText="equipment_model_id" 
            SortExpression="equipment_model_id" />
        <asp:BoundField DataField="equipment_attrib_name" 
            HeaderText="equipment_attrib_name" SortExpression="equipment_attrib_name" />

        <asp:TemplateField HeaderText="equipment_attrib_value_details" SortExpression="equipment_attrib_value_details">
            <ItemTemplate>

                <asp:SqlDataSource ID="SqlDataSourceAttribValues" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:customer_support_devConnectionString %>" 
                    ProviderName="<%$ ConnectionStrings:customer_support_devConnectionString.ProviderName %>" 
                    SelectCommand="SELECT [equipment_attrib_value_details] FROM [equipment_new_attrib_values] WHERE (equipment_attrib_heading_id = @head_id) AND (equipment_id = @equipment_id) AND (equipment_attrib_value_deleted = 0)">
                    <SelectParameters>
                        <asp:QueryStringParameter Name="equipment_id" QueryStringField="id" 
                            Type="Int32" />
                        <asp:ControlParameter ControlID="GridViewAttribs" 
                            Name="head_id" PropertyName="SelectedDataKey.Values[equipment_attrib_heading_id]" Type="Int32" />
                    </SelectParameters>
                </asp:SqlDataSource>

                <asp:ListView ID="ListViewAttribValues" runat="server" 
                    DataSourceID="SqlDataSourceAttribValues">
                    <EmptyDataTemplate>
                        <table runat="server" style="">
                            <tr><td>No data was returned.</td></tr>
                        </table>
                    </EmptyDataTemplate>
                    <ItemTemplate>
                        <tr style="">
                            <td><asp:Label ID="equipment_attrib_value_detailsLabel" runat="server" Text='<%# Eval("equipment_attrib_value_details") %>' /></td>
                        </tr>
                    </ItemTemplate>
                    <LayoutTemplate>
                        <table ID="itemPlaceholderContainer" runat="server" border="0" style="">
                            <tr ID="itemPlaceholder" runat="server">
                            </tr>
                        </table>
                    </LayoutTemplate>
                </asp:ListView>

            </ItemTemplate>
        </asp:TemplateField>


    </Columns>

</asp:GridView>

GridView显示所有列都没有问题,除了“equipment_attrib_value_details”列,我得到“没有返回数据。”

但是,如果我取出调用@head_id的asp:ControlParameter并使用这个硬编码的SELECT语句,那么我会看到正确的(尽管是硬编码的)值:

SelectCommand="SELECT [equipment_attrib_value_details] FROM [equipment_new_attrib_values] WHERE (equipment_attrib_heading_id = 3) AND (equipment_id = @equipment_id) AND (equipment_attrib_value_deleted = 0)">

因此,出于某种原因,看起来嵌套的asp:ControlParameter

似乎没有将“equipment_attrib_heading_id”的GridViewAttribs值传递给/拾取

关于失败的机会,我也尝试过替换:

PropertyName="SelectedDataKey.Values[equipment_attrib_heading_id]"

使用:

PropertyName="SelectedValue"

但这也没有解决问题,仍然没有返回数据。

如果有帮助,我正在使用Visual Studio 2010和ASP.NET4(vb)。

2 个答案:

答案 0 :(得分:1)

您可以尝试使用隐藏字段并将该控件用于控制参数

<asp:TemplateField HeaderText="equipment_attrib_value_details" SortExpression="equipment_attrib_value_details">
                    <ItemTemplate>
                         <asp:HiddenField runat="server" ID="heading_id" Value='<%# Eval("equipment_attrib_heading_id") %>' />
                        <asp:SqlDataSource ID="SqlDataSourceAttribValues" runat="server" 
                            ConnectionString="<%$ ConnectionStrings:customer_support_devConnectionString %>" 
                            ProviderName="<%$ ConnectionStrings:customer_support_devConnectionString.ProviderName %>" 
                            SelectCommand="SELECT [equipment_attrib_value_details] FROM [equipment_new_attrib_values] WHERE (equipment_attrib_heading_id = @head_id) AND (equipment_id = @equipment_id) AND (equipment_attrib_value_deleted = 0)">
                            <SelectParameters>
                                <asp:QueryStringParameter Name="equipment_id" QueryStringField="id" Type="Int32" />
                                <asp:ControlParameter ControlID="heading_id" Name="head_id" PropertyName="Value" Type="Int32" />
                            </SelectParameters>
                        </asp:SqlDataSource>

                        <asp:ListView ID="ListViewAttribValues" runat="server" 
                            DataSourceID="SqlDataSourceAttribValues">
                            <EmptyDataTemplate>
                                <table id="Table2" runat="server" style="">
                                    <tr><td>No data was returned.</td></tr>
                                </table>
                            </EmptyDataTemplate>
                            <ItemTemplate>
                                <tr style="">
                                    <td><asp:Label ID="equipment_attrib_value_detailsLabel" runat="server" Text='<%# Eval("equipment_attrib_value_details") %>' /></td>
                                </tr>
                            </ItemTemplate>
                            <LayoutTemplate>
                                <table ID="itemPlaceholderContainer" runat="server" border="0" style="">
                                    <tr ID="itemPlaceholder" runat="server">
                                    </tr>
                                </table>
                 </LayoutTemplate>
         </asp:ListView>
     </ItemTemplate>
</asp:TemplateField>

答案 1 :(得分:0)

如果HiddenField位于同一ItemTemplate内,这只会起作用(似乎)。如果DataSource位于TemplateField之外的某个位置,如果您希望数据填充一个控件,就像在GridView的另一个列或字段上那样,那么{ {1}}无效,因为它在技术上隐藏

克服这个问题的方法是代码隐藏。我是这样做的......

我的HiddenField指的是@StudentID,实际上是另一个TemplateField 中的TemplateField

HiddenField

所以我将@StudentID参数定义为一个简单的参数,而不是 <asp:TemplateField HeaderText="Currently&lt;br/&gt;Tracking&lt;br/&gt;(past weeks)"> <ItemTemplate> <asp:CheckBoxList ID="listWeeksTracking" runat="server" DataSourceID="sdsTETpastWeeks" DataTextField="WeekNo" DataValueField="WeekNo" OnDataBound="listWeeksTracking_DataBound" OnDataBinding="listWeeksTracking_DataBinding"></asp:CheckBoxList> <br /> <asp:SqlDataSource ID="sdsTETpastWeeks" runat="server" ConnectionString="<%$ ConnectionStrings:ATCNTV1ConnectionString %>" SelectCommand="SELECT tb.WeekNo, ISNULL((SELECT TOP 1 ts.ETRTWtracking FROM tblTETMeetingStudent AS ts INNER JOIN tblTETMeeting AS tm ON tm.TETmeetingID = ts.TETmeetingID AND tm.WeekNo = tb.WeekNo WHERE ts.StudentID = @StudentID),0) AS Tracking FROM tblTETInstructionalTrainingBlocks AS tb WHERE tb.WeekNo <= @WeekNo AND tb.Active = 1 AND tb.YearNum = YEAR(@TETdate) ORDER BY tb.WeekNo"> <SelectParameters> <asp:Parameter Name="StudentID" Type="string" /> <asp:ControlParameter ControlID="ddlWeekNo" Name="WeekNo" PropertyName="SelectedValue" Type="int16" /> <asp:ControlParameter ControlID="lblTETdate" Name="TETdate" Type="DateTime" /> </SelectParameters> </asp:SqlDataSource> </ItemTemplate> <ItemStyle HorizontalAlign="Center" /> <HeaderStyle Wrap="False" /> </asp:TemplateField> ,它是否能够在我的GridView中的其他位置拾取隐藏字段。

在代码隐藏(C#)中,我通过 DataBinding 捕获Selecting事件。 在那里,我专门寻找与我刚刚填充(或即将)的控件相关的行,然后我寻找HiddenField。

ControlParameter

瞧!