快捷方式服务器端代码块
本文关键字:代码 服务器端 快捷方式 | 更新日期: 2023-09-27 18:32:44
我正在处理一个 asp.net 页面,我需要向超链接的href添加一个可选参数。参数值位于代码隐藏中。
这是超链接:
<a href='FileView.aspx?fid=<%#Eval("FileOrFolderID") %>&uid=<%=UserID %> <%=(ProfileType.HasValue && ProfileId.HasValue?"&profiletype=" + (int)ProfileType.Value + "&profileid=" + ProfileId.Value:"") %>'
<%#Convert.ToBoolean(Eval("IsFolder")) ? "style='display:none;'" : "style='display:block;'" %>
id="aDownLoad">
<%# Eval("Name").ToString().HE()%></a>
我想补充一下:
<%=(Convert.ToBoolean(Eval("IsFolder")) ? "" : "&id=" + CurrentID) %>
所以它看起来像这样:
<a href='FileView.aspx?fid=<%#Eval("FileOrFolderID") %>&uid=<%=UserID %> <%=(Convert.ToBoolean(Eval("IsFolder")) ? "" : "&id=" + CurrentID) %> <%=(ProfileType.HasValue && ProfileId.HasValue?"&profiletype=" + (int)ProfileType.Value + "&profileid=" + ProfileId.Value:"") %>'
<%#Convert.ToBoolean(Eval("IsFolder")) ? "style='display:none;'" : "style='display:block;'" %>
id="aDownLoad">
<%# Eval("Name").ToString().HE()%></a>
但是给出了这个错误:
An exception of type 'System.InvalidOperationException' occurred in System.Web.dll but was not handled in user code
Additional information: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
请提出解决此问题的建议
仅
当控件绑定到数据源时,才能使用 Eval 和 Bind,而实际情况似乎并非如此。使用服务器控件:
<asp:HyperLink runat="server" ID="link" />
并在代码隐藏上设置它的 NavigationUrl 属性。