0

在我的主页视图中,我有一个链接按钮、一个占位符,并且我注册了我的用户控件:

<%@ 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 并链接按钮单击事件,然后将它们全部加载到面板中。当我单击用户控件中的按钮时,我希望它在我的主页上触发一个方法 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 方法来保存用户控件中的数据:

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

这是用户控件的 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>

这是用户控件的代码隐藏:

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

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

每次我单击 lbRemove 链接按钮时会发生什么,它会调用主页的 OnInit 方法,仅此而已。

我错过了什么?

4

1 回答 1

1

弄清楚了。

直接将UC的点击事件绑定到页面中的点击事件即可。我把它复杂化了。

    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);
    }
于 2013-01-24T18:53:37.837 回答