[英]How to use c# code inside <% … %> tags on asp.net page?
I'm writing an asp.net user control. 我正在编写一个asp.net用户控件。 It has a property, FurtherReadingPage, and two controls bound to it: ObjectDataSource and a Repeater.
它有一个属性,FurtherReadingPage,以及绑定到它的两个控件:ObjectDataSource和Repeater。 Inside the Repeater I would like to display a hyperlink with an href property set to something like
FurtherReadingPage + "?id=" + Eval("Id")
. 在Repeater中我想显示一个超链接,其href属性设置为
FurtherReadingPage + "?id=" + Eval("Id")
。 I don't know how to do it inside the page's markup. 我不知道如何在页面标记内执行此操作。 I can use
<% Eval("Id") %>
or <% Response.Write(FurtherReadingPage + "?id=") %>
alone but I don't know how to mix them. 我可以单独使用
<% Eval("Id") %>
或<% Response.Write(FurtherReadingPage + "?id=") %>
但我不知道如何混合它们。
You have a couple of different tags: 你有几个不同的标签:
<%
executes the code inside: <%
执行里面的代码:
<% int id = int.Parse(Request["id"]); %>
<%=
writes out the code inside: <%=
写出里面的代码:
<%=id %> <!-- note no ; -->
<!-- this is shorthand for: -->
<% Response.Write(id); %>
Both of these break up the normal flow when rendered on a page, for instance if you use them in a normal Asp.net <head runat="server">
you'll get problems. 当在页面上呈现时,这两个都会破坏正常流程,例如,如果您在普通的Asp.net
<head runat="server">
使用它们,您将遇到问题。
<%#
databinding: <%#
数据绑定:
<%# Eval("id") %>
This allows you to specify the bindings for controls that Asp.net WebForms render as a collection (rather than the literal controls that you can use <%=
with), for instance: 这允许您指定Asp.net WebForms呈现为集合的控件的绑定(而不是可以使用
<%=
with的文字控件),例如:
<!-- this could be inside a repeater or another control -->
<asp:Hyperlink runat="server" ID="demo"
NavigateUrl="page.aspx?id=<%# Eval("id") %>" />
<% //without this bind the <%# will be ignored
void Page_Load( object sender, EventArgs e ) {
demo.DataBind();
//or
repeaterWithManyLinks.DataBind();
}
%>
For your specific case you either: 对于您的具体情况,您可以:
<%# Eval(...) %>
with repeater.DataBind();
<%# Eval(...) %>
with repeater.DataBind();
or 要么
<% foreach(... %>
) with <%= ... %>
<% foreach(... %>
)和<%= ... %>
试试这个:
<%#String.Format("{0}?id={1}",FurtherReadingPage, Id)%>
你可以这样做 -
<asp:Hyperlink runat="Server" ID="hlLink" NavigateUrl='<%# FurtherReadingPage + "?Id=" + DataBinder.Eval(Container.DataItem, "Id") %>' />
试试这个(例如链接): <a href='<%=FurtherReadingPage %>?id=<%# Eval("Id") %>'>My link</a>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.