Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
155 views
in Technique[技术] by (71.8m points)

c# - how to set contol property in asp.net

Very simple and stupid question.

i have a page class

public partial class ProtectWayItem : System.Web.UI.UserControl
    {
        public int Count { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
                 Count = 10;
        }
    }

and how i can set div id equal Count.

I mean something like:

<div id='<%# Count %>' > </div>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have to use = instead #

<div id='<%= Count %>' >

And if you want to call with the # sign then you need to call a DataBind() method..

protected void Page_PreRenderComplete(object sender, EventArgs e)
{
    DataBind();
}

here is what each expression means

  • The <%= expressions are evaluated at render time
  • The <%# expressions are evaluated at DataBind() time and are not evaluated at all if DataBind() is not called.
  • <%# expressions can be used as properties in server-side controls. <%= expressions cannot.

For a better understanding, please check out this link: The difference between <%= and <%# in ASP.NET


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...