如何在asp.net页面上的标签内使用c#代码?

我正在编写一个asp.net用户控件。 它有一个属性,FurtherReadingPage,以及绑定到它的两个控件:ObjectDataSource和Repeater。 在Repeater中我想显示一个超链接,其href属性设置为FurtherReadingPage + "?id=" + Eval("Id") 。 我不知道如何在页面标记内执行此操作。 我可以单独使用但我不知道如何混合它们。

你可以这样做 –

  

你有几个不同的标签:

<%执行里面的代码:

 <% int id = int.Parse(Request["id"]); %> 

<%=写出里面的代码:

 <%=id %>   <% Response.Write(id); %> 

当在页面上呈现时,这两个都会破坏正常流程,例如,如果您在普通的Asp.net 使用它们,您将遇到问题。

<%#数据绑定:

 <%# Eval("id") %> 

这允许您指定Asp.net WebForms呈现为集合的控件的绑定(而不是可以使用<%= with的文字控件),例如:

  " /> <% //without this bind the <%# will be ignored void Page_Load( object sender, EventArgs e ) { demo.DataBind(); //or repeaterWithManyLinks.DataBind(); } %> 

对于您的具体情况,您可以:

  • 使用转发器和<%# Eval(...) %> with repeater.DataBind();

要么

  • 使用foreach循环( <% foreach(... %> )和<%= ... %>

试试这个:

 <%#String.Format("{0}?id={1}",FurtherReadingPage, Id)%> 

试试这个(例如链接): My link