我正在嵌套三个GridView,每个GridView依赖于之前的一个。我完成了asp结构,每个都从各自的数据源(asp:aspx页面中的asp:SqlDataSource)中提取。但是,我无法弄清楚如何构建关系,因此每个只提取与嵌套网格的行相关的数据。
实施例: 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(其父级)的行,但这也不起作用。
错误:索引超出范围。必须是非负数且小于集合的大小。 参数名称: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>
答案 0 :(得分:1)
首先,我建议更改看似错误的Parameter
。假设CatLinkULink
是一个字符串字段,我会将SelectParameters更改为:
<SelectParameters>
<asp:Parameter Name="catlinkulink" Type="String" DefaultValue="" />
</SelectParameters>
并在CategoryGrid中移动SqlDataSource,添加一个隐藏字段。以下是CategoryGrid标记的外观:
<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>
<asp:HiddenField ID="hdnCatlinkulink" runat="server" Value='<%#Eval("catlinkulink") %>' />
<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" Type="String" DefaultValue="" />
</SelectParameters>
</asp:SqlDataSource>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:gridview>
在代码中我会设置参数:
protected void CategoryOnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView TargetGrid = e.Row.FindControl("TargetGrid") as GridView;
HiddenField hdnCatlinkulink = e.Row.FindControl("hdnCatlinkulink") as HiddenField;
SqlDataSource TargetLinks = e.Row.FindControl("TargetLinks") as SqlDataSource;
if (TargetGrid != null && hdnCatlinkulink != null && TargetLinks != null)
{
string catlinkulink = hdnCatlinkulink.Value;
TargetLinks.SelectParameters[0].DefaultValue = catlinkulink;
}
}
}
免责声明:我没有测试上面的代码,可能需要调整一下。
答案 1 :(得分:-1)
您可以在hiden div中设置文本框,以便在触发网格选定事件时获取值。 然后你可以在sqldatasource中设置这个textbox.text的参数。 这就是我为这种情况所做的事情