开发者

ASP Labels? Why not just =Variable?

In Classic ASP, I loved the fact that I could write out variables, formulas, etc., etc. with a simple:

<span class='<%=myClass%>'><%=myariable%></span>

I could format it, or do anything I Wanted. It was easy. I could even run functions on them, etc. I know, in .Net, I can still do all the functions etc., (And the fact that it's treated like an object is good), but I seem to be doing a lot of this, and it's annoying!!!

<asp:Label ID='pnlMyVariable' runat='server'></asp:Panel>
%% CODE BEHIND %%
pnlMyVariable.Text = "Yeah, write this short sentence...";
pnlMyVarialbe.CssClass = "blah";

And don't get me started with the annoying, yet somewhat useful FindControl method!

Is this really the way it is to be? If you say "YES,开发者_如何学编程 That's just the way it is", then I will accept it. But surely some people out there (If any are old enough to appreciate Classic ASP) who have seen this to be a little annoying. Am I approaching this correctly?


You're using the asp:Label control correctly, yes. However, it's not a requirement that you use this control. You have lots of other options, but the two which will likely appeal to you the most are:

  1. Use the asp:Literal control. This will work much like a Label but doesn't wrap the value in any additional markup. This allows you to control the markup and styling just like you did "in the old days."

  2. Use the classic syntax. If your page class has a public or protected property on it, that property can be referenced in the markup using the same classic ASP syntax. As a matter of convention in .NET and for a number of good reasons, it's best to use properties instead of directly accessing member variables. But that might end up just being a matter of preference for you.


In webforms, you have this to maintain a model that's similar to winforms. These controls also make use of viewstate, making them hold their values between post backs. MVC is a lot closer to classic asp on the html side, but still very object oriented on the back end. I heavily recommend it!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜