简体   繁体   English

在Web窗体类中控制访问事件处理程序

[英]Control acessing event handler in web form class

I have a Custom WebControl. 我有一个自定义WebControl。 Inside this control I add a button and I want it to access an EventHandler that is on the WebForm where the control is included. 在此控件内,我添加了一个按钮,并希望它访问WebForm上包含控件的EventHandler。 The handler handles with controls from the WebForm, so it has to be there. 处理程序使用WebForm中的控件进行处理,因此它必须存在。 I could probably manage to take the button out of the control, but it would be better to keep it on the control, for organization sake. 我可能可以设法将按钮从控件中删除,但是出于组织的考虑,最好将其保留在控件中。

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

   }}

Having your button handled by an event on the containing webform is not advisable. 建议不要让包含的Web表单中的事件处理您的按钮。 Ideally, your control should be completely self-contained. 理想情况下,您的控件应完全独立。 Instead, what you can do is have your button click event handled inside your control and then raise another event, which can be handled by the 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);

On your webform, you can have something like this: 在您的Web表单上,您可以具有以下内容:

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

When the button is clicked inside the webcontrol, it would be handled by the btnOk_Click method inside the webcontrol. 当在Web控件内单击按钮时,将由Web控件内的btnOk_Click方法处理。 This would then raise the OkButtonClicked event which would be handled by the btnSave_Click method in WebForm containing the control. 然后,这将引发OkButtonClicked事件,该事件将由包含控件的WebForm中的btnSave_Click方法处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 访问单击事件以进行自定义控件 - Acessing the click event for a customized control 是否可以关闭事件处理程序内部的窗体,其中该事件处理程序位于类中? - Is it possible to close a form inside an event handler, wherein the event handler is in a class? 使用Web API访问查询字符串和表单参数 - Acessing querystring and form parameters with Web API 如何制作一个为表单中的每个控件添加事件处理程序的组件? - How to make a component that adds an event handler to each control in a Form? 通过VS接口进行表单控制的多个事件处理程序 - Multiple Event Handler for Form Control Via VS interface 在 1 个控件中添加 2 个事件处理程序 - Adding 2 Event Handler in 1 Control 如何从类中访问事件处理程序以在Form中使用 - How to access the event handler from the class to use in the Form 遍历窗体上的所有控件,如果控件上存在事件,则向控件添加处理程序 - Loop through all controls on form and if event exists on control add handler to control 如何使用表单中的事件操作在用户控件中创建新的事件处理程序 - How to make new event handler in user control with event action from form 在C#中从父窗体的子窗体中为控件添加事件处理程序 - Adding an event handler for a control in child form from parent form in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM