Wednesday, 30 March 2011

Reset SQL identity to a custom value (RESEED)

--Replace tblName and 0 with the number you wish to reset the default value to DBCC CHECKIDENT('tblName', RESEED, 0)

Tuesday, 10 March 2009

C# Form DefaultButton Firefox fix

When should you use default buttons?

A default button should be set when a page has the requirements for two or more forms or buttons, if no default button is set then the first button within the HTML document will be submitted. This default behavoiur is fine for a page with a singular form, however if like we stated earlier their are two or more forms or buttons then this behaviour is not acceptable as in most cases the first button may be a cancel action. This for a user would be very frustrating after filling in a lengthy form, or the user may even think that the form was submitted.

This problem only occurs when the return key is pressed on an input, as we stated earlier if no default button is set then the first button will be submitted

Note that within .Net only one server side form may be implemented.

I will present two solutions the first of which is commonly used but doesn't work, so I felt this was worth presenting.

Solution 1 (not suitable)

You can set a default button only in .Net 2.0+ by using the following code within the server side script.

Page.Form.DefaultButton = btnSubmit.ClientID;

The problem with this solution is it doesn't fix a bug exposed within Firefox, where a textarea will not allow new lines to be inserted. The default action for a textarea within a form should be to create a new line, however an element which is required within Firefox is missing, we will explain this in a bit more detail in solution 2.

Solution 2

This solution fixes a firefox bug, Firefox doesn't support an element known as event.srcElement within javascript. This problem affects the HTML textarea tag. When a return key is pressed within the textarea the default action should be to drop down to a new line. However in .Net 2.0 the action performed is to submit the form.

We can fix this by using this javascript within the header or by creating a javascript file and importing the file into the header:

Javascript 'default-button.js (file to be added)'
// Fix Firefox bug : stop textarea on return key press from submitting the form
function FireDefaultButton(event, target) {
    // srcElement is for IE
    var element = event.target || event.srcElement;

    if (13 == event.keyCode && element != undefined && element.tagName.toLowerCase() != "textarea") {
        var defaultButton = document.getElementById(target);

        if (defaultButton != undefined && defaultButton.click != undefined) {
            defaultButton.click();
            event.cancelBubble = true;

            if (event.stopPropagation) event.stopPropagation();

            return false;
        }
    }
    return true;
}

To use this new function we cannot use the normal default button that is defined by .Net on the form element (solution 1).

We have to add in this line of code to register the event for the client side, please replace btnMyButton with the input that has a runat at server attribute:

Page.Form.Attributes["onkeypress"] = "return FireDefaultButton(event, '" + btnMyButton.ClientID + "')";

Producing the effect of two forms on the same page you ask?

Using a similiar technique we can produce the effect of two forms on one page. To do this we will use a div instead of the form to add our 'onkeypress' event

please replace btnMyButton with the input that has a runat at server attribute:

divForm.Attributes["onkeypress"] = "return FireDefaultButton(event, '" + btnMyButton.ClientID + "')";

Tuesday, 11 November 2008

Accessible Radio Buttons

Problem: This has been on my mind for a while about the best way to use labels with radiobuttons and checkboxes. After discussing this with a friend we both looked at some solutions that would validate and point at all of the inputs. The reason that this came about is because we want to make sure that the markup is produced with the best semantic layout/meaning for screen readers as well as standard validation. We came up with the following courtesy of http://www.webstandards.org/learn/tutorials/the-label-element/ Now please bare in mind this isn't ground breaking but maybe should form part of all contact forms we create. Our normal solution would be something on the lines of: <p id="parCardholderConfirm" class="question">Are you the cardholder?</p> <ul class="radiobuttons"> <li> <input name="confirmcardholder" id="rbtnConfirmCardholderYes" runat="server" type="radio"> <asp:label id="lblConfirmCardholderYes" runat="server" associatedcontrolid="rbtnConfirmCardholderYes">Yes</asp:label> </li> <li> <input name="confirmcardholder" id="rbtnConfirmCardholderNo" runat="server" type="radio"> <asp:label id="lblConfirmCardholderNo" runat="server" associatedcontrolid="rbtnConfirmCardholderNo">No</asp:label> </li> </ul> New proposed solution: <asp:label id="lblCardholderConfirm" runat="server"> Are you the cardholder? <input name="confirmcardholder" id="rbtnConfirmCardholderYes" runat="server" type="radio"> <asp:label id="lblConfirmCardholderYes" runat="server" associatedcontrolid="rbtnConfirmCardholderYes">Yes</asp:label> <input name="confirmcardholder" id="rbtnConfirmCardholderNo" runat="server" type="radio"> <asp:label id="lblConfirmCardholderNo" runat="server" associatedcontrolid="rbtnConfirmCardholderNo">No</asp:label> </asp:label> The solution makes use of the implicit label rather than the normal explicit. Implicit Label: <asp:label id="lblName" runat="server"> Please enter your name: <asp:textbox id="txtName" runat="server" tooltip="Name" maxlength="50"> </asp:textbox> </asp:label> Explicit Label: <asp:label id="lblName" runat="server" associatedcontrolid="txtName">Please enter your name:</asp:label> <asp:textbox id="txtName" runat="server" tooltip="Name" maxlength="50"> </asp:textbox> My latest solution is: <label for="business">What business sector do you work in?</label> <p id="business"> <label for="retail">Retail</label> <input id="retail" title="Retail" type="radio"> <label for="business">Business</label> <input id="business" title="Business" type="radio"> </p> Please leave any feedback that may help sway my decision of the best method. Some of my resources include: The Web Standards Project