asp:LinkBut​​ton 的内部图像和文本在回发后消失

发布于 2024-10-29 22:31:06 字数 1383 浏览 1 评论 0原文

我有一个链接按钮:

<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 发送回浏览器),但是如果我单击页面上的另一个按钮(这会产生完整的回发),LinkBut​​tonPrint不会有内部内容,它将像这样渲染:

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

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

我在这里缺少什么?

编辑
这是
的重复 asp.net 链接按钮图像在回发后不可见
但这个问题也没有解决:(

I have a link button:

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

In code behind I add an onclick handler in Page_Load like this:

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

The rendered HTML is like this:

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

If I click this button it is working OK (it will respond with a PFD file so no HTML is sent back to the browser), but if I click another button on the page (which makes a full postback) the LinkButtonPrint will not have the inner content, it will be rendered like this:

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

If I remove the LinkButtonPrint.Attributes["onclick"] = ... line from Page_Load everything works fine (except my js function is not called, but that is normal).

What am I missing here?

EDIT
This is duplicate of
asp.net Link button image not visible after postback
but that one is not solved either :(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

深陷 2024-11-05 22:31:06

Microsoft 现已知道此问题,并且他们已在 Microsoft Connect 问题 718810:LinkBut​​ton 控制回发时丢失的集合

微软的声明是:

感谢您的反馈。这似乎很奇怪,因为 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> 

This problem is now known to Microsoft, and they have published an official statement and workaround on the Microsoft Connect issue 718810: LinkButton controls collection lost on postback.

Microsoft statement is:

Thank you for the feedback. This seems like an oddity with the fact that the LinkButton control allows either direct text content via the Text property or in markup, or sub-controls via the Controls collection or markup. The workaround is to use a placeholder as the root content for the control and place the desired mixed content in there, e.g.

<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> 
未蓝澄海的烟 2024-11-05 22:31:06

我找到了解决方案:
我必须将 runat="server" 添加到 内的 标记:

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

I found the solution:
I had to add runat="server" to the <img> tag inside the <asp:LinkButton>:

<img src="img/print-icon.png" alt="" runat="server" />
氛圍 2024-11-05 22:31:06

只需添加一条注释到上面的解决方案中即可。

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

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

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

Just a note to add to the solution above.

I found this specifically occuring when I had enabled=false (to show disabled style) my linkbutton and then caused a postback from the page via another control.

The .Text component of the linkbutton got wiped out after postback.

BUT by putting runat="server" on my .Text / html tags (em, span), they now are retained after postback.

故事与诗 2024-11-05 22:31:06

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

你可以尝试的两件事:

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

或者

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

the problem is that the Attributes["onclick"] = override the handler of the onclick operation.

2 thing you could try :

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

or

LinkButtonPrint.Attributes["onclick"] = "StartLoadTracking(this, '" + GetLocalResourceObject("Loading") + "');" + LinkButtonPrint.Attributes["onclick"];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文