Programming Journal C#, Java, SQL and to a lesser extent HTML, CSS, XML, and regex. I made this so other programmers could benefit from my experience.

Saturday, December 29, 2007

Web User Control shortcut

I just discovered a Web User Control shortcut for using your controls in apsx pages.
As per the reference link's example, add your implementation of this example:


<add tagPrefix="scottgu" src="~/Controls/Footer.ascx" tagName="footer"/>
<add tagPrefix="ControlVendor" assembly="ControlVendorAssembly"/>
All in the Web.config file in the elements.

Then you can reference it in aspx as

Reference: Link

Thursday, December 20, 2007

Microsoft Visual Web, CSS, and Images link bug

Here's a clue to avoid a bug with Microsoft Visual Web, CSS, and Images. I used the CSS style tool to add an image to an element, but the image would not show. The cause was a faulty link to the image.

Here is an example. I edit my StyleSheet.css in the css folder. I add an image from the brother folder named images. The link will say images/mypic.gif, but it is actually ../images/mypic.gif. Problem solved.

Friday, December 14, 2007

C# string is null or empty

Here's another shortcut method to make sure the string is not null and not empty. What I find interesting is that using myString.length>0 is supposedly more efficient than myString != string.empty.

if (!String.IsNullOrEmpty(myString))
{
myString = "myString != null && myString != string.Empty";
}

C# string extract tag method

This extracts a tagged string. For example, string findTagValue = extractTag ("Foo", "", ""); This would fetch Foo.

private string extractTag(string original, string beginTag, string endTag)
{
string retVal = string.Empty;
int begin = original.IndexOf(beginTag);
int end = -1;
if (begin > 0)
{
end = original.IndexOf(endTag, begin);
}
if ((begin > 0))
{
if ((end > begin))
{
retVal = original.Substring((begin+beginTag.Length), (end - begin-endTag.Length));
}
}
return retVal;
}

Thursday, December 13, 2007

Handy C# tricks

Here are some handy C# tricks to increase productivity.
First, the code:

        //

        // Summary:

        //     Gets or sets a value that indicates whether the System.Web.UI.WebControls.MenuItem

        //     object is enabled, allowing the item to display a pop-out image and any child

        //     menu items.

        //

        // Returns:

        //     true if the menu item is enabled; otherwise, false.

        [Browsable(true)]

        [DefaultValue(true)]

        public bool Enabled { get; set; }

  • The [Browsable(true)] allows the properties window to display the property or event.
  • Default value does as you would expect if the variable is not directly set
  • Short cut get and sets! Simple.

Thursday, December 6, 2007

C# ASP.NET using JavaScript

For ASP.NET, use the ClientScriptManager to register client scripts.

For example, in the Page_Load code behind I'll use:


ClientScript.RegisterClientScriptBlock(typeof(string), "MyScriptShow", "<script language=javascript>function showIt() { alert('showed it'); }</script>");

If the javascript was in a js file register it in the code behind with:

ClientScript.RegisterClientScriptBlock(typeof(string), "myScripts", "<script language='javascript' src='scripts/myScripts.js'></script>");

In the ASPX page I'll use;

<asp:Button ID="Button1" runat="server" OnClientClick="showIt()" Text="Button" />
Resource links: link, link

Note: Use the ClientScript.RegisterClientScriptBlock or ClientScript.Register_x methods (since Page.Register_x methods are version 1.1)

Setting the path environmental variable in XP and Vista

Setting the path environmental variable in XP and Vista.

It is often convenient to set the path environmental variable to call commands. For example, the "javac MyClass.java" command would use the javac command located in the path to compile the MyClass.java file to an executable. Here is how to set it up:


For Windows XP:
Start-> Control Panel -> System -> Advance Tab -> Environmental Variables.
Select Path -> Edit.
Append ";myPath" to environment variable, where myPath is the directory that contains your resources..
OK

For Windows Vista:
Start -> Control Panel -> System and Maintenance -> System -> Advanced system settings
click Continue
Select Environmental Variables
Select Path -> Edit.
Append ";myPath" to environment variable, where myPath is the directory that contains your resources..
OK

Using Google Web Toolkit gwt on Windows

Here's a quick start to using Google's Web Toolkit or gwt.

1. Download latest gwt release at link.
2. Unzip gwt_x to target directory. I used C:/Program Files/Google.

Now to create Eclipse projects do the following:
1. Append to the path environmental variable the target directory such as ";C:/Program Files/Google/gwt_x" , where gwt_x is the gwt version installed.

To create an Eclipse project called "MyProject":
1. use the command prompt and navigate (cd) to the Eclipse workspace. Mine was "C:\work\workspace\".
2. Create a folder via "mkdir MyProject".
3. "cd MyProject"
4. Create project via "projectCreator -eclipse MyProject"
5. Create application via "applicationCreator -eclipse MyProject com.mycompany.client.MyApplication"

To open the Eclipse Project:
1. In Eclipse: File->Import->General->Existing Projects into workspace
2. Select the MyProject directory that you originally created.

Other References:
link, link

Tuesday, December 4, 2007

Creating a Web User Control

Creating a Custom Web Control.
Within Visual Studio:
To keep organized, I create a company/website/user folder. Within the folder I create another folder named Controls. For example I'll have this directory structure: FooInc/Controls.

Within that folder, rt. click and Add New Item. Select the WebUserControl option. I'll name this one WebUserControl_DatePanel.ascx.

Switch to design view. Add a Panel (named Panel1 by default). Add a Label (named Label1 by default) within Panel1. Set Panel1's background to cyan for a custom feel.

In the load Event of WebUserControl_DatePanel.ascx, add the following:
Label1.Text = DateTime.Now.ToString();

Now save and close those files.

In your project, create a new Web Form named WebUser.aspx. Switch to design view.

From the Solution Explorer, drag the WebUserControl_DatePanel.ascx onto the page (much like adding a CSS file). Switch to Source View.

You will notice the following header:

<%@ Register Src="FooInc/Controls/WebUserControl_DatePanel.ascx" TagName="WebUserControl_DatePanel"
TagPrefix="uc1" %>
You will also notice the following web user control in the body:

<uc1:WebUserControl_DatePanel ID="WebUserControl_DatePanel1" runat="server" />

There it is. You can add custom functions at the web user control source if you want additional functionality.