我有一个链接按钮:
<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.
但是一个没有解决:
解决方法
这个问题现在已经被Microsoft知道了,他们已经在
Microsoft Connect issue 718810: LinkButton controls collection lost on postback发布了官方声明和解决方法.
微软的声明是:
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>