问题描述
我有一个链接按钮:
<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
处理程序,如下所示:
In code behind I add an onclick
handler in Page_Load
like this:
LinkButtonPrint.Attributes["onclick"] = "StartLoadTracking(this, '" + GetLocalResourceObject("Loading") + "')";
呈现的 HTML 是这样的:
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>
如果我点击这个按钮它工作正常(它会响应一个 PFD 文件,所以没有 HTML 被发送回浏览器),但是如果我点击页面上的另一个按钮(这使得一个完整的回发)LinkButtonPrint
将没有内部内容,它将像这样呈现:
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>
如果我从 Page_Load
中删除 LinkButtonPrint.Attributes["onclick"] = ...
行,一切正常(除了我的 js 函数没有被调用,但是是正常的).
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).
我在这里遗漏了什么?
编辑
这是
的副本asp.net 链接按钮图像在回发后不可见
但那个也没有解决:(
EDIT
This is duplicate of
asp.net Link button image not visible after postback
but that one is not solved either :(
推荐答案
我找到了解决方案:
我不得不将 runat="server"
添加到 <img>
标签内的 <asp:LinkButton>
:
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" />
这篇关于asp:LinkButton 的内部图像和文本在回发后消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!