Thursday, August 6th, 2009
jQuery, a lightweight (only 19kb in size) JavaScript library has become my new best friend. It's like mushrooms to Mario. Obviously I'm not alone since Microsoft is now distributing jQuery with Visual Studio (including jQuery intellisense). If you are using the new MVC framework from Microsoft then you will no doubt become familiar with the intricate workings of jQuery.
jQuery is not all that difficult to learn. The biggest thing you have to understand is all the different "selectors" that are available to you. Using selectors developers can query, in a CSS like way, for HTML elements, and then apply "commands" to them.
For example, the below JavaScript uses jQuery to find a <div> element within a page that has a CSS id of "rightSide", and shows it and "leftSide" and hides it.
JAVASCRIPT:
-
$('div#rightSide').show();
-
$('div#leftSide').hide();
As another example, the JavaScript below uses jQuery to find a specific <table> on the page with an id of "datagrid1", then retrieves every other <tr> row within the datagrid, and sets those <tr> elements to have a CSS class of "even" - which could be used to alternate the background color of each row:
JAVASCRIPT:
-
$('#datagrid1 tr:nth-child(even)').addClass('even');
This next example gets all links <a> in a specific <div> and attaches an onclick event to them:
JAVASCRIPT:
-
$('#navBtns a').bind('click', function(event){
-
event.preventDefault(); //stop the link from going to href
-
//do something
-
});
Being able to traverse the DOM and locate HTML elements to attach events, behaviors, animations and CSS is priceless. But what about ASP.NET controls like the RadioButtonList, GridView, ListView, Repeater, and many others that we as developers like to bind to? How do we traverse them when they all get their ids auto-generated? That's what we will look at now! And with jQuery it's not that hard!
(more...)
Posted in .NET, JavaScript | 5 Comments »
Thursday, February 5th, 2009
In this article you will learn how to send email using ASP.NET. Yes, there are plenty of other articles that cover sending email via .NET, but after spending a day doing research, I was amazed at how many articles failed to provide either a correct solution or a real world example. I found that many articles suggest you create your HTML email by using a string with the HTML markup in it. That's crazy and not at all a real world solution, at least not for most situations. In this article we will look at a more realistic solution. One in which we use a regular HTML file as our template for the email. The template file will be a standard HTML file with the exception of some placeholders that we will use to populate our content and images right before we send the email. Think mail-merge in Microsoft Word. Finally, we will also learn how to send the email in such a way that if the email recipient's mail-client can't render HTML they will get an alternate plain text version.
Let's start by looking at the code in its entirety; the people that just want to grab the code and use it can do so. I will then explain the code.
(more...)
Posted in .NET | 24 Comments »
Wednesday, August 20th, 2008
I love ASP.NET, but one thing I find extremely frustrating is dealing with the automatically generated ID properties that it places on page elements.
For example, let's say you have a div with an id of "contentTop" like below:
HTML:
-
<div id="contentTop" runat="server">...
</div>
Because you have made the control a server-side control by adding the runat="server" attribute it will now be rendered with a different ID when it hits the browser (view the source code and you will see). It will get rendered as something like this:
HTML:
-
<div id="ctl00_contentTop">...
</div>
This is because ASP.NET generates its own IDs to ensure that every element on the page has a unique ID. Developers that work with JavaScript and CSS will immediately see the problem. This causes difficulty when using JavaScript and CSS which rely on those IDs to reference elements, as they can't easily predict what the generated ID will be. When it comes to CSS specificity the ID selector is extremely useful. In addition, anyone that uses JavaScript knows that getElementById (or one of the JavaScript library ways) is the most popular way to target an element. So why the hell does ASP.NET do this to us? Honestly, I can't really tell why, other than the fact that they must not trust us enough to be able to uniquely ID our elements ourselves.
But I don't want to just complain here, I would like to provide some solutions too.
Solution
The solution will be found in the ClientID property of the server-side control. The ClientID property represents the ID that ASP.NET will use for the element on the client.
Using the ScriptManager we place the ClientID in a hidden form field. This can all be done in the code-behind file by using the following code:
C#:
-
ScriptManager.RegisterHiddenField(this, "contentTop", contentTop.ClientID);
Be sure to have included the ScriptManager to your page, like so:
ASP:
-
<asp:ScriptManager ID="ScriptManager1" runat="server">
-
</asp:ScriptManager>
Now we can use JavaScript to retrieve the value from our hidden form fields.
JAVASCRIPT:
-
var id = form1.contentTop.value;
-
var cTop = document.getElementById('id');
That's it, if you know of another way, please let us know.
Happy coding.
Posted in .NET, C#, Website Design | 7 Comments »
Monday, June 16th, 2008
1. Automatic Properties
Automatic properties provide you with a shorthand for defining a new property. No more do we have to create the private property and then use Getters and Setters to access and set that property. Here's the old code:
ASP:
-
private string _Name;
-
-
public string Name
-
{
-
get {return _Name;}
-
set {_Name = value;}
-
}
Now here's the code using automatic properties:
ASP:
-
public string Name {get; set;}
Oh my! How nice is that? The C# compiler creates the Getters and Setters and the private fields for you automagically!
(more...)
Posted in .NET, C# | 3 Comments »
Wednesday, July 11th, 2007
This past week, I have been undergoing a week of rigorous training learning the key differences between .NET 2.0/3.0 and .NET 1.1. For the past two years our company has been working with the 1.1 framework, and is now migrating towards the 2.0/3.0 framework. This week the instructor said something I knew, but was glad to be reminded about. It wasn't even a .NET oriented question, it was an object oriented question that does not apply to one specific language, but to all OOP (object oriented programming) models in general.
What is the difference between a struct and a class? When should you use a struct versus a class? How do you know?
(more...)
Posted in .NET, C#, PHP | 4 Comments »
Monday, February 19th, 2007
If you hang out on enough web development forums, mailing lists, or chatrooms, you'll eventually get asked this question:
What language should I use to program my next project?
As someone who has been on a few of said forums, mailing lists, and chatrooms for a while now, I hate seeing this question. Yes, it comes around a lot, but repetitiveness isn't what bugs me about it. What bothers me is that there's usually very little background information attached to that question, which makes it as open-ended as asking what mode of transportation you should take to get to the store. I can tell you anything from a skateboard to a jet and either vehicle can get you to the store, but to give you the best answer I need to know how far you have to travel, what you plan on buying, and how much money you have, among other things. The same holds true for the programming language question, so let's look at some factors that can help you choose which language to use on your next project.
(more...)
Posted in .NET, C#, PHP, Website Tools | 3 Comments »
Wednesday, February 7th, 2007
Search-This welcomes Louis Simpson to the team!
Often times when you belong to a forum you start stalking certain people. Every time the person you are stalking post and you notice it, you go read what they have to say because you know it's going to be good. Well, Louie was the first person I ever stalked
So when I was putting together the Search-This team I knew that I had to get him on board, it makes it easier to stalk him that way! We're glad to have you on the team Louie.
Enjoy Louie's bio below:
User Name: louissimps
- childhood ambition: marry a rich woman
- fondest memory: going to my grandpa's ranch
- favorite music: a little something from every genre
- retreat: the ocean
- proudest moment: the path I have lead so far
- biggest challenge: not going to college
- alarm clock: 6:50(snooze until 7:45)
- perfect day: coffee, surfing, lunch with my wife, afternoon nap, head to my favorite bar
- first job: busboy at a banquet hall
- indulgence: Belgian Ale
- favorite movie: too many to name, Super Troopers, Napolean Dynamite, Platoon
- inspiration: accomplishment
Posted in .NET, C#, Flash | 5 Comments »