5 Ways To Make GUIDs with Javascript and Custom Attributes

Tips submitted by ASP.NET Maker users

5 Ways To Make GUIDs with Javascript and Custom Attributes

Postby xgis » Sun Dec 22, 2013 9:24 pm

Some fields need GUIDs generated that may not be automated.
In this example I will demonstrate a number of methods to achieve this goal.
These implementations may be adapted to ANY FUNCTION for dynamic data generation and insertion using Javascript.

Javascript is used to generate the GUID itself and 5 methods to initiate are detailed.
In this example it is position on the Add/Copy Page under Client or Startup Script depending on where you want the button to display.
The button is actually ONLY used in one of the following implementations.

Here is the Javascript that creates the GUID. This may be located in Client Scripts where it is required.
function S4() {
return (((1+Math.random())*0x10000)|0).toString(16).substring(1);

function GenerateGUID()
guid = (S4() + S4() + "-" + S4() + "-4" + S4().substr(0,3) + "-" + S4() + "-" + S4() + S4() + S4()).toLowerCase(); //This line calls the previous function and works the magic.
//alert(guid); //Only use this if you want to see the GUID in a message box
document.getElementById("x_ClientFunctionGUID").focus(); //This line is used when the onfocus method is required
document.getElementById("x_ClientFunctionGUID").value = guid; //This line places the generated data in the required field Simply replace "ClientFunctionGUID" with your field name

1. Using a Javascript Button
To Create a button at the TOP of the FORM use the following line of code in "Client Script" section;
document.write('<button id="btnGUID" onclick=GenerateGUID()>Generate GUID</button>');
To Create a button at the BOTTOM of the FORM use the previous line of code in "Startup Script" section;

2. Make the Textbox Act Like a Button
If you prefer to eliminate an extra control and use the textbox to initiate the action then;
"onclick=GenerateGUID()" as the Custom Attribute and it will generate a GUID when you click in the textbox

3. Let the application force you into the textbox
If you do not want to use a button and prefer an event the following "Custom Attribute" for the relevant FIELD will do the trick;
If the field is REQUIRED and you go to ADD the record the Cursor will be placed in the textbox when you click OK on the Error Message Box.
onfocus will then automatically generate the GUID

4. Use the Mouse Hover
If you simply want to allow the user to pass the mouse across a textbox and have it generate automatically use the following line in your Custom Attribute;

5. Use the Mouse Hover and take your pick
Just for fun I have included the next event so you can see how many GUIDs can be generated in less than a second by just passing the mouse across the textbox

Dont forget to set your dynamically generated field checkbox to EDIT | Read Only so users cannot change the GUID value.
Posts: 87
Joined: Thu Jan 17, 2013 10:40 pm

Return to User Submited Tips (ASP.NET Maker)