2

我是 ASP.NET 的新手。我有一个列出公司名称的基本工作网格视图(参见下面的代码)。我想做的是在每个公司下提供另一个联系人子列表,在“子列表”中缩进一点。

我想以某种方式遍历 Gridview 数据,然后运行联系人查询,将是“Select * from Import_Contact where Company = 'The Company from the 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>

关于如何做到这一点的任何建议?一个很棒的代码示例。谢谢。

4

1 回答 1

4

我会使用ListVieworDataList代替,因为它会更容易操作显示。这应该使您走上正确的道路:

<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 中的 LinkBut​​ton/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();
            }  
        }
    }
}
于 2013-09-27T18:42:11.117 回答