0

我是 ASP.NET 和 C# 的新手。当用户单击 (+) 符号时,我想刷新第二个 DataList (DataList2)。

Datalist1 将列出所有带有 (+) 号列的公司记录。如果用户单击 (+),它将在公司下方展开 (DataList2) 并列出所有联系人姓名。

请帮忙。谢谢!

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CompanyList.aspx.cs" Inherits="_CompanyList" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link id="Link1" rel="stylesheet" runat="server" media="screen" href="/Apps/ERP/ERP_Styles.css" />
</head>
<body>
    <form id="form1" runat="server">
        <table  style="width:100%" border="0">
            <tr>
                <td style="width:20%"></td>
                <td style="width:60%"> 
                    <p class="PageTitle">Company List</p> 

                </td>
                <td style="width:20%">               
                    <asp:Button ID="myBtn" runat="server" Text="Click me" 
                    OnClientClick="window.parent.location.href='/Apps/ERP/ASPX/UploadContact/UploadContact.aspx'; return false;" /> 
                </td>
           </tr>
        </table>       

        <%--  ////////////////////////////////////// Start Data Row  ///////////////////////////////////////// --%>               
        <table width="595px">
            <asp:DataList BackColor="#ffffff" id="DataList1" DataSourceID="dsCompanyList"  runat="server" Width="100%">     
                <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="DataList2" runat="server" Width="100%">
                           <ItemTemplate>
                               <tr>
                                  <td><%#Eval("FirstName")%></td>
                                  <td><%#Eval("LastName")%></td>                        
                               </tr>
                           </ItemTemplate>
                       </asp:DataList>
                   </asp:Panel>
                </ItemTemplate>
            </asp:DataList>
        </table>

        <asp:SqlDataSource ID="dsCompanyList" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:ConnApps %>"

                SelectCommand="SELECT ROW_NUMBER() OVER (ORDER By MatchFlag ASC) AS ROW
                                , Company FROM (
                                    SELECT DISTINCT(Company) AS Company, MatchFlag
                                    --,InsertFlag, MatchFlag
                                    FROM dbo.Import_CompanyContact icc 
                                    WHERE RefNum = @RefNum
                                    AND MatchFlag = 2
                                ) a  "              
                > 

                <SelectParameters> 
                    <asp:QueryStringParameter  Name="RefNum" QueryStringField="RefNum" DefaultValue="0" Type="Int16" /> 
                </SelectParameters>

            </asp:SqlDataSource>  

    </form>
</body>
</html>

.cs 文件背后的代码

using System;
using System.Collections.Generic;
//using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _CompanyList : 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

            if (childViewPanel.Visible)
            {
                DataList childList = (DataList)childViewPanel.FindControl("DataList2");
                if (childList != null)
                {
                    int keyValue = (int)DataList1.DataKeys[itemIndex];

                    //bind the list using DataList1 data key value
                    childList.DataSource = "SELECT FirstName, LastName FROM dbo.Import_CompanyContact WHERE Company = 'Applied Micro'"; //get data using keyValue
                    childList.DataBind();
                }  
            }
        }
    }

}
4

1 回答 1

0

需要做一些事情才能为您工作,但首先 -考虑使用存储过程或至少参数化查询。

您正在尝试访问.DataKeys“父级” DataList,但您从未将其包含在您的定义中。为了准确地做到这一点,您将需要更改您的源查询以包含一些键值(而不仅仅是ROW_NUMBER()(不是键)和Company):

<asp:DataList BackColor="#ffffff" 
  id="DataList1" 
  DataSourceID="dsCompanyList"  
  runat="server" 
  Width="100%" 
  DataKeyField="YourKeyField">

然后,你需要改变你的代码隐藏来设置.DataSource你的“孩子” DataList......当我使用你的查询时,请考虑使用动态 SQL 以外的东西:

    protected void LinkButton1_Command(object sender, CommandEventArgs e)
    {
        int itemIndex = Convert.ToInt32(e.CommandArgument);
        Panel childViewPanel = (Panel)DataList1.Items[itemIndex].FindControl("pnlChildView");
        if (childViewPanel != null)
        {
            if (childViewPanel.Visible)
            {
                DataList childList = (DataList)childViewPanel.FindControl("DataList2");
                if (childList != null)
                {
                    int keyValue = (int)DataList1.DataKeys[itemIndex];
                    using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["YourConnectionString"].ConnectionString))
                    {
                        con.Open();
                        using (SqlCommand cmd = new SqlCommand("SELECT FirstName, LastName FROM dbo.Import_CompanyContact WHERE Company = " + keyValue, con))
                        {
                            cmd.CommandType = CommandType.Text;
                            using (SqlDataReader dr = cmd.ExecuteReader())
                            {
                                childList.DataSource = dr;
                                childList.DataBind();
                            }
                        }
                    }
                }
            }
        }
    }
于 2013-09-27T23:16:35.460 回答