2009-07-21 20 views
8

我在写一个asp.net用户控件。它有一个属性FurtherReadingPage和两个绑定到它的控件:ObjectDataSource和一个Repeater。在Repeater中,我想显示一个超链接,其href属性设置为类似于FurtherReadingPage + "?id=" + Eval("Id")。我不知道如何在页面的标记内做到这一点。我可以单独使用<% Eval("Id") %><% Response.Write(FurtherReadingPage + "?id=") %>,但我不知道如何混合它们。如何使用asp.net页面上<% ... %>标签内的c#代码?

回答

3

你可以做到这样的 -

<asp:Hyperlink runat="Server" ID="hlLink" NavigateUrl='<%# FurtherReadingPage + "?Id=" + DataBinder.Eval(Container.DataItem, "Id") %>' /> 
1

试试这个(例如,作为链接):<a href='<%=FurtherReadingPage %>?id=<%# Eval("Id") %>'>My link</a>

+0

此混合物<(%)=和<%#,这将创建在问题大多数情况。除非调用DataBind(),否则<%=在Repeater内不起作用,<%#将不起作用。 – Keith 2009-07-22 08:58:59

3

试试这个:

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

你有几个不同的标签:

<%执行里面的代码:

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

<%=写出里面的代码:

<%=id %> <!-- note no ; --> 

<!-- this is shorthand for: --> 
<% Response.Write(id); %> 

当一个页面上呈现这两个分手的正常流动,例如,如果你在正常使用它们Asp.net <head runat="server">你会得到问题。

<%#数据绑定:

<%# Eval("id") %> 

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

<!-- 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(); 
    } 
%> 

针对您的特殊情况下,您:

  • 使用中继器和<%# Eval(...) %>repeater.DataBind();

  • 使用foreach循环(<% foreach(... %>)与<%= ... %>
相关问题