控制Web表单类中的访问事件处理程序

时间:2010-03-11 22:07:36

标签: c# asp.net web-controls

我有一个Custom WebControl。在这个控件里面我添加了一个按钮,我希望它能够访问包含控件的WebForm上的EventHandler。处理程序处理来自WebForm的控件,因此它必须在那里。我可能设法将按钮从控件中取出,但为了组织起见,将它保留在控件上会更好。

public class LanguageSelection : WebControl
{
    private List<Language> _Languages;

    private CSSImageButton btnOk = new CSSImageButton();
    private CSSImageButton btnClose = new CSSImageButton();

    public List<Language> Languages
    {
        set { _Languages = value; }
        get { if (_Languages != null) return _Languages; else; _Languages = LanguageManager.Select(); return _Languages;  }
    }

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);

        Control parent;
        Control container;

        btnClose.CssClass = "sprReprove";
        btnClose.DivClass = "float-right";
        btnClose.OnClientClick = "$('#languagesOptions').hide('slow')";

        btnOk.CssClass = "sprApprove";
        btnOk.DivClass = "float-right";
        btnOk.Click += new ImageClickEventHandler("btnSave_Click"); // this method here is on the webform where i included the control

        // Get a reference to the ScriptManager object for the page
        // if one exists.
        ScriptManager sm = ScriptManager.GetCurrent(Page);

        if (sm == null || !sm.EnablePartialRendering)
        {
            // If partial rendering is not enabled, set the parent
            // and container as a basic control.
            container = new Control();
            parent = container;
        }
        else
        {
            // If partial rendering is enabled, set the parent as
            // a new UpdatePanel object and the container to the 
            // content template of the UpdatePanel object.
            UpdatePanel up = new UpdatePanel();
            container = up.ContentTemplateContainer;
            parent = up;
        }

        container.Controls.Add(new LiteralControl("<div id=\"languagesOptions\" class=\"divSelectLanguages\">"));
        container.Controls.Add(new LiteralControl(" <strong>Salvar conteúdo nestes idiomas?</strong>"));
        container.Controls.Add(new LiteralControl("<table class=\"tblSelectLanguages\">"));


        int i = 0;
        foreach (Language l in Languages)
        {
            CheckBox cb = new CheckBox();
            cb.Enabled = false;
            if(i % 2 == 0) container.Controls.Add(new LiteralControl("</tr><tr>"));
            container.Controls.Add(new LiteralControl("<td>"));
            container.Controls.Add(cb);
            container.Controls.Add(new LiteralControl(l.FullName));
            container.Controls.Add(new LiteralControl("</td>"));
            i++;


        }
        container.Controls.Add(new LiteralControl("</tr>"));
        container.Controls.Add(new LiteralControl("</table>"));


        container.Controls.Add(btnOk);
        container.Controls.Add(btnClose);

        container.Controls.Add(new LiteralControl("</div>"));

        Controls.Add(parent);

   }}

1 个答案:

答案 0 :(得分:1)

不建议在包含的webform上使用事件处理按钮。理想情况下,您的控制应该是完全独立的。相反,你可以做的是让你的按钮点击事件在你的控件内处理,然后引发另一个事件,这可以由WebForm处理。

// This event will be handled by the webform
public event EventHandler OkButtonClicked;

protected void btnOk_Click(object sender, EventArgs e)
{
    // Raise the okButtonClicked event
    if (OkButtonClicked != null)
       OkButtonClicked(sender, e);
}

// The btnOk button will be wired to our new event handler
btnOk.Click += new ImageClickEventHandler(btnOk_Click);

在您的网络表单上,您可以使用以下内容:

<app:LanguageSelection ID="LanguageSelection1" OnOkButtonClicked="btnSave_Click" runat="server"/>

当在webcontrol中单击按钮时,它将由webcontrol中的btnOk_Click方法处理。这将引发OkButtonClicked事件,该事件将由包含控件的WebForm中的btnSave_Click方法处理。