添加的代码中的Click事件不会触发LinkBut​​ton控件. [英] Click event of a in-code added LinkButton control doesn't fire.

查看:58
本文介绍了添加的代码中的Click事件不会触发LinkBut​​ton控件.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我创建了一个用户控件,该用户控件使用一个函数从数据库中加载了一个链接按钮列表,该链接按钮从后面的代码中添加到了该用户控件中.奇怪的是,当我单击linkbutton时,不会触发Click事件.

有什么建议吗?

Hi,

I''ve created a usercontrol that loads a list of linkbuttons from the database using a function, the linkbuttons get added to the usercontrol from the code-behind. The weird thing is that when i click the linkbutton the Click event doesn''t get fired.

Any suggestions?

public partial class RapportRowControl : System.Web.UI.UserControl
{
    // Gets fired from the Page_Load of the owning WebPage. 
    public void LoadByRapportRow(tblRapportRow row) 
    {
        PlaceHolder1.Controls.Clear();

        HanDataContext dc = new HanDataContext();

        var list = from a in dc.tblCategories
                   where a.lng_prf_ID == row.tblRapport.lng_prf_ID
                   select a;

        Table t = new Table();

        foreach (var item in list)
        {
            LinkButton CategorieLink = new LinkButton();
            CategorieLink.EnableViewState = true;
            CategorieLink.ID = item.lng_ctg_ID.ToString();
            CategorieLink.CommandName = item.lng_ctg_ID.ToString();
            CategorieLink.Text = item.str_ctg_Naam;
            CategorieLink.Click += new EventHandler(CategorieLink_Click);

            TableRow tr = new TableRow();
            TableCell td = new TableCell();

            td.Controls.Add(CategorieLink);
            tr.Cells.Add(td);
            t.Rows.Add(tr);
        }

        PlaceHolder1.Controls.Add(t);
    }

    // ===== DOESN''T FIRE AT ALL =====
    private void CategorieLink_Click(object sender, EventArgs e)
    {
        ActiveRow.lng_ctg_ID = Convert.ToInt32(((LinkButton)sender).CommandName);
    }
    // ===== DOESN''T FIRE AT ALL =====
}

推荐答案



确保在回发链接按钮时重新创建它,否则不会触发按钮事件.
这意味着使用您的控件的页面应为f.x.调用LoadByRapportRow(或其他函数)以重新创建链接按钮.您可以通过Page_Load事件来执行此操作.

问候
Joachim
Hi,

Make sure that the linkbutton is re-created when it is postback otherwise the button event will not get fired.
That means the page using your control should f.x. call LoadByRapportRow (or some other function) to re-create the link button. This you can do from the the Page_Load event.

Regards
Joachim



尝试从Page_Init而不是Page_Load ...调用用户控件的LoadByRapportRow方法.
如果这不能帮助您从asp.net网页上发布一些代码,那么我们将尝试找出问题出在哪里!
Hi,
Try to call your usercontrol LoadByRapportRow method from Page_Init instead of Page_Load...
If this dont help post some code from your asp.net webpage and then we''ll try to figure out where is the problem!


这篇关于添加的代码中的Click事件不会触发LinkBut​​ton控件.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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