如何使用字符串列表将多个变量传递给转发器

本文关键字:变量 转发器 何使用 字符串 列表 | 更新日期: 2023-09-27 18:30:31

我一直在寻找一种将我的中继器与多个变量(如创建日期、文件路径和父文件夹)一起使用的方法,我能够将数据绑定到字符串列表以便能够填充文件路径,我有一个将文本放入标签中的方法,但它只重复列表中所有标签字段的第一项。有没有办法将每个字段的多个变量传递给中继器?

。.aspx

<asp:Repeater id="repLinks" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
   <ItemTemplate>
         <tr>
             <td>
      <asp:HyperLink runat="server" NavigateUrl='<%# Container.DataItem.ToString() %>' Text="<%# Container.DataItem.ToString().Split('''').Last() %>"  />    
                 </td>
                  <td> 
      <asp:Label ID="CRD" runat="server" Text="Label"></asp:Label>           
                  </td>  
             <td>
      <asp:Label ID="USR" runat="server" Text="Label"></asp:Label>        
             </td>
                 </tr>      
            </ItemTemplate>
           </asp:Repeater> 

.aspx.cs

 public List<string> CD = new List<string>();
    public List<string> lLinks = new List<string>();
    public List<string> folder = new List<string>();
    public string root = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //Welcomes User
            string Uname = Environment.UserName;
            UserName.Font.Size = 17;
            UserName.Text = "Welcome: " + Uname;
            Get_Vars();
            //Define your list contents here 
            repLinks.DataSource = lLinks;
            repLinks.DataBind();
        }
    }
    protected void Get_Vars()
    {
        //gives path and constructs lists for directory paths and file links
        root = "C:''Users''James''Documents''Visual Studio 2015''WebSites";
        //adds files to list
        foreach (var path in Directory.GetDirectories(@root))
        {
            foreach (var path2 in Directory.GetFiles(path))
            {
                lLinks.Add(path2);
                CD.Add(File.GetCreationTime(path2).ToString());
                //CD.Add(File.GetCreationTime(path2).Date.ToString("yyyy-mm-dd"));
                string[] temp = Path.GetDirectoryName(path2).Split(new string[] { "''" }, StringSplitOptions.None);
                folder.Add(temp[temp.Length-1]);
            }

        }
    }

    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        foreach (RepeaterItem item in repLinks.Items)
        {
            if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
            {
                for(int k=0;k<CD.Count;k++) {
                    var lbl = (Label)item.FindControl("CRD");
                    //int k = 0;
                    lbl.Text = CD[k];    
                }
            }
            if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
            {
                var lbl = (Label)item.FindControl("USR");
                int k = 0;
                lbl.Text = folder[k];
                k++;
            }
        }
    }

如何使用字符串列表将多个变量传递给转发器

删除ItemDataBound事件处理程序。您不需要它只是将一些文本绑定到标签。最好以声明方式执行此操作。

应将数据封装在模型中。

public class CD
{
    public string Name {get; set;}
    public string Link {get; set;
    public string Folder {get; set;}
    //additional properties about the CD here
}

然后创建一个列表,并将其绑定到您的中继器。

List<CD> cds = new List<CD>();
//populate your list with data. Here's manually populating with one
CD cd = new CD
{
    Name = "Dark Side of the Moon",
    Link = "http://www.google.com/pinkfloyd",
    Folder = "C:''Users''etc"
};
cds.Add(cd);
repLinks.DataSource = cds;
repLinks.DataBind();

最后,强键入中继器,然后可以访问属性。

<asp:Repeater id="repLinks" runat="server" ItemType="CD" OnItemDataBound="Repeater1_ItemDataBound">
    <!-- omit some stuff -->
    <%#: Item.Name %>  

不要将相关值存储在不相关的变量中。 创建一个对象,该对象表示转发器中的任何给定元素。 像这样:

public class MyObject
{
    public string CD { get; set; }
    public string Link { get; set; }
    public string Folder { get; set; }
}

使用该对象的列表作为数据源:

public List<MyObject> MyObjects = new List<MyObject>();

并用您的数据填充该列表:

foreach (var path2 in Directory.GetFiles(path))
{
    string[] temp = Path.GetDirectoryName(path2).Split(new string[] { "''" }, StringSplitOptions.None);
    MyObjects.Add(new MyObject {
        CD = File.GetCreationTime(path2).ToString(),
        Link = path2,
        Folder = temp[temp.Length-1]
    });
}

绑定到列表:

repLinks.DataSource = MyObjects;
repLinks.DataBind();

在中继器中,您可以绑定到对象的属性:

<%# ((MyObject)Container.DataItem).CD %>

<%# ((MyObject)Container.DataItem).Link %>

等。

只要有数据元素的逻辑分组,就对它们进行分组。 它变得比试图保持一堆单独的变量同步要容易得多。