0

I am creating some link buttons dynamically and try to access them in some other functions in code behind but facing some problem what i am doing is in the page load event

for (int i = 65; i <= 90; i++)
{
    LinkButton lbtnCharacters = new LinkButton();
    lbtnCharacters.Text = "<u>" + Char.ConvertFromUtf32(i) + "</u>";
    lbtnCharacters.ID = Char.ConvertFromUtf32(i);
    lbtnCharacters.CommandArgument = Char.ConvertFromUtf32(i);
    lbtnCharacters.CommandName = "AlphaPaging";
    lbtnCharacters.CssClass = "firstCharacter";
    lbtnCharacters.Click += new EventHandler(lbtnAlphabets_Click);
    alphabets.Controls.Add(lbtnCharacters);
}

As there are multiple link buttons so i have assigned unique id to them but i am not getting how to access them in other functions in code behind.and one more thing the "alphabet" to which i am adding all linkbutton is a div can anybody tell me how i can access them

djdd87
  • 67,346
  • 27
  • 156
  • 195
Mac
  • 6,991
  • 8
  • 35
  • 67

3 Answers3

4

If you want to access them in CodeBehind, your only real option is to use FindControl:

LinkButton aButton = (LinkButton)alphabets.FindControl("a");
LinkButton bButton = (LinkButton)alphabets.FindControl("b");
LinkButton cButton = (LinkButton)alphabets.FindControl("c");
djdd87
  • 67,346
  • 27
  • 156
  • 195
  • why do all recommend `OnInit`? i would rather locate it in `CreateChildControls` and call `EnsureChildControls` appropriately in `OnInit` or `OnLoad` since there are 2 trials for attaching post data, and attaching `ViewState` data happens between `OnLoad` and `OnLoadComplete` –  Jul 29 '10 at 12:07
  • on adding it onInit it is causing some styling problem the content of page coming downwards why i don't know – Mac Jul 29 '10 at 12:09
  • @Mac - Ignore that then. Just use FindControl. – djdd87 Jul 29 '10 at 12:20
0

You can use the FindControl method to get the control.

LinkButton aControl = (LinkButton)Page.FindControl("a");
David_001
  • 5,703
  • 4
  • 29
  • 55
  • Starting the search at the `Page` level will be slower than starting it at the container control. – slugster Jul 29 '10 at 12:30
  • @slugster: Yep, definitely will be slower. The speed of this is very unlikely to be a bottleneck though, but you're right, you'd pretty much always specify the container to find the control within, rather than do it from a page level. – David_001 Aug 04 '10 at 18:47
0

I think there are 2 parts to this.

Firstly, when you add controls to the .aspx page, visual studio automatically creates properties for these controls (you can see this in some versions of asp.net by looking at the aspx.designer.cs file - you should see properties relating to each of the controls in the aspx file).

As you are adding the controls dynamically the controls are not added to the class. So you will need to keep track of them manually. Perhaps create a class level field/property and store them in that. In your case probably worth using a Dictionary:

private Dictionary<int, LinkButton> alphabetLinkButtons = new Dictionary<int, LinkButton>();

This should allow you to access any of the link buttons by their id in other code called during page_load:

f

or (int i = 65; i <= 90; i++)
        {
            LinkButton lbtnCharacters = new LinkButton();
            lbtnCharacters.Text = "<u>" + Char.ConvertFromUtf32(i) + "</u>";
            lbtnCharacters.ID = Char.ConvertFromUtf32(i);
            lbtnCharacters.CommandArgument = Char.ConvertFromUtf32(i);
            lbtnCharacters.CommandName = "AlphaPaging";
            lbtnCharacters.CssClass = "firstCharacter";
            lbtnCharacters.Click += new EventHandler(lbtnAlphabets_Click);
            alphabets.Controls.Add(lbtnCharacters);

            // add this
            alphabetLinkButtons.Add(lbtnCharacters);


        }

Now, later in your code you should be able to access the controls like this:

alphabetLinkButtons[65].Text = "Some new text";

Now point 2... these buttons will not exist on postback.

The ViewState for the page is created in the Initialize event of the page lifecycle, so by adding your controls to the page during page_load, they are never added to the viewstate, so you will not have access to them in the post back event. This may not cause you any problems, but good to be aware of it. There are lots of posts out there that cover this - just search "Dynamically adding controls viewstate page_load initialize".

Here is a good discussion of the issue.

Hope this helps

Giles Smith
  • 1,952
  • 15
  • 17