我想尝试这样的事情:
但是当我点击GridView中的显示详细信息时,从cs文件中给出这行错误:
gvRow.FindControl("GridView2").Visible = true;
当我单击显示详细信息以查看其他表中ID_Proba =的ID到第一个表中的ID时,我正在尝试。
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="id" DataSourceID="SqlDataSource1"
onselectedindexchanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:BoundField
DataField="id"
HeaderText="id"
ReadOnly="True"
SortExpression="id" InsertVisible="False" />
<asp:BoundField
DataField="name"
HeaderText="name"
SortExpression="name" />
<asp:CommandField ShowSelectButton="True"
SelectText="Show Details"/>
</Columns>
<EmptyDataTemplate>
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
DataKeyNames="id" DataSourceID="SqlDataSource2" Visible="False">
<Columns>
<asp:BoundField DataField="id"
HeaderText="id"
SortExpression="id" InsertVisible="False" ReadOnly="True" />
<asp:BoundField DataField="id_proba"
HeaderText="id_proba"
SortExpression="id_proba" />
<asp:BoundField DataField="name"
HeaderText="name"
SortExpression="name" />
</Columns>
</asp:GridView>
</EmptyDataTemplate>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ProbaConnectionString %>"
SelectCommand="SELECT * FROM [Proba1]"></asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:ProbaConnectionString %>"
SelectCommand="SELECT * FROM [proba3] WHERE ([id_proba] = @id_proba)">
<SelectParameters>
<asp:Parameter Name="id_proba" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</form>
这是cs文件:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (GridViewRow gvRow in GridView1.Rows)
{
gvRow.FindControl("GridView2").Visible = true;
}
SqlDataSource2.SelectParameters[0].DefaultValue = GridView1.SelectedDataKey[0].ToString();
GridView1.SelectedRow.FindControl("GridView2").Visible = true;
}
答案 0 :(得分:0)
您需要从EmptyDataTemplate
中取出“GridView2”并将其放入TemplateField
。当您拥有数据时,根本不会创建EmptyDataTemplate
,并且每行中也没有网格,因为此模板适用于整个网格。
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="id" DataSourceID="SqlDataSource1"
onselectedindexchanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:BoundField
DataField="id"
HeaderText="id"
ReadOnly="True"
SortExpression="id" InsertVisible="False" />
<asp:BoundField
DataField="name"
HeaderText="name"
SortExpression="name" />
<asp:CommandField ShowSelectButton="True"
SelectText="Show Details"/>
<asp:TemplateField>
<ItemTemplate>
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
DataKeyNames="id" DataSourceID="SqlDataSource2" Visible="False">
<Columns>
<asp:BoundField DataField="id"
HeaderText="id"
SortExpression="id" InsertVisible="False" ReadOnly="True" />
<asp:BoundField DataField="id_proba"
HeaderText="id_proba"
SortExpression="id_proba" />
<asp:BoundField DataField="name"
HeaderText="name"
SortExpression="name" />
</Columns>
</asp:GridView>
</ItemTemplate>
<asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ProbaConnectionString %>"
SelectCommand="SELECT * FROM [Proba1]"></asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:ProbaConnectionString %>"
SelectCommand="SELECT * FROM [proba3] WHERE ([id_proba] = @id_proba)">
<SelectParameters>
<asp:Parameter Name="id_proba" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</form>