2013-10-02 68 views
2

我有一個簡單的GridView(ID爲'GridViewAttribs'),它顯示數據庫中的一些值。我希望有一個(嵌套的?)ListView,但由於某些原因,我不能使用asp:ControlParameter ControlID =「GridViewAttribs」PropertyName,因爲在我的(嵌套的)ListView中沒有返回結果。無法從GridView的TemplateField SELECT語句中獲取/使用ControlParameter

如果我不使用ControlParameter並硬編碼ListView的SELECT語句,那麼一切都按預期工作。

這裏是我的代碼:

<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」欄在那裏我得到「未返回的數據。」

但是,如果我拿出的asp:ControlParameter它呼籲@head_id和使用硬編碼的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)"> 

因此,出於某種原因,它這樣看來的「equipment_attrib_heading_id」的GridViewAttribs值不被傳遞到/拾起嵌套的asp:ControlParameter

在關閉的機會,我也試着更換:

PropertyName="SelectedDataKey.Values[equipment_attrib_heading_id]" 

有了:

PropertyName="SelectedValue" 

但這並沒有解決這個問題要麼,仍然沒有被返回的數據。

如果有幫助,我使用ASP.NET4(vb)的Visual Studio 2010。

回答

1

什麼,你可以嘗試使用隱藏字段和使用,對於controlparameter

<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> 
+0

GENIUS!完美的作品!但我不明白爲什麼這個作品?你能否進一步闡明我做錯了什麼? – Rich

0

控制這隻作品(似乎)如果HiddenField相同ItemTemplate內。如果DataSourceTemplateField之外的某處,在你想案件數據填充這是說在GridView上的另一列或領域的控制,那麼HiddenField將無法​​正常工作,因爲它在技術上隱藏的

解決此問題的方法是在代碼隱藏方面。這是我如何做...

我有一個TemplateField是指@StudentID,這實際上是在另一個模板列一個HiddenField

 <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> 

所以我定義@StudentID參數作爲簡單的參數,而不是ControlParameter,難道就不能在我的GridView其他地方拿起hiddenfield。

在代碼隱藏(C#)中,我通過DataBinding捕獲選擇事件。 在那裏,我特別尋找與我剛剛填充(或即將)的控件有關的行,然後查找HiddenField。

protected void listWeeksTracking_DataBinding(object sender, EventArgs e) 
{ 
    //set the parameter for the StudentID here as using hiddenfields does not work directly in asp.net, as they are hidden! 
    CheckBoxList cbl1 = (CheckBoxList)sender; 
    GridViewRow gvRow = (GridViewRow)cbl1.NamingContainer; 
    if (gvRow != null) 
    { 
     SqlDataSource sdsTETpastWeeks = (SqlDataSource)gvRow.FindControl("sdsTETpastWeeks"); 
     HiddenField hfStudentID = (HiddenField)gvRow.FindControl("hfStudentID"); 
     if (hfStudentID != null) sdsTETpastWeeks.SelectParameters["StudentID"].DefaultValue = hfStudentID.Value.ToString(); 
    } 
} 

瞧!