Problem accessing dynamically created controls

I create several link buttons dynamically and try to access them in some other functions in the code, but I encounter some problems, what I do 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 several link buttons, so I assigned them a unique identifier, but I don't get how to access them in other functions in the code behind. And one more thing the “alphabet” to which I add all the link buttons is a div can someone tell me how I can access them

0
source share
3 answers

If you want to access them in CodeBehind, the 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");
+4

FindControl .

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

, .

-, .aspx, visual studio ( asp.net, aspx.designer.cs - aspx).

. . , / . , , :

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

, page_load:

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


        }

, :

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

2... .

ViewState Initialize , , page_load, viewstate, . , . , - " viewstate page_load initialize".

.

Hope this helps

0
source

All Articles