【问题标题】:Inner image and text of asp:LinkButton disappears after postbackasp:LinkBut​​ton内部图片和文字回发后消失
【发布时间】:2011-07-29 04:49:36
【问题描述】:

我有一个链接按钮:

<asp:LinkButton ID="LinkButtonPrint" runat="server" OnClick="OnPrint_Click">
     <img src="img/print-icon.png" alt="" />
     <asp:Literal runat="server" Text="<%$ Resources:PrintPage %>" />
</asp:LinkButton>

在后面的代码中,我在Page_Load 中添加了一个onclick 处理程序,如下所示:

LinkButtonPrint.Attributes["onclick"] = "StartLoadTracking(this, '" + GetLocalResourceObject("Loading") + "')";

渲染出来的 HTML 是这样的:

<a href="javascript:__doPostBack('ctl00$LinkButtonPrint','')" 
id="ctl00_LinkButtonPrint" onclick="StartLoadTracking(this, 'Loading...');">
    <img alt="" src="img/print-icon.png">Print page
</a>

如果我单击此按钮,它可以正常工作(它会以 PFD 文件响应,因此不会将 HTML 发送回浏览器),但如果我单击页面上的 另一个 按钮(这会使一个完整的回发)LinkButtonPrint 将没有内部内容,它将呈现如下:

<a href="javascript:__doPostBack('ctl00$LinkButtonPrint','')"  
id="ctl00_LinkButtonPrint" onclick="StartLoadTracking(this, 'Loading...');"></a>

如果我从Page_Load 中删除LinkButtonPrint.Attributes["onclick"] = ... 行,一切正常(除了我的js 函数没有被调用,但这是正常的)。

我在这里错过了什么?

编辑
这是
asp.net Link button image not visible after postback
的副本 但是那个也没有解决:(

【问题讨论】:

  • 这听起来像是一个 ViewState 问题。您可以尝试将EnableViewState="false" 添加到您的 LinkBut​​ton 并报告结果吗?

标签: asp.net webforms


【解决方案1】:

微软现在已经知道这个问题,他们已经在Microsoft Connect issue 718810: LinkButton controls collection lost on postback上发布了官方声明和解决方法。

微软声明是:

感谢您的反馈。 LinkBut​​ton 控件允许通过 Text 属性或标记中的直接文本内容,或通过 Controls 集合或标记的子控件,这似乎有点奇怪。解决方法是使用占位符作为控件的根内容,并在其中放置所需的混合内容,例如

<asp:LinkButton runat="server">
    <asp:PlaceHolder runat="server">
        This is some text.</br>
        <asp:Label runat="server">This is a control</asp:Label>
    </asp:PlaceHolder>
</asp:LinkButton> 

【讨论】:

  • 这对我不起作用,即使添加了 enableviewstate = "false"
  • 为什么我仍然支持旧代码?这正是问题所在,解决了问题!
  • 谢谢!这解决了我的问题。请注意,该问题仅在链接状态为“Enabled=False”时发生。在回发时,它不记得控件的内部集合。但是,如果内容区域为空,请记住属性中的文本标签。我花了很多时间研究这个并提出了其他可行的解决方案,但不喜欢它们。占位符解决方案似乎是最干净的解决方法。有了占位符,问题就解决了,我可以继续前进了。
  • 2020,这仍然存在!!!!我的天啊,你这个,你怎么找到的?我现在找不到任何有关此的信息...
【解决方案2】:

只是添加到上述解决方案的注释。

当我有 enabled=false(以显示禁用的样式)我的链接按钮然后通过另一个控件导致从页面回发时,我发现这种情况特别发生。

链接按钮的 .Text 组件在回发后被清除。

但是通过将 runat="server" 放在我的 .Text / html 标签(em、span)上,它们现在在回发后保留。

【讨论】:

    【解决方案3】:

    我找到了解决方案:
    我必须将runat="server" 添加到&lt;asp:LinkButton&gt; 内的&lt;img&gt; 标记中:

    <img src="img/print-icon.png" alt="" runat="server" />
    

    【讨论】:

    • +1 分享答案。尽管如此,这听起来更像是一种解决方法,而不是一种解决方案,因为这种行为的原因尚不清楚。
    • @Heinzi,你说得对,我发现这也是一种解决方法,但没有时间进行更多研究,我已经为此浪费了几个小时
    • 很好的解决方法,但如果您在LinkButton 中有许多html标签,它可能会污染标记(例如:&lt;img&gt;&lt;b&gt;&lt;i&gt; 等等)..另一种选择是我在another answer 中报告的微软官方解决方法,其中还包括微软关于此问题的官方声明。
    【解决方案4】:

    问题在于 Attributes["onclick"] = 覆盖了 onclick 操作的处理程序。

    你可以尝试两件事:

    LinkButtonPrint.Attributes.Add("onclick", "StartLoadTracking(this, '" + GetLocalResourceObject("Loading") + "');");
    

    LinkButtonPrint.Attributes["onclick"] = "StartLoadTracking(this, '" + GetLocalResourceObject("Loading") + "');" + LinkButtonPrint.Attributes["onclick"];
    

    【讨论】:

    • 我为其放置了处理程序的按钮工作正常。我很幸运,上面没有其他 onclick 处理程序。无论如何感谢您的提示,我会记住这一点!
    猜你喜欢
    • 2010-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多