我是ASP.NET新手。我有一个列出公司名称的基本工作gridview(参见下面的代码)。我想要做的是在每个公司下提供另一个联系人子列表,在“子列表”中缩进一点。
我想以某种方式遍历Gridview数据,然后运行联系人查询,将“从Import_Contact中选择*,其中Company ='来自GridView DataSource的公司'”,然后显示子集,如果每个公司下面的联系人。输出应如下所示:
1) Company A
1) Contact x
2) Contact y
1) Contact x
2) Company B
1) Contact h
2) Contact i
所以......
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False"
AllowPaging="False"
SelectedIndex="0"
DataSourceID="dsUploadList"
ShowHeaderWhenEmpty="True" EmptyDataText="No records Found"
EnableRowClick = "False" Width="100%" AllowSorting="True"
AutoGenerateEditButton="False"
>
<Columns>
<asp:BoundField ReadOnly="True" HeaderText="Row" ItemStyle-HorizontalAlign="Left" ItemStyle-Width="5%"
InsertVisible="False" DataField="Row"
SortExpression="Row" />
<asp:BoundField ReadOnly="True" HeaderText="Company" ItemStyle-HorizontalAlign="Left" ItemStyle-Width="30%"
InsertVisible="False" DataField="Company"
SortExpression="Company" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="dsUploadList" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnApps %>"
SelectCommand="SELECT DISTINCT(Company)
, ROW_NUMBER() OVER (ORDER By Company ASC) AS Row
FROM dbo.Import_CompanyContact icc WHERE RefNum = @RefNum "
>
<SelectParameters>
<asp:QueryStringParameter Name="RefNum" QueryStringField="RefNum" DefaultValue="0" Type="Int16" />
</SelectParameters>
有关如何完成此任务的任何建议?一个很好的代码示例。感谢。
答案 0 :(得分:4)
我会使用ListView
或DataList
,因为操作显示会更容易。这应该会让你走上正确的道路:
<table width="595px">
<asp:DataList BackColor="#ffffff" id="DataList1" DataKeyField="<ID>" OnItemDataBound="DataList1_ItemDataBound" runat="server" Width="100%">
<ItemTemplate>
<tr>
<td>
<asp:LinkButton ID="LinkButton1" runat="server" Text="+" OnCommand="LinkButton1_Command" CommandArgument='<%#Container.ItemIndex%>'></asp:LinkButton>
</td>
<td><%#Eval("<COLUMN NAME>")%></td>
<td><%#Eval("<COLUMN NAME>")%></td>
<td><%#Eval("<COLUMN NAME>")%></td>
</tr>
<asp:Panel ID="pnlChildView" runat="server">
<asp:DataList ID="DataList2" runat="server" Width="100%">
<ItemTemplate>
<tr>
<td><%#Eval("<CHILD OLUMN NAME>")%></td>
<td><%#Eval("<CHILD COLUMN NAME>")%></</td>
<td><%#Eval("<CHILD COLUMN NAME>")%></</td>
</tr>
</ItemTemplate>
</asp:DataList>
</asp:Panel>
</ItemTemplate>
</asp:DataList>
</table>
当用户单击DataList1中的LinkButton / Button时,请执行以下操作:
protected void LinkButton1_Command(object sender, CommandEventArgs e)
{
//pass index of item in command argument
int itemIndex = Convert.ToInt32(e.CommandArgument);
//depending on your needs bind the details on demand
//or preload during ItemDataBound
Panel childViewPanel = (Panel)DataList1.Items[itemIndex].FindControl("pnlChildView");
if (childViewPanel != null)
{
//toggle visibility of childViewPanel and bind child list if panel is visible
if (childViewPanel.Visible)
{
DataList childList = childViewPanel.FindControl("DataList2");
if (childList != null)
{
int keyValue = (int)DataList1.DataKeys[itemIndex];
//bind the list using DataList1 data key value
childList.DataSource = <DATA SOURCE>; //get data using keyValue
childList.DataBind();
}
}
}
}