Mac
Mac

Reputation: 7523

Problem in accessing dynamically created controls

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

Upvotes: 0

Views: 1529

Answers (3)

Giles Smith
Giles Smith

Reputation: 1962

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

Upvotes: 0

djdd87
djdd87

Reputation: 68456

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");

Upvotes: 4

David_001
David_001

Reputation: 5802

You can use the FindControl method to get the control.

LinkButton aControl = (LinkButton)Page.FindControl("a");

Upvotes: 0

Related Questions