因为领导要求在模块的顶部将分类绑定(我个人认为没什么意义,但领导说的就一定有道理),于是有一个动态绑定按钮问题出来了,代码很简单:
代码
protected void BindBtn()
{
if (CategoryIds.Length > 0)
{
string[] split = CategoryIds.Split(',');
int[] cids = new int[split.Length];
for (int i = 0; i < split.Length; i++)
{
cids[i] = int.Parse(split[i]);
LinkButton btn = new LinkButton();
btn.ID = "btnCategory" + cids[i].ToString();
btn.CommandArgument = cids[i].ToString();
CategoryController ctlCate = new CategoryController();
CategoryInfo infoCate = ctlCate.Get(cids[i], ArticleManagerModuleID);
btn.Text = infoCate != null ? infoCate.Name : "value = " + cids[i].ToString();
btn.Click += new EventHandler(CategoryBtnClicked);
phCategory.Controls.Add(btn);
Label lbl = new Label();
lbl.Width = 10;
lbl.Height = 10;
phCategory.Controls.Add(lbl);
}
}
}
{
if (CategoryIds.Length > 0)
{
string[] split = CategoryIds.Split(',');
int[] cids = new int[split.Length];
for (int i = 0; i < split.Length; i++)
{
cids[i] = int.Parse(split[i]);
LinkButton btn = new LinkButton();
btn.ID = "btnCategory" + cids[i].ToString();
btn.CommandArgument = cids[i].ToString();
CategoryController ctlCate = new CategoryController();
CategoryInfo infoCate = ctlCate.Get(cids[i], ArticleManagerModuleID);
btn.Text = infoCate != null ? infoCate.Name : "value = " + cids[i].ToString();
btn.Click += new EventHandler(CategoryBtnClicked);
phCategory.Controls.Add(btn);
Label lbl = new Label();
lbl.Width = 10;
lbl.Height = 10;
phCategory.Controls.Add(lbl);
}
}
}
却在调用时一直不能触发事件,怎么也没想明白,后来发现我将BindBtn放在了if(!IsPostBack) 中,代码如下:
if(!IsPostBack)
{
//.......
BindBtn();
}
{
//.......
BindBtn();
}
由于动态生成的控件 ,有的生存周期。在初始化里面写的话,回发时就没了,响应不了。放到外面问题解决。