ASP.NET页面有多个定制控件实现IPostBackEventHandler [英] ASP.NET Page with multiple custom controls implementing IPostBackEventHandler

查看:188
本文介绍了ASP.NET页面有多个定制控件实现IPostBackEventHandler的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.Net网页,其中包含实现IPostBackEventHandler接口多重控制。在code的简化版本如下所示:

 公共部分类WebForm1的:System.Web.UI.Page
{
    保护无效的Page_Load(对象发件人,EventArgs的发送)
    {
        //自定义控件
        MyTextBox mytxt =新MyTextBox();
        mytxt.ID =mytxt;
        mytxt.TextChange + = mytxt_TextChange;
        this.Form.Controls.Add(mytxt);        //自定义控件
        用作MyButton mybtn =新用作MyButton();
        mybtn.ID =mybtn;
        mybtn.Click + = mybtn_Click;
        this.Form.Controls.Add(mybtn);
    }    无效mybtn_Click(对象发件人,EventArgs的发送)
    {
        的Response.Write(mybtn_Click);
    }    无效mytxt_TextChange(对象发件人,EventArgs的发送)
    {
        的Response.Write(mytxt_TextChange);
    }
}[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand,NAME =将FullTrust)]
公共类MyTextBox:控制,IPostBackEventHandler
{
    公共事件的EventHandler TextChange;    受保护的虚拟无效OnTextChange(EventArgs的发送)
    {
        如果(TextChange!= NULL)
        {
            TextChange(这一点,E);
        }
    }    公共无效RaisePostBackEvent(字符串eventArgument)
    {
        OnTextChange(新的EventArgs());
    }    保护覆盖无效渲染(HtmlTextWriter的输出)
    {
        output.Write(<输入类型=文本的id ='+身份证+'名='+身份证+'的onchange ='__ doPostBack(安培;#39;+ ID +&放大器;#39 ;,&安培;#39;&安培;#39;)/>中);
    }
}[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand,NAME =将FullTrust)]
公共MyButton类:控制,IPostBackEventHandler
{
    公共事件的EventHandler点击;    受保护的虚拟无效的OnClick(EventArgs的发送)
    {
        如果(点击!= NULL)
        {
            点击(这一点,E);
        }
    }    公共无效RaisePostBackEvent(字符串eventArgument)
    {
        的OnClick(新的EventArgs());
    }    保护覆盖无效渲染(HtmlTextWriter的输出)
    {
        output.Write(<输入类型='按钮'ID ='+身份证+'名='+身份证+'值=点击我的onclick =__ doPostBack(安培;#39;+ ID +&放大器;#39;,&安培;#39;&安培;#39;)/>中);
    }
}

有2自定义控件 - MyTextBox&安培;用作MyButton实现IPostBackEventHandler接口。 MyTextBox有TextChange事件,并用作MyButton有Click事件。

如果我把页面(MyTextBox或用作MyButton)上只有一个控制 - 事件的火属性。但随着网页上同时控制MyTextBox TextChange事件越来越即使点击了用作MyButton后被解雇。用作MyButton Click事件时MyTextBox在页面上不会被解雇。

我已经在这里张贴尝试过多次的事情。在此先感谢您的帮助。


解决方案

问题的原因是,你需要调用__doPostBack方法与控制的UniqueID。这是至关重要的。此外,基本的服务器控件呈现的ClientID作为元素的ID和的UniqueID为元素名称。所以,如果你更新你的渲染方法,下面的一切都将工作:

文本框:

  output.Write(<输入类型=文本的id ='+ this.ClientID +'名='+ this.UniqueID +'的onchange =' __doPostBack(安培;#39;+ this.UniqueID +&放大器;#39;,&安培;#39;&安培;#39;)/>中);

按钮:

  output.Write(<输入类型='按钮'ID ='+ this.ClientID +'名='+ this.UniqueID +的价值='点击我的onclick =__ doPostBack(安培;#39;+ this.UniqueID +&放大器;#39;,&安培;#39;&安培;#39;)/>中);

修改

使用的UniqueID似乎真的无助于解决问题。我不知道这个问题的原因,但我试图用基础WebControl类覆盖您的自定义文本框,这有助于解决问题。所以,你可以尝试去用这个实现。

<$p$p><$c$c>[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, NAME =将FullTrust)]
公共类MyTextBox:WebControl的,IPostBackEventHandler
{
    公共事件的EventHandler TextChange;    受保护的虚拟无效OnTextChange(EventArgs的发送)
    {
        如果(TextChange!= NULL)
        {
            TextChange(这一点,E);
        }    }    公共无效RaisePostBackEvent(字符串eventArgument)
    {
        OnTextChange(新的EventArgs());
    }    保护覆盖的HtmlTextWriterTag TagKey
    {
        得到
        {
            返回HtmlTextWriterTag.Input;
        }
    }    保护覆盖无效的AddAttributesToRender(HtmlTextWriter的作家)
    {
        base.AddAttributesToRender(作家);
        writer.AddAttribute(HtmlTextWriterAttribute.Onchange,Page.ClientScript.GetPostBackEventReference(这一点,的String.Empty));
        writer.AddAttribute(安其preSS,如果(WebForm_TextBoxKeyHandler(事件)== FALSE)返回false;);
        writer.AddAttribute(HtmlTextWriterAttribute.Type,文本);
    }
}

I am having an ASP.Net Page which contains multiple controls which implement the IPostBackEventHandler interface. The SIMPLIFIED VERSION of the code is given below:

public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Custom Control
        MyTextBox mytxt = new MyTextBox();
        mytxt.ID = "mytxt";
        mytxt.TextChange += mytxt_TextChange;
        this.Form.Controls.Add(mytxt);

        //Custom Control
        MyButton mybtn = new MyButton();
        mybtn.ID = "mybtn";
        mybtn.Click += mybtn_Click;
        this.Form.Controls.Add(mybtn);
    }

    void mybtn_Click(object sender, EventArgs e)
    {
        Response.Write("mybtn_Click");
    }

    void mytxt_TextChange(object sender, EventArgs e)
    {
        Response.Write("mytxt_TextChange");
    }
}

[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
public class MyTextBox : Control, IPostBackEventHandler
{
    public event EventHandler TextChange;

    protected virtual void OnTextChange(EventArgs e)
    {
        if (TextChange != null)
        {
            TextChange(this, e);
        }
    }

    public void RaisePostBackEvent(string eventArgument)
    {
        OnTextChange(new EventArgs());
    }

    protected override void Render(HtmlTextWriter output)
    {
        output.Write("<input type='text' id='" + ID + "' name='" + ID + "' onchange='__doPostBack(&#39;" + ID + "&#39;,&#39;&#39;)' />");
    }
}

[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
public class MyButton : Control, IPostBackEventHandler
{
    public event EventHandler Click;

    protected virtual void OnClick(EventArgs e)
    {
        if (Click != null)
        {
            Click(this, e);
        }
    }

    public void RaisePostBackEvent(string eventArgument)
    {
        OnClick(new EventArgs());
    }

    protected override void Render(HtmlTextWriter output)
    {
        output.Write("<input type='button' id='" + ID + "' name='" + ID + "' value='Click Me' onclick='__doPostBack(&#39;" + ID + "&#39;,&#39;&#39;)' />");
    }
}

There are 2 custom controls - MyTextBox & MyButton implementing the IPostBackEventHandler interface. MyTextBox has the TextChange event and MyButton has the Click event.

If I keep only one control on the page (MyTextBox or MyButton) - the events fire property. But with both controls on the page the MyTextBox TextChange event is getting fired even after clicking the MyButton. The MyButton Click event does not get fired when the MyTextBox is on the page.

I have tried multiple things before posting it here. Thanks in advance for the help.

解决方案

The cause of the problem is that you need to call __doPostBack method with UniqueID of the control. This is vital. Also, basically server controls render ClientID as ID of the element and UniqueID as element name. So if you update your render methods to the following everything will work:

TextBox:

output.Write("<input type='text' id='" + this.ClientID + "' name='" + this.UniqueID + "' onchange='__doPostBack(&#39;" + this.UniqueID + "&#39;,&#39;&#39;)' />");

Button:

output.Write("<input type='button' id='" + this.ClientID + "' name='" + this.UniqueID + "' value='Click Me' onclick='__doPostBack(&#39;" + this.UniqueID + "&#39;,&#39;&#39;)' />");

EDIT:

Using UniqueID seems really didn't help to resolve your problem. I don't know the cause of the problem, but I tried to overwrite your custom text box using base WebControl class and this helps to resolve problem. So, you can try to go with this implementation.

[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
public class MyTextBox : WebControl, IPostBackEventHandler
{
    public event EventHandler TextChange;

    protected virtual void OnTextChange(EventArgs e)
    {
        if (TextChange != null)
        {
            TextChange(this, e);
        }

    }

    public void RaisePostBackEvent(string eventArgument)
    {
        OnTextChange(new EventArgs());
    }

    protected override HtmlTextWriterTag TagKey
    {
        get
        {
            return HtmlTextWriterTag.Input;
        }
    }

    protected override void AddAttributesToRender(HtmlTextWriter writer)
    {
        base.AddAttributesToRender(writer);
        writer.AddAttribute(HtmlTextWriterAttribute.Onchange, Page.ClientScript.GetPostBackEventReference(this, string.Empty));
        writer.AddAttribute("onkeypress", "if (WebForm_TextBoxKeyHandler(event) == false) return false;");
        writer.AddAttribute(HtmlTextWriterAttribute.Type, "text");
    }
}

这篇关于ASP.NET页面有多个定制控件实现IPostBackEventHandler的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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