我嵌套了三個GridView,其中每個都依賴於前一個。我已經完成了asp結構,並且每個都從各自的數據源(aspx頁面中的asp:SqlDataSource)提取。但是,我不知道如何構建關係,因此每個只拉取與嵌套網格的行相關的數據。將參數從父項傳遞給子項ASP GridView(C#代碼隱藏)
例子: Similar but I don't need the drill downs
Also very like what I'm looking for but this uses EntityDataSource
這些例子都幫助我瞭解我需要的RowDataBound事件,使這項工作。大!但我無法弄清楚如何讓RowDataBound事件給我每行的唯一參數,這些參數必須通過我的數據源來運行,以便在下一個GridView中生成數據。
代碼隱藏:
protected void CategoryGrid_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
//need to set my @catlinkulink parameter here
}
protected void CategoryGrid_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void CategoryOnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView CategoryGridFC = DomainGrid.Rows[e.Row.RowIndex].FindControl("CategoryGrid") as GridView;
string catlinkulink = CategoryGridFC.DataKeys[e.Row.RowIndex].Values["catlinkulink"].ToString();
TargetLinks.SelectParameters[0].DefaultValue = catlinkulink;
}
}
下面是從ASPX數據源。這是針對依賴於從類別網格的每一行拉取的參數的網格。
<asp:SqlDataSource ID="TargetLinks" runat="server"
ConnectionString="<%$ ConnectionStrings:MyDB %>"
SelectCommand="SELECT t.ULink AS targetlinkulink, TargetDE
FROM targetLink t
INNER JOIN IntTargets it ON t.TargetULink = it.ULink
INNER JOIN IntCategoryLink cl ON t.CatLinkULink = cl.ULink
WHERE t.CatLinkULink = @catlinkulink"
>
<SelectParameters>
<asp:Parameter Name="catlinkulink" DefaultValue="0" Type="Empty" Size="0" ConvertEmptyStringToNull="False" DbType="Object" />
</SelectParameters>
</asp:SqlDataSource>
我多久,這是道歉,但我要確保我提供足夠的信息給的什麼,我試圖做一個清晰的思路。感謝您的任何輸入。我總是在這裏學到很多東西 - 即使它是完成某件事的不同或更好的方法。
根據以下建議添加了TargetLinks位。我也改變了CategoryGrid的FindControl來查看DomainGrid(它的父級)的行,但那也行不通。
Error: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
這裏是嵌套的GridView的代碼。
<asp:GridView ID="DomainGrid" runat="server"
onselectedindexchanged="DomainGrid_SelectedIndexChanged"
DataSourceID="DomainLinks" AutoGenerateColumns="False" DataKeyNames="domainlinkulink">
<Columns>
<asp:BoundField DataField="DomainDE" ShowHeader="False"
SortExpression="DomainDE" HeaderStyle-Width="125">
</asp:BoundField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:DropDownList ID="IntervCatDrop" runat="server" DataSourceID="IntervCatDD"
DataValueField="ulink" DataTextField="IntCategories">
</asp:DropDownList> <asp:Button ID="AddIntCat" runat="server" Text="+" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:GridView ID="CategoryGrid" runat="server" DataKeyNames="catlinkulink"
DataSourceID="IntCategoryLinks" AutoGenerateColumns="false"
onselectedindexchanged="CategoryGrid_SelectedIndexChanged"
onrowdatabound="CategoryOnRowDataBound">
<Columns>
<asp:BoundField DataField="IntCategories" ShowHeader="False"
SortExpression="Categories" HeaderStyle-Width="125"></asp:BoundField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:DropDownList ID="TargetDrop" runat="server" DataSourceID="TargetsDD"
DataValueField="ulink" DataTextField="TargetDE">
</asp:DropDownList> <asp:Button ID="AddTarget" runat="server" Text="+" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="false">
<ItemTemplate>
<asp:GridView ID="TargetGrid" runat="server" DataSourceID="TargetLinks" AutoGenerateColumns="False"
DataKeyNames="targetlinkulink" onselectedindexchanged="TargetGrid_SelectedIndexChanged"
>
<Columns>
<asp:BoundField DataField="TargetDE" ShowHeader="false" SortExpression="TargetDE" />
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
在行綁定事件中設置asp:參數值,如下所示: TargetLinks.SelectParameters [0] .DefaultValue = catlinkulink; – PushCode
謝謝!我補充說。 –
@R_Scott - 你得到的錯誤是什麼,你只是在評論中發佈了代碼? –