Tuesday, June 21, 2011

Give Dad a ring with free calling from Gmail in the U.S.

Today is Father’s Day in many countries around the world, so whether your dad lives across the street, across the country or across the ocean, it’s a good day to let him know you’re thinking about him. Of course, email is a simple, quick way to check in with your old man, but it’s just as easy—and maybe even more meaningful—to call him right from Gmail.

If you’re in the U.S., you can use Gmail to make free domestic calls and calls to Canada. If your dad lives abroad, we have some of the best rates around, like two cents per minute to Australia, Argentina, France and many other parts of the world (unfortunately, those of you outside the U.S. will have to phone Dad the old-fashioned way).

And in case you don’t read this post in time, we've added a small note in the Gmail chat roster, right under the “Call phone” link, to remind you to give him a ring.


Happy Father’s Day from Gmail!

Wednesday, June 15, 2011

ASP.NET <% vs <script runat=server>

ASP.NET <% vs <script runat=server>



I'm writing some ASP.NET pages and just starting to figure it all out.

I'm able to global variables in like this:
<% bool bHeader = true; %>

and then reference that variable in other <% %> blocks

I'm able to put global functions in
<script language="C#" runat="server"> </script>
blocks and call them from <% %> blocks

However, the script blocks can't access variables in the <% %> blocks.  And variables in the <script> blocks seem to reinitialize every time the script is called.

Can anyone explain the difference, nuances and gotchas of the two code block types?

Thanks

Doug Send private email

Saturday, October 28, 2006
 


 




Some questions are just too big to tackle all in one go.

Are you using a copy of Visual Studio?

Do you understand the differences between ASP and ASP.NET?

It might just make sense for some of us to recommend a book or two to get you started but it would help if we knew what tools you had available to build your pages.

Mike Griffiths Send private email

Sunday, October 29, 2006
 


 




Yes, I have Visual Studio 2003 and 2005, but I'm not using their wizards and frameworks (I'm a "get under the hood" kind of person and want to see what's going on).

I guess I'm really trying to understand the scope of the two block types in ASP.NET.

I've done a lot of ASP before, but we didn't have <script> blocks that ran on the server (well, that I knew of), so that is the new bit to me.

I'm _guessing_ (based on the JIT compiler output when there is an error) that the blocks of code are added into a namespace/class that is based on the filename.  Don't have it all down yet though.

Doug Send private email

Sunday, October 29, 2006
 


 




As an experienced developer I would have thought that it would make sense to build an ASP.NET application using VS2005 first and then take a look at how the code is divided between client and server. In truth, a "native" ASP.NET will try and run most "events" on the server. You will have to make a judgement on that and choose how much to implement as client side scripts and how much to leave in server side code blocks. That's wheer your developer skills come in.

The server side code blocks are written in much the same way as (say) a Windows app. The code is compiled and called by events as required.

It works very effectively and ASP.NET applications can be built very quickly with plenty of support coming from the IDE for both server development and client side debugging. It might be time to give up hand carving the scripts and start enjoying an IDE that delivers a lot even if not quite everything.

Mike Griffiths Send private email

Monday, October 30, 2006
 


 




Thank you for the reply, but I don't think you understood my question.  ASP.NET supports TWO server script block types.  One is embedded in <% %> (like classic ASP) and the other is in <script runat=server> blocks.

I'm trying to understand them so I can take advantage of them (and stop getting bit by nuances). 

I agree, new development would be with the IDE and Wizards, but I'm trying to port a classic ASP site to ASP.NET as quickly as possible.

Based on the feedback I'm getting, I'm guessing this just isn't the right place to ask.

Doug Send private email

Monday, October 30, 2006
 


 




OK... what the ASP.NET framework does when an .aspx file is invoked is to compile a new type on the fly, which inherits from either the specified code-behind type or from System.Web.UI.Page directly. Code included in the page in <script runat="server"> blocks is compiled into methods within the class - one per block, I think. Code included in the page in <% %%> blocks is generally compiled straight into the render method, which consists of a big long string concatenation operation interspersed with bits of code.

If you can find the temporary assembly created by the compilation of an .aspx file, you can disassemble it with Reflector or something similar to see how it's made up.

The short answer is that ASP.NET is not comparable to ASP, and you shouldn't be fooled by the presence of the same in-the-page constructs ('classic' ASP has both <% %> and <script runat="server"> too). Microsoft went a long way to ensure that there was some basic level of compatibility and familiarity, but they are still two fundamentally different beasts. You are clear that you understand this, which is good. However - this is certainly my experience - there is no easy way to 'short circuit' the migration from ASP to ASP.NET via code-in-front. Better to do a proper re-write. There's nothing stopping you have code in the code-in-front - Microsoft went through a phase of recommending that as the default in earlier VS2005 betas - but it's generally considered better practice to go with code-behind and partial types for the designer, a la VS2005.

I applaud you wanting to get to grips with ASP.NET from the ground up, but I think the learning curve will be a little daunting if you're mainly used to ASP (and I mean no disrespect to you as a programmer by that - it's just a very different paradigm and like any such switch it will take time). VS2005 will help you here, and by digging in to what it does and how you will see more quicker than by going the bare-bones approach.

Just my £0.02...

.NET Guy

Monday, October 30, 2006
 


 




Thank you .NET Guy.

I've used code behind files before, but I guess I'm having a hard time seeing the advantage to switching.  Is there a book just on the concepts (that's what I'm missing I think, not necessarily the syntax and technology).

For example, I use the <%=someVariable%> all over the place to quickly inject a value into the outgoing HTML stream.  Is there really a better way?

I also do stuff like:
<% if(Request.QueryString("name") != null)
//handle request...
else
{
<%

lots of HTML describing that you need to enter your name, etc etc

<% } %>

Yeah, I'm definitely mixing code and presentation, but it seems like to handle the if statement above, I'd have to put the error HTML into the code anyway--just mixing it in a diffent location now.

I strongly suspect I just don't get it yet.  If I needed viewstate and such, I can understand.  But for a relatively simple site like mine I don't see it.

Teach me oh great one!  (Or point me towards a book/site that describes the Zen of it all and I'll go read)

Thanks

Doug Send private email

Monday, October 30, 2006
 


 




To be honest I can't recommend a good ASP.NET book because I never read one :-) But the Wrox or Apress books are well-regarded, and I assume O'Reilly does an ASP.NET book which would probably be worthwhile.

To be clear, there's nothing wrong with having code in the code-in-front. I often use expressions (<%= someValue %>), particularly when I have to insert values into HTML attributes, and conditional blocks to control specific aspects of the output. However, you really *ought* to use the server controls (<asp:literal>, <asp:panel> etc) because this gives control of the rendering over to the server control which is *supposed* to be smarter than you. Some people are more religious about this than others - I know people who will contort themselves into knots to avoid putting any code at all into the ASPX file, which leads them to have endless <asp:literal> and placeholder controls all over the place. OTOH, using panel or placeholder controls to turn bits of the page on and off is much simpler than the ASP alternative.

You only really start appreciating the use of server controls once you put your logic into code-behind. Then it becomes annoying to have to faff around with blocks in the ASPX when you could just drop in a server control and then do all the heavy lifting in the code-behind. VS2005 removes the annoying need to remember to declare all your control instances in the code-behind by having a separate, auto-generated designer file that takes care of all that for you.

This all goes hand-in-hand with the fundamental OO nature of .NET. Once you start to think about pages (and user controls) as classes and hence objects in their own right, and start to design them as such, then you can see that the ASPX file is basically just a template which the class that is actually the page uses to build up its output.

Play about with stuff. Try different ways of doing the same thing. Eventually it will click.

.NET Guy

Tuesday, October 31, 2006
 


 




If you want to understand the generated code, an easy way is to make sure customErrors is off in your web.Config, then deliberately introduce a syntax error in the code of a <script runat="server"> block.

E.g.
<script runat="server'>
qwertyuiop
</script>

Then view the page in a browser, you'll get an error page with a link to "Show Complete Compilation source".  When you view the generated source, you'll see how variables declared in <% %> blocks are generated.

Joe

Tuesday, October 31, 2006
 


 




Doug,

You need to get Fritz Onion's Essential ASP.NET book to really grasp how ASP.NET works under the hood. Once you read that book, you will appreciate the terrific job MS has done to develop web development framework like ASP.NET


To talk about your particular example, you would do something like this in ASP.NET

Your aspx page will have:

<div id='userData' runat='server'>

</div>

<div id='dataForm' runat='server'>

</div>

In your code behind, you would do

protected Web.UI.Webcontrols.WebControl userData;
protected Web.UI.Webcontrols.WebControl dataForm;
private void Page_Load(//)
{
  dataForm.visible = false;
  userData.visible = false;
  if(Request.QueryString("name") == null)
        dataForm.visible = true;
  else
        userData.visible = true;
}

This way, your aspx pages only have HTML code and you will be able to manipulate them easily from the code behind.

Again, have a look at Essential ASP.NET book and you will gain more insights in to how ASP.NET works.

Good luck,

JD Send private email

Thursday, November 02, 2006