切换显示子DataList

时间:2013-09-28 00:52:45

标签: c# asp.net

我尝试切换显示子网数据(childList)。以下代码工作正常,但无法显示。当我单击(+)符号时,ChildList dataList展开并显示列表子集数据(Contact)。但是,我希望在扩展childList数据时将(+)更改为( - )。当用户点击( - )时,它会折叠(子列表消失)

知道怎么做吗?一个示例代码会很棒,因为我是ASP.NET的新手。

<table width="595px">
    <asp:DataList BackColor="#ffffff" id="DataList1" DataSourceID="dsCompanyList"  runat="server" Width="100%" DataKeyField="Company">     
        <ItemTemplate>
           <tr>
              <td>
                  <asp:LinkButton ID="LinkButton1" runat="server" Text="+" CommandArgument='<%#Container.ItemIndex%>'
                    OnCommand="LinkButton1_Command"  
                    ></asp:LinkButton>    
              </td>
              <td><%#Eval("Row")%></td>
              <td><%#Eval("Company")%></td>
           </tr>
           <asp:Panel ID="pnlChildView" runat="server">
               <asp:DataList ID="childList" runat="server" Width="100%">
                   <ItemTemplate>
                       <tr>
                          <td><%#Eval("FirstName")%></td>
                          <td><%#Eval("LastName")%></td>                        
                       </tr>
                   </ItemTemplate>
               </asp:DataList>
           </asp:Panel>
        </ItemTemplate>
    </asp:DataList>
</table>

代码背后:

public partial class _CompanyList2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    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

            DataList childList = (DataList)childViewPanel.FindControl("childList");
            if (childList != null)
            {
                //int keyValue = (int)DataList1.DataKeys[itemIndex];
                string keyValue = (string)DataList1.DataKeys[itemIndex];
                using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connApps"].ConnectionString))
                {
                    con.Open();
                    using (SqlCommand cmd = new SqlCommand("SELECT FirstName, LastName FROM dbo.Import_CompanyContact WHERE REPLACE(Company, '''', '') = '" + keyValue + "'", con))
                    {
                        cmd.CommandType = CommandType.Text;
                        using (SqlDataReader dr = cmd.ExecuteReader())
                        {
                            childList.DataSource = dr;
                            childList.DataBind();
                        }
                    }
                }
            }
        }
    }

}

0 个答案:

没有答案