LinkBut​​ton单击事件未在动态usercontrol中触发

时间:2013-01-24 17:04:15

标签: c# asp.net user-controls webforms

在我的主页面视图中,我有一个linkbutton,一个占位符,我注册了我的usercontrol:

<%@ Register TagPrefix="vf" TagName="Headline" Src="~/Controls/Headline.ascx" %>

<asp:LinkButton ID="lbAddHeadline" runat="server" OnClick="lbAddHeadline_OnClick">+ Headline</asp:LinkButton>

<asp:PlaceHolder ID="phAddTemplateControlsArea" runat="server"></asp:PlaceHolder>

如果出现这个链接按钮,我会根据我添加到页面的控件数给控件一个ID,我还设置了一个命令arg并链接了一个按钮点击事件,然后我将它们全部加载到一个面板。 当我单击usercontrol中的按钮时,我希望它在我的主页面上触发一个方法,RemoveItem_OnClick()

protected void lbAddHeadline_OnClick(object sender, EventArgs e)
{
    Controls_Headline ctrl = (Controls_Headline)LoadControl("~/Controls/Headline.ascx");
    ctrl.ID = "MyCtrl_" + CMSSession.Current.AddedTemplateControls.Count;
    ctrl.Remove.CommandArgument = CMSSession.Current.AddedTemplateControls.Count.ToString();
    ctrl.RemoveEvent += new EventHandler(RemoveItem_OnClick);

    CMSSession.Current.AddedTemplateControls.Add((Control)ctrl);

    LoadControlsToPanel();
}

private void LoadControlsToPanel()
{
    PlaceHolder ph = accAddTemplates.FindControl("phAddTemplateControlsArea") as PlaceHolder;

    foreach (var ctrl in CMSSession.Current.AddedTemplateControls)
    {
        ph.Controls.Add(ctrl);
    }
}

public void RemoveItem_OnClick(object sender, EventArgs e)
{
    LinkButton lb = sender as LinkButton;
}

当控件添加到主页面板上时,我已重写OnInit方法以保留usercontrol中的数据:

protected override void OnInit(EventArgs e)
{
    PlaceHolder ph = accAddTemplates.FindControl("phAddTemplateControlsArea") as PlaceHolder;

    int counter = 0;

    foreach (UserControl ctrl in CMSSession.Current.AddedTemplateControls)
    {
        ctrl.ID = "MyCtrl_" + counter;
        ITemplateControl ictrl = ctrl as ITemplateControl;
        ictrl.Remove.CommandArgument = counter.ToString();
        ictrl.RemoveEvent += new EventHandler(RemoveItem_OnClick);
        counter++;
        ph.Controls.Add(ctrl);
    }

    base.OnInit(e);
}

正如您所看到的,用户控件实现了它和其他用户控件将使用的通用接口,因此我可以链接一些常用按钮

public interface ITemplateControl
{
    LinkButton Remove { get; set; }
    TextBox Label { get; set; }
    event EventHandler RemoveEvent;
}

这是usercontrol的html:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Headline.ascx.cs" Inherits="Controls_Headline" %>

<div class="headline">

    Headline Text:

    <asp:TextBox ID="txtLabel" runat="server"></asp:TextBox>

    <asp:LinkButton ID="lbRemove" runat="server" OnClick="lbRemove_OnClick">X</asp:LinkButton>

</div>

这是usercontrol的代码隐藏:

public partial class Controls_Headline : UserControl, ITemplateControl
{    
    protected void Page_Load(object sender, EventArgs e)
    {
        lbRemove.Click += new EventHandler(lbRemove_OnClick);
    }

    public TextBox Label
    {
        get { return txtLabel; }
        set { txtLabel = value; }
    }

    public LinkButton Remove
    {
        get { return lbRemove; }
        set { lbRemove = value; }
    }

    public event EventHandler RemoveEvent;

    protected void lbRemove_OnClick(object sender, EventArgs e)
    {
        if (RemoveEvent != null)
        {
            RemoveEvent(sender, e);
        }
    }
}

现在,当我单击位于usercontrol中的lbRemove链接按钮时,我希望它调用它自己的方法lbRemove_OnClick,然后调用我在OnInit方法中绑定的委托事件。

每次单击lbRemove链接按钮时会发生什么事情它会调用主页面的OnInit方法,就是这样。

我错过了什么?

1 个答案:

答案 0 :(得分:1)

想出来。

直接将UC中的click事件绑定到页面中的click事件。我太复杂了。

    protected void lbAddHeadline_OnClick(object sender, EventArgs e)
    {
        Controls_Headline ctrl = (Controls_Headline)LoadControl("~/Controls/Headline.ascx");
        ctrl.ID = "MyCtrl_" + CMSSession.Current.AddedTemplateControls.Count;
        ctrl.Remove.CommandArgument = CMSSession.Current.AddedTemplateControls.Count.ToString();
        ctrl.Remove.Click += new EventHandler(RemoveItem_OnClick);

        CMSSession.Current.AddedTemplateControls.Add((Control)ctrl);

        LoadControlsToPanel();
    }

    protected override void OnInit(EventArgs e)
    {
        PlaceHolder ph = accAddTemplates.FindControl("phAddTemplateControlsArea") as PlaceHolder;

        int counter = 0;

        foreach (UserControl ctrl in CMSSession.Current.AddedTemplateControls)
        {
            ctrl.ID = "MyCtrl_" + counter;
            ITemplateControl ictrl = ctrl as ITemplateControl;
            ictrl.Remove.CommandArgument = counter.ToString();
            ictrl.Remove.Click += new EventHandler(RemoveItem_OnClick);
            counter++;
            ph.Controls.Add(ctrl);
        }

        base.OnInit(e);
    }