Supertext Home
Chief of the System Blog

Archive for the 'Programming' Category


DataNavigateUrlFormatString with multiple fields

Wednesday, January 16th, 2008

If you have an ASP.NET GridView with a HyperLinkfield you can not only pass just one DataField, but actually multiple in a very simple way.

The Property is already called DataNavigateUrlFields (with an s, hint, hint).

<asp:HyperLinkField DataNavigateUrlFields="iddocument,documentName" DataNavigateUrlFormatString="../DocumentDownload/{0}/{1}" DataTextField="documentName" HeaderText="Document" SortExpression="documentName" />

Not much more to it. Got the hint from AzamSharp. Alternatively you can always use a TemplateField, which gives you more flexibility.

<asp:TemplateField> <ItemTemplate> <asp:HyperLink ID="lnkShowFile1" runat="server" NavigateUrl='<%# "../DocumentDownload/" + Eval("iddocument") + "/" + Eval("documentName") %>' > <%# Eval("documentName") %> </asp:HyperLink> </ItemTemplate> </asp:TemplateField>


Very simple MySQL backup via FTP script

Friday, November 9th, 2007

There are tons of pretty enhanced scripts out there to make backup and also to copy them to FTP sites, but what I needed was just something really simple to have my daily backup saved somewhere.

The script below will dump your DB into the backup.sql file and copy it to an FTP server of your choice into the root directory of that user. Can’t be any simpler.

Here it is:

The BackupAndFTP.cmd file:

mysqldump -uuserName -ppassword DBName > backup.sql ftp -s:ftpcommands.txt ftp.hostname.com

The ftpcommands.txt file:

username password binary put backup.sql bye

Just put both code blocks into an individual file, replace the usernames and passwords (first for MySQL and then for the FTP access), replace DBName with the name of the database you wanna save and that’s it.


Changing the MySQL my.ini File in Windows Vista

Friday, November 9th, 2007

Not sure if I’m the only left that uses Vista with the UAC (User Account Control), but it definitely got me into trouble yesterday.

I tried to change the my.ini (Configuration File for the MySQL Database), but due to some Vista security feature it created a Shadow Copy of the file and MySQL never actually got to see my changes.

You can check this via the MySQL command prompt. In my case I had to adjust the max_allowed_packet setting, so if you run:

mysql> show variables like 'max%' ; 

+-----------------------+------------+
| Variable_name         | Value      |
+-----------------------+------------+
| max_allowed_packet    | 1048576    |

So, this is a good way to check if your changes actually have an effect.

Now, to change the my.ini file, it helps to know where and how MySQL reads it.

This Section in the MySQL Online help explains it nicely. As a summary, this is where it reads from:

  • WINDIR\my.ini Global options
  • C:\my.cnf Global options
  • INSTALLDIR\my.ini Global Options
  • defaults-extra-file The file specified with --defaults-extra-file=path, if any

If you start MySQL as a service you can also use the defaults-file option. More info here.

What we can do to have our my.ini file read, is to make a copy, store it somewhere outside of the Program Files path and point defaults-file to it. It’s a registry setting:

KEY:
HKU\SYSTEM\CurrentControlSet\Services\MySQL\ImagePath

Value:
“C:\Program Files\MySQL\MySQL Server 5.0\bin\mysqld-nt” –defaults-file=”C:\MySQL\my.ini” MySQL

Good luck!


See search results as you type – An ASP.NET Ajax Control

Tuesday, June 19th, 2007

Search as you type

Highrise and other new Ajax enabled tools have this feature that when you type in a search query, it automatically updates the search results below. It’s almost like a Auto-Complete Box, but with full results on the page instead of a drop down list below the control. Unfortunately I didn’t find anything like this in the Microsoft ASP.NET Ajax Toolkit.

So there was only one solution, build it myself.

Here is the result:

Live Demo
VS 2008 Solution with Source Code
There is some other stuff in there too, but the demo for this project is under /RemyExamples/DelayedSubmitExample and the source code for the Control under /DelayedSubmit.

It’s my first Ajax Control, so I’m sure there is room for improvements. I’ve tested it in IE7, FF and Safari. Seems to work fine in all of them.

I used the Membership Editor Example from Peter Keller and TextChangedBehavior.js from Garbin as my inspiration and resource, but started with a VS ASP.NET Ajax Control Project to get the framework up and running.

The only thing the code really does is start a time on the keyup event, stop the time on keydown and after the time fires, it executes the onchange method of the associated Textbox. This way we get a delayed postback after the user stopped typing and not tons of postbacks when he’s still writing something.

_onkeyup : function(ev) {
    var k = ev.keyCode ? ev.keyCode :
                ev.rawEvent.keyCode;
    if (k != Sys.UI.Key.Tab) {
        this._timer.set_enabled(true);
    }
},

_onkeydown : function(ev) {
    this._timer.set_enabled(false);
},

_onTimerTick : function(sender, eventArgs) {
    this._timer.set_enabled(false);

    if(this._text != this.get_element().value) {
        this._text = this.get_element().value;

        this.get_element().onchange();
    }
},

Everthing else is just setup and teardown code. In the ASP page, one can just add this Control like any other Extender and add an OnChange handler to the textbox.

<cc1:DelayedSubmitExtender ID="DelayedSubmit"
    runat="server" Timeout="1000"
    TargetControlID="TextBox1"/>
<asp:TextBox ID="TextBox1" runat="server"
    AutoPostBack="True"
    OnTextChanged="TextBox1_TextChanged"
    Columns="50"></asp:TextBox>

Please let me know how it goes and if it is of any use. Good luck!

 

kick it on DotNetKicks.com


HTML Entity Character Lookup

Monday, June 4th, 2007

For all non-english speaking developers that are constantly fighting with umlauts and other funny language specific characters, here is a tool that might help:

HTML Entity Character Lookup

You type in one Character and it spills out all versions that are somewhat similar to it with the appropriate HTML entity.

E.g. for a, you get â ã ã and more. Including the entity code like for example &auml.

Pretty helpful.


Imitate the css hover effect in IE6 with Prototype, Behaviour or Event-Selectors

Sunday, February 18th, 2007

As most here might already know, Internet Explorer 6 only supports the CSS hover tag for anchor elements. Very unfortunate I think, since this is often a nice effect to have.

This is how it would work:

li.CssExample:hover { background-color: Lime; display: inline; margin: 10px; }

CSS Demo

The most obvious solution is to add some javascript to the element, like for example this:

<li class="JavascriptExample" onmouseover = "javascript:this.className='JavascriptExample_hover';" onmouseout = "javascript:this.className='JavascriptExample'"> Supertext Home</li>

Demo Javascript

This is obviously not very nice to look at and also a pain to maintain. But there are good solutions out there. Prototype, Behaviour and Event-Selectors are some of them I will have a look at here.

Prototype

Let’s look at Prototype first. It’s the biggest with about ~70kb and also most powerful JavaScript library out of the three.

Event.observe(window, 'load', function(){ $$('li.PrototypeExample').each(function(el){ Event.observe(el, 'mousemove', function(){ el.className = 'PrototypeExample_hover'; }); Event.observe(el, 'mouseout', function(){ el.className = 'PrototypeExample'; }); }); });

This would do exactly the same as adding the above Javascript to every list element. We use the new $$ CSS selector from Prototype to get a list of all elements with the CSS class PrototypeExample. But one can do it even smaller:

Event.observe(window,'load',function(){ $$('li.PrototypeExample').each(function(el){ el.onmouseover = function(){ this.className = 'PrototypeExample_hover'; } el.onmouseout = function(){ this.className = 'PrototypeExample'; } }); });

Not 100% sure about it, but I think the main disadvantage of this method is that you cannot just call stopObserving() to unhook the event, but for a simple Roll-Over effect it should be good enough.

Demo Prototype

Behaviour

Another library that solves this problem is Behaviour, initially Prototype didn’t have the $$ function, so Behaviour came to rescue. It’s also only about 5kb and does not rely on Prototype. If you only need to add some event handler to your code, this is most likely the way to go.

var myrules = { 'div.formEntry' : function(el){ el.onmouseover = function(){ this.className = 'formEntry_hover'; } el.onmouseout = function(){ this.className = 'formEntry'; } } }; Behaviour.register(myrules);

As you can see, it’s a little bit simpler and you don’t need to add it to the window load event.

Demo Behaviour

Event-Selectors

Last but not least, the same thing again, but with Event-Selectors. This is an extension to Prototype (meaning you need to include Prototype too).

var Rules = { 'li.EventSelectorsExample:mouseover': function(element) { element.className = 'EventSelectorsExample_hover'; }, 'li.EventSelectorsExample:mouseout': function(element) { element.className = 'EventSelectorsExample'; } } EventSelectors.start(Rules);

One thing to keep in mind here, so that you either have to add it to the window onload event or add it at the end of your code and not in the beginning. I think the main advantage of Event-Selectors is that you can add multiple events and elements to one action like in the Example below (copied from their homepage):

'#icons a:mouseout, #other-item:mouseover': function(element) { var app = element.id; new Effect.BlindUp(app+'-content',{queue: 'end', duration: 0.2}); }

Demo Event-Selectors

Debugging Tips:

  • Do you add the events at the beginning or at the end? If it doesn’t work, try it in the windows onload event.
  • Are you using ‘onmouseover’ or just ‘mouseover’? Check the examples!
  • Is your element identifier an ID or a class name? ID’s are normally prefixed with #.

The only thing missing is an example with the Microsoft Ajax Library. Have not found too much about it yet, but I promise I will work on it.

  • Topics
  • Archive
  • Subscribe
  • Facebook
  • Twitter