Posted on Thursday, November 19, 2009 at 9:15 PM
Here is an example of how to add a button to a page at a specific location and then have it call a function onclick.
// Inserts javascript that will be called by the autoCheckOrderButton
var scriptElement = document.createElement('script');
scriptElement.type = 'text/javascript';
scriptElement.innerHTML = 'function checkForOrders() { alert("inside checkForOrders"); }';
document.getElementsByTagName("head")[0].appendChild(scriptElement);
window.addButton = function () {
// Get the location on the page where you want to create the button
var targetDiv = document.getElementById('offer');
// Create a div to surround the button
var newDiv = document.createElement('div');
newDiv.setAttribute('id', 'autoCheckOrder');
// Create the button and set its attributes
var inputButton = document.createElement('input');
inputButton.name = 'autoCheckOrderButton';
inputButton.type = 'button';
inputButton.value = 'Start Checkin';
inputButton.setAttribute("onclick", "checkForOrders();");...
Posted on Thursday, September 17, 2009 at 6:49 AM
The following method would be called after a form submit occurs on the popup. I placed the method call in the OnClick event for my submit button. The preprocessor directive of System.Text is required for the stringbuilder that I use below (You don't actually have to use a stringbuilder, but I find it makes it easier to read and debug later).
private void RegisterCloseWindowScript()
{
if (!Page.IsClientScriptBlockRegistered("CloseWindow"))
{
StringBuilder sb = new StringBuilder();
sb.Append("<script language='javascript'>");
sb.Append("window.opener.location.href = ");
sb.Append("window.opener.location.href;");
sb.Append("window.opener.focus();");
sb.Append("window.close();");
sb.Append("</script>");
Page.RegisterClientScriptBlock("CloseWindow", sb.ToString());
}
}
Comments:
0
Tags:
ASP.NET,
C#,
JavaScript
Posted on Monday, August 31, 2009 at 7:59 AM
The following code is an example of how to find a list of all person records that have the firstname of 'Jim' (probably not the best way, but this is just an example of this method).
SELECT
PersonId
FROM
Person
WHERE
'Jim' IN (SELECT FirstName FROM Person)
Posted on Wednesday, August 26, 2009 at 10:53 AM
First we need to setup a row counter and a row max. In this example I'm iterating through a table of coupons.
DECLARE @RowCounter int
DECLARE @RowMax int
SET @RowCounter = 1
SET @RowMax = (SELECT COUNT(*) FROM Coupon)
And this is how the while loop is created
WHILE (@RowCounter <= @RowMax)
BEGIN
-- Here you could enter real logic to process, I am just printing the RowCounter.
PRINT 'This is row ' + CAST(@RowCounter as nvarchar(10))
-- The RowCounter must be incremented each time to ensure the loop continues on.
SET @RowCounter = @RowCounter + 1
END
Comments:
0
Tags:
loop,
MSSQL,
Count
Posted on Wednesday, August 26, 2009 at 6:58 AM
I use this frequently to make quick-and-dirty data backups when I am experimenting with scripts. THIS SHOULD NOT REPLACE REAL DATA BACKUPS!
SELECT *
INTO
DestinationTable
FROM
OriginTable
Comments:
0
Tags:
Copy,
MSSQL
Posted on Tuesday, August 25, 2009 at 10:37 AM
The following is an example of how to validate a field using the asp:customvalidator control. I am going to be validating a textbox to ensure that its entry is alphanumeric without any spaces.
This is the field to be validated...
<asp:TextBox id='PersonId' runat='server' />
Here is the asp:CustomValidator on the aspx page.
<asp:CustomValidator id='PersonIdCustomValidator' runat='server'
Display='Dynamic'
ControlToValidate='PersonId'
EnableClientScript='true'
ClientValidationFunction='ValidatePersonId'
OnServerValidate='ValidatePersonId'
Text='* PersonId must be alphanumeric without any spaces' />
Javascript implementation of the client-side validation.
function ValidatePersonId(source, args)
{
var re = new RegExp("^\\w*$");
args.IsValid = (window.event.srcElement.value.match(re));
}
C# codebehind implementation of the server-side validation.
protected void ValidatePersonId(object source, ServerValidateEventArgs args)...
Posted on Tuesday, August 25, 2009 at 10:24 AM
First we need to create the function...
CREATE FUNCTION dbo.AddTwoNumbers(@Value1 as int, @Value2 as int)
RETURNS int
BEGIN
DECLARE @Result int
SET @Result = @Value1 + @Value2
RETURN @Result
END
GO
Here is how we actually use the function...
SELECT dbo.AddTwoNumbers(5, 8) AS Result
GO
The SELECT statement outputs the integer 13.
Posted on Tuesday, August 25, 2009 at 10:18 AM
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[Person]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [dbo].[Person]
(
PersonId int IDENTITY(1,1) NOT NULL,
FirstName nvarchar(50) NOT NULL,
LastName nvarchar(50) NOT NULL,
BirthDate date NOT NULL,
FavoriteColor nvarchar(50) NULL CONSTRAINT [DF_Person__FavoriteColor] DEFAULT(''),
CONSTRAINT [PK_Person] PRIMARY KEY CLUSTERED (PersonId)
)
END
GO
Comments:
0
Tags:
MSSQL,
Create Table
Posted on Tuesday, August 25, 2009 at 7:07 AM
Here is an example of how to remove duplicates from a temporary table in MSSQL.
create table marxBrothers (
ident int IDENTITY,
Name varchar(32)
)
go
insert marxBrothers (Name)
select 'Groucho Marx' UNION ALL
select 'Harpo Marx' UNION ALL
select 'Chico Marx' UNION ALL
select 'Groucho Marx' UNION ALL
select 'Harpo Marx' UNION ALL
select 'Chico Marx' UNION ALL
select 'Zeppo Marx' UNION ALL
select 'Gummo Marx' UNION ALL
select 'Zeppo Marx'
select * from marxBrothers
delete marxBrothers
from marxBrothers,
(
select min(ident) as minIdent, name
from marxBrothers m
group by name
having count(1) > 1
) as derived
where marxBrothers.name = derived.name
-- This 'and' makes sure that one of the duplications is not deleted,
-- remove it if you do not want one copy of the duplication to remain.
and ident > minIdent
select * from marxBrothers...