简体   繁体   中英

How to reference code behind variable in ASPX?

I'm not sure why when referencing a code behind variable in an asp.net control, I get the text of the reference:

<%=this.Person.Contact.Emails[0].EmailAddress%>

This outputs the literal reference text:

<asp:TextBox ID="EmailAddress" runat="server" Text="<%=this.Person.Contact.Emails[0].EmailAddress%>"></asp:TextBox>

This renders the variable value:

<input id="testfield" type="text" value="<%=this.Person.Contact.Emails[0].EmailAddress%>" />

Any ideas how I can get the variable value in the asp.net control?

You could say:

EmailAddress.Text = this.Person.Contact.Emails[0].EmailAddress

in your code behind

I prefer the solution in Code Behind in hunter's solution but another option would be to use data binding with #:

<asp:TextBox ID="EmailAddress" runat="server" Text="<%# this.Person.Contact.Emails[0].EmailAddress%>" />

But then you have to bind the server control in code-behind:

EmailAdress.DataBind();

The = sign is like a call to Response.Write() at this place and just outputs whatever follows as text.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM