简体   繁体   English

c#如何使重写的面板在用户控件中工作

[英]c# How to get an overrided Panel to work inside a User Control

I am making a C# Web Application in .Net 我正在.Net中制作一个C#Web应用程序

I have overridden the Panel control to make a div clickable. 我已覆盖面板控件以使div可单击。 It looks like this: 看起来像这样:

public class ProjectPanel : System.Web.UI.WebControls.Panel, System.Web.UI.IPostBackEventHandler
{
    public ProjectPanel() 
    {
        CssClass = "project";
        this.Click += new EventHandler(ProjectPanel_Click);
    }

    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(System.Web.UI.HtmlTextWriter writer)
    {
        this.Attributes.Add("onclick", "__doPostBack('" + this.ClientID + "', '');");
        base.Render(writer);
    }
    public void ProjectPanel_Click(object sender, EventArgs e)
    {
        Label l = new Label();
        l.Text = " HIYA";
        this.Controls.Add(l);
    }
}

Now, this works fine inside a Page. 现在,在Page中可以正常工作。 However, I also have a Web User Control which looks like: 但是,我也有一个Web用户控件,如下所示:

<div class="team">

<asp:Panel runat="server" ID="TheCanvas" CssClass="projects" />

</div>

Now when I add the overriden Panel to the TheCanvas panel, the click event isnt registered anymore. 现在,当我将替代面板添加到TheCanvas面板时,单击事件不再注册。 The postback happens, but the ProjectPanel_Click doesnt fire. 回发发生,但ProjectPanel_Click不会触发。 Any ideas? 有任何想法吗? I'm adding the panel through code. 我通过代码添加面板。

Your panel should be created with every postback. 您的面板应与每个回发一起创建。 The postback happens but your panel does not exist on the sever side. 回发发生,但服务器侧不存在您的面板。 Try adding panel through code on pageload function. 尝试通过页面加载功能上的代码添加面板。

Instead of ClientID you should use UniqueID because when in another control client ID is prefixed with parend client ID, replace onclick attribute setting with this : 您应该使用UniqueID而不是ClientID,因为在另一个控件中,客户端ID前缀为parend客户端ID时,请使用以下内容替换onclick属性设置:

this.Attributes.Add("onclick", "__doPostBack('" + this.UniqueID + "', '');");

btw. 顺便说一句 you should be aware that if you don't have on page another control that implements IPostBackDataHandler, __doPostback javascript would not be present and you will get javascript error: 您应该意识到,如果页面上没有另一个实现IPostBackDataHandler的控件,则__doPostback javascript将不存在,并且会出现javascript错误:

__doPostBack is not defined __doPostBack未定义

You should implement IPostBackDataHandler and call Page.RegisterRequiresPostBack(this) inside your panel control, see details on this links: 您应该实现IPostBackDataHandler并在面板控件内调用Page.RegisterRequiresPostBack(this),请参见以下链接的详细信息:

http://aspalliance.com/1071_Smart_ListControl_in_ASPNET_1x20.4 http://aspalliance.com/1071_Smart_ListControl_in_ASPNET_1x20.4

http://www.timvasil.com/blog14/post/2007/10/30/Implementing-IPostBackDataHandler-once-and-for-all.aspx http://www.timvasil.com/blog14/post/2007/10/30/Implementing-IPostBackDataHandler-once-and-for-all.aspx

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM