Musings<Biefeld>
- curiosities of development, life, the universe and everything -
Archive for September, 2008
Wednesday, September 17th, 2008

I recently needed a textbox that would fire a server event when the textbox’s onblur JavaScript event was fired.  It took a while of googling and asking coworkers questions to figure out how exactly to do it.  Couldn’t seem to find an example of exactly what I needed.

 

I started off by creating a class that inherited from textbox and implemented ICallBackEventHandler.  I soon realized that this was not going to work for what I needed.  The ICallBackEventHandler is best suited for a simple Ajax control that just needs to update its own content.  Problem is that I need a post back to happen in order to update other controls on the page.

 

So instead of implementing the ICallBackEventHandler interface I implemented the IPostBackEventHanlder interface.  This turned out to be a much simpler, cleaner way to do it.

When implementing the IPostBackEventHandler you must Implement the RaisePostBackEvent method:

public class BlurTextbox : TextBox, IPostBackEventHandler
{
    #region Implementation of IPostBackEventHandler

    /// <summary>
    /// When implemented by a class, enables a server control to process an event raised when a form is posted to the server.
    /// </summary>
    /// <param name="eventArgument">A <see cref="T:System.String" /> that represents an optional event argument to be passed to the event handler. </param>
    public void RaisePostBackEvent(string eventArgument)
    {

    }

    #endregion
}

 

This method is what gets called by .net’s JavaScript when your control does a post back.

 

Now what we want to do is create the JavaScript function that will cause the post back will allow the RaisePostBackEvent method to handle the logic for that event.

 

I just created a method on the textbox that returns the script.  You can use the Page.ClientScript.GetPostBackEventReference(Control control, string argument) method but this is not necessary because all the it generates is __doPostBack(‘controlName’,'argument’), either way works.

 

If you want to be able to have multiple instances of the control on the same page you need to write it yourself, this is what I did:

private string GetScript()
{
    return "function OnBlurred(control, arg)\n{\n __doPostBack(control, arg);\n}";
}

There are two parameters needed, the control and the argument, the control string that the asp.net JavaScript call is expecting will need the textbox’s UniqueID for it to work properly. 

Next I created a server side event on the control called Blur:

public delegate void OnBlurDelegate(object sender, EventArgs e);

public event OnBlurDelegate Blur;

private void RaiseOnBlurEvent()
{
    if(Blur != null)
    {
        Blur(this, EventArgs.Empty);
    }
}

Now we can call the RaiseOnBlurEvent() method in the implemented RaisePostBackEvent() method like:

public void RaisePostBackEvent(string eventArgument)
{
    RaiseOnBlurEvent();
}

Lastly I overrode the OnInit event, called the base.  I check if that script is already registered, if it’s not I register it, otherwise move one.  Then registered the OnBlurred script we created, and added the JavaScript onblur event attribute to the textbox.  I pass in the UniqueID and empty quotes because I don’t need any arguments.

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
    if (!Page.ClientScript.IsClientScriptBlockRegistered("OnBlurTextboxEvent"))
        Page.ClientScript.RegisterStartupScript(GetType(), "OnBlurTextboxEvent", GetScript(), true);
    Attributes.Add("onblur", "OnBlurred('" + UniqueID + "','')");
}

Now all that is left to do is use it on some web page and setup the server side handling.

Aspx:

<cc3:BlurTextbox ID="Test" runat="server" OnBlur="Test_OnBlur"></cc3:BlurTextbox>

Server Handling:

protected void Test_OnBlur(object sender, EventArgs e)
{
}

The whole thing is here.

Sunday, September 7th, 2008

I just started working on a new Firefox theme, it is actually just a modification of the existing one.  I went through and de-saturated all of the icons in the default Firefox 3 theme.  I have plans to eventually create my own icons and provide monochromed icons for some of the popular extensions.

 

Here are some screen shots:

 

It was all fairly simple to do, but ran into a bit of an issue after uploading it Mozilla’s official add-on page.  A good resource is Mozilla’s own documentation, http://developer.mozilla.org/En/Creating_a_Skin_for_Firefox.

Basically all I had to do was grab Firefox’s default theme folder, usually located at FireFox\chrome\classic.jar, then you have to extract the items, it is basically just a zip folder, so you can use 7zip or winrar.

 

Then you’ll want to create a new folder somewhere else with the title of your theme, we’ll use BadAssTheme.  Inside that folder you’ll want a chrome folder, copy the browser, communicator, global, help and mozapps folders from the classic theme into your new chrome folder, also copy the icon.png and preview.png files into your chrome folder.  Create a content.rdf and install.rdf, follow the instructions in Mozilla’s documentation for what needs to be in those files.  Go back up to the BadAssTheme folder, create a chrome.manifest file, and put a copy of the icon.png, preview.png, and install.rdf in here as well.  I’ll get to what goes into the manifest next.

 

Once you have gone through and modified all of the images and css you want its time to package everything up.  The way that seems most commonly used is to have an xpi in the chrome folder and then a jar for what is in the root level.  So zip everything up in the chrome folder, rename the file from chrome.zip to chrome.xpi.  Inside of the chrome.manifest you need to specify where the skin files are located inside of the xpi, for example:

 

skin browser BasAssTheme jar:chrome/chrome.xpi!/browser/

skin communicator BasAssTheme jar:chrome/chrome.xpi!/communicator/

skin global BasAssTheme jar:chrome/chrome.xpi!/global/

skin mozapps BasAssTheme jar:chrome/chrome.xpi!/mozapps/

skin help BasAssTheme jar:chrome/chrome.xpi!/help/

 

So now you should only have the xpi container in the chrome folder and a manifest, icon.png, preview.png, install.rdp in the BadAssTheme folder.  Now zip everything in the BadAssTheme folder into a BadAssTheme.zip and rename to BadAssTheme.jar.  You should be good to go now.

 

The issue I ran into with Mozilla’s theme website is that the download works on one of my computers but doesn’t on my other.  The error I am getting is:

 

Firefox could not install the file at

https://addons.mozilla.org/en-US/firefox/downloads/file/37146/monochrome-0.4-fx.jar

because: Invalid file hash (possible download corruption)

-261

 

I am not sure if this is actually a bug in Firefox or with Mozilla’s hosting, because it work on one PC and not the other, they both have the same version of Firefox installed.  Have not been able to find anything helpful through google.  It does install properly if you download it manually and drag and drop it onto Firefox’s add-on manager, so who knows.

 

Source: http://code.google.com/p/monochrometheme

 

Theme: https://addons.mozilla.org/en-US/firefox/addon/8791

Wednesday, September 3rd, 2008

Ran into an issue today when I needed to get a column from a many to many relational table.  It was not in any table that mapped directly to any of my entities, so I was somewhat dumb founded on how to proceed.

 

An example would be if you have a school domain where there are multiple Students associated with multiple Courses.  You would most likely have a Student and Course entity that map to their respective database tables and a Student_Course table that does not map directly to anything.  Depending on the point of view you are working with you would probably have the Student with a collection of Course or vice versa.  For this example let’s say that we are looking at it from the perspective of a student.  As a Student I would want to see my final grade for the Course , for simplicity let’s say that the final grade is stored in the Student_Course table.  The most likely place to want that Grade is on the Course.  When you are mapping the Course you would have a property for Grade mapped out,

<class name="Course" table="Course">

    <id name="Id" column="Id" />
    <property name="Name" column="Name" />
    <property name="Description" column="Description" />
    <property name="FinalGrade" column="FinalGrade" />

</class>

but where would you specify the relational table to pull the Grade from? With the code above NHibernate will blow chunks, saying it cannot find the column FinalGrade in the Course table even though it is a property on your Course entity.

 

Well, in NHibernate there is a <join> mapping that you can use to map columns from other tables to the entity you are working with.  So, in the example we would replace the mapping:

<property name="FinalGrade"column="FinalGrade"/>

with:

<join table="Student_Course"optional="true">
    <key column="CourseId"/>
    <property name="FinalGrade"column="FinalGrade"/>
</join>

We would specify the many to many relational table with the table parameter.  Set the key column to the CourseId so NHibernate can join on the Id and you specify the property/column as normal.  Note the optional attribute, this is used to specify if you still want results returned where FinalGrade may be null.  This is more apparently useful in situations where you are loading courses and you don’t really care if there are FinalGrades associated with the Courses. 

 

Now when I am looking at a Student’s Courses the will all have the final grade populated.

Musings<Biefeld> is proudly powered by WordPress
Entries (RSS) and Comments (RSS).