嵌套gridview没有数据,如果父gridview在(where = querystring)上被填充,这里需要帮助
本文关键字:gridview 填充 这里 querystring 帮助 where 数据 如果 嵌套 | 更新日期: 2023-09-27 18:18:22
问题如上所述。我有一个父gridview,它将通过获取查询字符串来填充。然而,子网格视图(嵌套)将不会被填充。我也不明白为什么。任何帮助吗?
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BorderColor="Black" BorderStyle="Solid" DataKeyNames="QuestionID"
DataSourceID="SqlDataSource1" Width="447px">
<Columns>
<asp:BoundField DataField="QuestionID" HeaderText="QuestionID" ReadOnly="True"
SortExpression="QuestionID" />
<asp:BoundField DataField="SurveyID" HeaderText="SurveyID"
SortExpression="SurveyID" />
<asp:BoundField DataField="QuestionTitle" HeaderText="QuestionTitle"
SortExpression="QuestionTitle" />
<asp:BoundField DataField="AnswerType" HeaderText="AnswerType"
SortExpression="AnswerType" />
<asp:TemplateField>
<ItemTemplate>
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
BorderStyle="Solid" DataSourceID="SqlDataSource2">
<Columns>
<asp:BoundField DataField="AnswerValue" HeaderText="AnswerValue"
SortExpression="AnswerValue" />
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:myConnectionString %>"
SelectCommand="SELECT * FROM [Question] WHERE ([SurveyID] = @SurveyID)">
<SelectParameters>
<asp:QueryStringParameter Name="SurveyID" QueryStringField="sid"
Type="String" />
</SelectParameters>
</asp:SqlDataSource>
</div>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:myConnectionString %>"
SelectCommand="SELECT AnswerValue FROM Results WHERE (QuestionID = @QuestionID)">
<SelectParameters>
<asp:Parameter Name="QuestionID" />
</SelectParameters>
</asp:SqlDataSource>
可以将HiddenField
添加到GridView1的ItemTemplate中,并与QuestionID
绑定。
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
...
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="HiddenField1" runat="server"
Value='<%# Eval("QuestionID") %>' />
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
BorderStyle="Solid" DataSourceID="SqlDataSource2">
<Columns>
<asp:BoundField DataField="AnswerValue" HeaderText="AnswerValue"
SortExpression="AnswerValue" />
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
并使用ControlParameter比较QuestionID与SqlDataSource2。
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:myConnectionString %>"
SelectCommand="SELECT AnswerValue FROM Results WHERE (QuestionID = @QuestionID)">
<SelectParameters>
<asp:ControlParameter ControlID="HiddenField1" Name="QuestionID"
PropertyName="Value" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>