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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1158424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 09:36:42  来源:igfitidea点击:

How to use c# code inside <% ... %> tags on asp.net page?

c#asp.net

提问by agnieszka

I'm writing an asp.net user control. It has a property, FurtherReadingPage, and two controls bound to it: ObjectDataSource and a Repeater. Inside the Repeater I would like to display a hyperlink with an href property set to something like 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.

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

采纳答案by Kirtan

You can do it like this -

你可以这样做 -

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

回答by Sergio

Try this (example as link): <a href='<%=FurtherReadingPage %>?id=<%# Eval("Id") %>'>My link</a>

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

回答by Saul Dolgin

Try this:

尝试这个:

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

回答by Keith

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 呈现为集合(而不是您可以使用的文字控件)的控件指定绑定<%=,例如:

<!-- 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:

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

  • Use a repeater and <%# Eval(...) %>with repeater.DataBind();
  • 使用一个中继器,并<%# Eval(...) %>repeater.DataBind();

or

或者

  • Use a foreach loop (<% foreach(... %>) with <%= ... %>
  • 使用 foreach 循环 ( <% foreach(... %>)<%= ... %>