访问动态创建的控件时出现问题 [英] Problem in accessing dynamically created controls

查看:88
本文介绍了访问动态创建的控件时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在动态创建一些链接按钮,并尝试在后面的代码中的其他一些功能中访问它们,但遇到了一些问题我在做什么是在页面加载事件中

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

因为有多个链接按钮,所以我为它们分配了唯一的ID,但是我无法在后面的代码中的其他函数中访问它们.我要添加所有linkbutton的字母"是一件事div有人可以告诉我如何访问它们

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

推荐答案

如果要在CodeBehind中访问它们,唯一的选择是使用 FindControl :

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

这篇关于访问动态创建的控件时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆