asp:LinkButton 的内部图像和文本在回发后消失
我有一个链接按钮:
<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 链接按钮图像在回发后不可见
但这个问题也没有解决:(
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Microsoft 现已知道此问题,并且他们已在 Microsoft Connect 问题 718810: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:
我找到了解决方案:
我必须将
runat="server"
添加到
内的标记:
I found the solution:
I had to add
runat="server"
to the<img>
tag inside the<asp:LinkButton>
:只需添加一条注释到上面的解决方案中即可。
我发现当我的链接按钮 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.
问题是 Attributes["onclick"] = 覆盖了 onclick 操作的处理程序。
你可以尝试的两件事:
或者
the problem is that the Attributes["onclick"] = override the handler of the onclick operation.
2 thing you could try :
or