开发者

Table of label ASP.net

i want to know if i can create an array of labels in the code Behind. And then, if 开发者_如何学Ci can put label from the .aspx in the label Array with the id and manipulate it with this.

Thanks


With a page that looks something like this:

<body>
<form id="form1" runat="server">
<div id="labelSpace" runat="server">

<asp:Label ID="Label1" runat="server" Text="Label" Visible="false" >Label One</asp:Label><br />
<asp:Label ID="Label2" runat="server" Text="Label">Label Two</asp:Label><br />
<asp:Label ID="Label3" runat="server" Text="Label">Label Three</asp:Label><br />
<asp:Label ID="Label4" runat="server" Text="Label" Visible="false">Label Four</asp:Label></div><br />

<asp:Button ID="Button1" runat="server" Text="PostBack and Change Labels" />

</form>

this will get you an array of all of the labels in the <div id="labelSpace"> and none of the other controls:

  protected void Page_Load(object sender, EventArgs e)
    {

        if (IsPostBack)//This will only fire after button is clicked...
        {
            //This does [just as you ask] provide you with an array of labels
            Label[] labels = this.labelSpace.Controls.OfType<Label>().ToArray<Label>();
            //& Allows you to manipulate a label in the code behind 
            // by addressing it's index value;
            labels[2].Text = "Modified In Code Behind";


            //Something you may find more useful than an array is getting 
            // a list of labels like:
            List<Label> listOfLabels = this.labelSpace.Controls.OfType<Label>().ToList<Label>();


            //with a list you can identify an individual label easily like:
            IEnumerable<Label> invisibleLabels = listOfLabels.Where(l => l.Visible == false);
            //with an 'IEnemerable of invisible labels you can now manipulate those to make them all visible...
            foreach (Label l in invisibleLabels)
            {
                l.Visible = true;
                l.Text = "Made Visible";
            }

            //or, if you just want a single label where the id is "Label2"
            var labelThree = labels.Where(p => p.ID == "Label2").First();
            labelThree.Text = "selected by label id and then edited";
        }
    }

If you want to start at the page level you will need to build a recursive routine to drill into child controls to create a masterControlCollection and then call to ".OfType<T>..."

Once you have gotten this far you can use LINQ to select out a particular label or set of labels based on any property you want.


Yes you can. Labels from the .aspx part will have a corresponding object in the codebehind file. There's nothing special about them, they are just like any other .NET object. For example if you have markup that looks like this

<asp:Label runat="server" id="MyLabl1" Text="Some Text 1" />
<asp:Label runat="server" id="MyLabl2" Text="Some Text 2" />
<asp:Label runat="server" id="MyLabl3" Text="Some Text 3" />
<asp:Label runat="server" id="MyLabl4" Text="Some Text 4" />

The codebehind file will have something like

protected global::System.Web.UI.WebControls.Label MyLabl1;
protected global::System.Web.UI.WebControls.Label MyLabl2;
protected global::System.Web.UI.WebControls.Label MyLabl3;
protected global::System.Web.UI.WebControls.Label MyLabl4;

Depending on how your project was set up this may be in either the .cs file or the .designer.cs file. From there there's nothing stopping you from adding them to an array and working with them from there.

For example

var labels = new Label[] { MyLabl1, MyLabl2, MyLabl3, MyLabl4 };

will make an array called labels with the 4 labels in it and then you can do whatever you'd like to them. Then you can do labels[0] to get to the first one, etc. Is this what you had in mind?


Label[] labels = new Label[10];
labels[0] = new Label();
labels[0].Text = "blablabla";
...

labels[9] = new Label();
labels[9].Text = "blablabla";

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜