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

时间:2016-01-14 20:05:12

标签: c# asp.net

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

的.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++;
            }
        }
    }

2 个答案:

答案 0 :(得分:1)

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

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 %>

每当您对数据元素进行逻辑分组时,请对它们进行分组。与试图保持一堆独立变量保持同步相比,它变得更容易维护。

答案 1 :(得分:1)

删除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 %>