Monday, December 13, 2010

How to Set MasterPage Variables from Content Page

Need to send information from your content page to your master page?
Despite what you may have read on a couple blogs, IT CAN BE DONE!
Here's how.

Suppose I have a variable in my code behind my master page that I want to set based on what content page is opened. In my particular case, I'm going to use an example of a menu that I will only show while browsing certain pages in my website.

The first thing I need to do is create a variable within my master page:
Private _showMenu As Boolean = False

I can use this variable later within my master page's Me.Load function to set the menu control to visible or not. Now, I need a way to interact with the Master page for this variable, so I will make a simple Property for the page:
Public Property ShowMenu() As Boolean
Get
Return _showMenu
End Get
Set(ByVal value As Boolean)
_showMenu = value
End Set
End Property


We are now done with the MasterPage, which for my example I have named "MyAwesomeMasterPage". Moving on to content pages, for each page I want to communicate with my master page, all I have to do is add one easy line in the initialization of the page. I do it so early, just in case I need to access the variable early in my master page's loading.
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
CType(Master, MyAwesomeMasterPage).ShowMenu = True
End Sub


By casting my content page's masterpage as the MyAwesomeMasterPage page that I created, it is now aware of the Public Property that I created for it.

Friday, December 3, 2010

How to handle GoDaddy's preinstalled URL Rewrite when running code locally

This blog assumes that you are either taking advantage of the Microsoft URL Rewrite module on a GoDaddy hosting server or another hosting server that has URL Rewrite preinstalled. If you have questions about how to use this module on GoDaddy, then click here to read one of my previous blogs that goes more into detail on how to implement the module.

If you are using the GoDaddy pre-installed URL Rewrite module on your site, and testing changes locally, you've probably run into a couple problems when you attempt to go to a url utilizing the rewrite method.

For example, if my site has user profiles that can be accessed with a unique ID the user chooses, a profile URL might look like this:
http://www.digitalplaydoh.com/RobFine

Behind the scenes, my URL Rewrite method looks something like this:
<rewrite>
<rules>
<rule name="RewriteCheck" stopProcessing="true">
<match url="^(.+)$"/>
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
<add input="{URL}" pattern=".*.axd$" negate="true"/>
</conditions>
<action type="Rewrite" url="MyProfile.aspx?key={R:0}"/>
</rule>
</rules>
</rewrite>


On GoDaddy's servers, this will execute perfectly however, while I'm debugging the code on my local machine, the URL Rewrite module is not available, meaning I constantly get a Server Error when I try to go to http://www.digitalplaydoh.com/RobFine yet when I go to http://www.digitalplaydoh.com/MyProfile.aspx?key=RobFine everything works fine.

There is a solution!

I have created a function within my Profile class called ProfileURL that I use to return the url for a particular profile. I originally created it to handle cases where users had not chosen a unique ID, but now it's going to help us develop locally as well.

The original function looked something like this:


If there is a unique ID for the user, it will send the unique id back as the url.
If there is no unique ID established, it will send back the profile page MyProfile.aspx and feed in my Profile ID.

It is important that anytime I feed my Profile URL to a link, I call this function.

Now, I need to add a bit to the function to support when I'm debugging locally. In the case of the "RobFine" profile earlier, this profileURL will still return just "~/RobFine" as it stands now. What I can do is add in a simple IF THEN statement to check if we are running locally:
Public ReadOnly Property ProfileURL() As String
Get
If HttpContext.Current.Request.IsLocal Then
Return "~/MyProfile.aspx?id=" & _id
Else
If Not _uniqueID = "" Then
Return "~/" & _uniqueID
Else
Return "~/MyProfile.aspx?id=" & _id
End If
End If
End Get
End Property


HttpContext.Current.Request.IsLocal will return a boolean function if my code is running locally.
If it is running locally, I want to throw back a URL that doesn't implement the URL Rewrite.
If it is not, then I'll have the function act as it normally would on the server.

Wednesday, December 1, 2010

Handling Line Breaks with FQL in ASP.NET

Facebook's FQL query language lets you do some really cool things as far as pulling data from Facebook profiles, events, groups and more. I've been using it lately to create fun websites for various events I've been hosting, but I ran into a small issue while pulling the wall posts from a Facebook Group.

I was able to pull everything from the wall, but it all came as one long string with no html line breaks <br/>. Seems simple enough to fix, right? I ususally append a .Replace(vbCrLf, "<br/>") to my string and I'm golden, but that wasn't working this time.

I tried everything I could think of: vbCrLf, Environment.NewLine, \n, \r\n, \r, Chr(13), PHP_EOL, ControlChars.NewLine, ControlChars.CrLf - nothing seemed to be working until I finally tried out vbLf

The vbLf constant represents \n just as vbCr represents \r and vbCrLf represents \r\n. Facebook uses the \n notation to store line breaks.

So if I were to store my FQL (Facebook Query Language) query results in a string called myFqlResult, I could print it out to the screen in a decent viewing manner with the string:

lblMyExample.Text = myFqlResult.Replace(vbLf,"<br/>")

Facebook also throw in all sorts of xml structures as well, so I usually filter it all out with something like this:
lblMyExample.Text = myFqlResult.Replace("<message>", "<p>")_
.Replace("</message>", "</p>")_
.Replace("<stream_post>", "<div>")_
.Replace("</stream_post>", "</div>")_
.Replace(vbLf, "<br/>")_
.Replace("<fql_query_response xmlns=""http://api.facebook.com/1.0/"" _
xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" list=""true""><br/>", "")_
.Replace("</fql_query_response><br/>", "")_
.Replace("<?xml version=""1.0"" encoding=""UTF-8""?><br/>", "")_
.Replace("<div><br/>", "<div>")

CSS Consistency Issue between IE7 and IE8

While I was creating a short menu out of a bullet list, I observed an inconsistency in padding that surface between websites being viewing Internet Explorer 6 or 7 and Internet Exporer 8.

With IE 8, when I set the margins of my <ul> tags and my <li> tags to 0px, the bulleted list is still indented.


However, with IE 7 and 6, when I set the margins to 0px, the list lines up with the text above it.


From my research and testing, it would appear there is no way to correct for this.
It especially becomes a problem when developing in IE 8 and correcting the margin-left to -30px.
While IE 8 will show the text of the list left aligned with the rest of the text on the page, previous versions of the browser will bleed the list far left beyond the placement of the control's container.


To avoid this problem, the best practice going forward for the next couple years is either to account space for the list to be indented or avoid using lists.