【问题标题】:Can't do anything inside my custom event在我的自定义事件中无法执行任何操作
【发布时间】:2016-08-26 21:46:05
【问题描述】:

我有一个自定义事件,我想用它在我的页面上显示以前隐藏的元素。

ASPX:

<%@ Register Src="~/Controls/EditSync.ascx" TagPrefix="IP" TagName="EditSync" %>

<asp:Content ID="main" ContentPlaceHolderID="body" runat="server">

//Some unrelated controls

    <fieldset id="fsEditPlugin" runat="server" class="inputForm" style="width:450px;" visible="false">
        <IP:EditSync id="ctlEditSync" runat="server" />
    </fieldset>
</asp:Content>

背后的代码:

protected void Page_Load(object sender, EventArgs e)
{
    this.ctlDelivery.EditPlugin += new EventHandler(onEditPlugin);
    if (!Page.IsPostBack)
    {
        //Some more unrelated things
    }
}

public void onEditPlugin(object sender, EventArgs e)
{
    System.Diagnostics.Debug.Write(((ip.Controls.EditPluginEventArgs)e).type);
    fsEditPlugin.Visible = true;
}

显示调试消息。我可以在事件处理程序中放置断点并到达它们,但无论我尝试什么,我都无法从我的事件处理程序操作页面。 fsEditPlugin 不是任何其他隐藏的子项。我尝试过的一些事情:

//I've tried this:
<fieldset id="fsEditPlugin" runat="server" class="inputForm" style="width:450px; display:none;">
//code behind:
fsEditPlugin.Style.Add("display", "inline");

//I've tried this:
<fieldset id="fsEditPlugin" runat="server" class="inputForm" style="width:450px;">
</fieldset>
//Code behind:
fsEditPlugin.Controls.Add(new LiteralControl("TEST"));

似乎没有任何效果。我的 Page_Load 中没有任何会干扰控件的内容。

正在从另一个子 Web 用户控件触发该事件。因此,当我按下另一个子 Web 用户控件中的按钮时,我想让 EditSync 控件变得可见。

【问题讨论】:

  • 我猜这不是你的全部代码,因为你从来没有在这里注册onEditPlugin。另外,IP:EditSync 是什么?
  • 如果你把fsEditPlugin.Visible的默认值设置为true,你能看到吗,还是看不到呢?
  • @PatrickHofman 稍等,我会添加更多代码
  • @StrahBehry 不,这没有任何作用。它从一开始就可见,在事件被捕获后可见。
  • 您要在更新面板中操作的控件吗?

标签: c# asp.net events


【解决方案1】:

好的,所以我终于设法解决了这个问题!

问题是我触发事件的按钮来自更新面板:

<asp:UpdatePanel ID="UpdatePanel4" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <IP:Delivery id="ctlDelivery" runat="server" /> <% /* this is a web user control */ %>
    </ContentTemplate>
</asp:UpdatePanel>

我通过在 ctlDelivery 的 Page_Load 中添加以下行来解决这个问题:

ScriptManager.GetCurrent(this.Page).RegisterPostBackControl(btnEdit); //btnEdit is the button that fires my custom event.

向 EvilDr 和他的 answer to another question 大声喊叫

【讨论】:

    猜你喜欢
    • 2014-10-22
    • 2013-03-31
    • 2020-12-25
    • 2017-09-23
    • 1970-01-01
    • 2018-03-29
    • 2020-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多