Blog

ATAYE HOME Blog Home Account Search
Using Clientside Validator

Hello,

To use the client side validator you first need to add one to your page.  Then in html view add a new javascript method as follows: (Im using the dotnet v2.0)

<script type="text/javascript">
function
ValidateClientItem(sender, args)
{
    var
sMyVal = args.Value;

    if (sMyVal != sValidString
)
    {
        args.IsValid = false
;
        return
;
    }
    args.IsValid = true
;
}
</script>

Then, set the ClientValidationFunction property of the custom validator to the name of the function, in this case 'ValidateClientItem'.  You dont need to set ControlToValidate if you dont want, but you wont be able to use args.Value.

Thats all there is to it.  When the client form is submitted, the validator is called and will in turn call your client side javascript method.  Setting args.IsValid to false cancels the form submit and displays the validator error message.

Happy coding,

James

ASP.Net secure (or unsecure) parts of a site using web.config

Hello,

To secure my dotnet sites i like to use the built-in authorization module of dotnet using the login control.  I usually set the main web.config to allow="*" and deny="?" and by setting the authentication mode="Forms" and supplying a loginUrl="~/Login.aspx" (or some other login page) sends the user to the login page if they havn't already logged in, thus requiring authentication before access the site.

The problem with this is having the web.config sitting in the root of the site and denying all unauthorized users.  This now secures *everything* including stylesheets and images etc.  To get around this you can specify security exclusions in the web.config file using the location node.

Heres a sample:

<configuration>
  <system.web>
    <authentication mode="Forms"
>
      <
forms name="SiteLogin" loginUrl="~/Login.aspx"
/>
    </
authentication
>
    <
authorization
>
      <
deny users="?"
/>
      <
allow users="*"
/>
    </
authorization
>
  </system.web>

  <location path="SiteStyle.css">
    <
system.web
>
      <
authorization
>
        <
allow users="?"
/>
      </
authorization
>
    </
system.web
>
  </
location>

  <location path="images">
    <
system.web
>
      <
authorization
>
        <
allow users="?"
/>
      </
authorization
>
    </
system.web
>
  </
location>
<configuration>

The above is the web.config which sits in the root of the site.  The two location nodes specifies that anonymous users can access the SiteStyle.css file and all files within the images folder!

Cheers,

James

How to return identity after doing an Insert with ADODB and Classic ASP.

Hello,

Today i thought i'd share an easy way to return an identity after doing an insert with classic ASP and the ADODB object.

Basically we open a connection and perform actions on the open connection, then close when we are finished.  By keeping the connection open we are re-using the same session.  Following is a code listing.

Dim cnn, rsPK, sSql, iTablePK

' Open a connection:
Set cnn = Server.CreateObject("ADODB.Connection")
cnn.Open ConnString

' Perform the insert into our table:
sSql = "INSERT INTO tbOrderDetails (FKOrderID, TourCode) values (1, 'Code')"
cnn.Execute sSql, , adCmdText + adExecuteNoRecords

' Retrieve the identity:
set rsPK = Server.CreateObject("ADODB.Recordset")
rsPK.Open "SELECT @@Identity", cnn,  0, 1, 1

' We now have the new record PK in iTablePK!
if (not rsPK.EOF) then
   iTablePK = rsPK(0)
end if

' Clean up:
cnn.close
set cnn = nothing

So, we open a connection and while we keep it open we perform the insert into the table with the identity field.  We then select the new table field using @@identity.  Keep in mind @@identity is GLOBAL to the connection, meaning it will return the latest identity regardless of where the insert occured!!  (i.e. table trigger performing an insert).  To be safer you could use the SCOPE_IDENTITY() method, which is LOCAL to the table.

Ok, i hope this helps.

Cheers,
James

How to return identity after insert using DotNET Sql Data source without using Stored Procedure

Here is a little trick i use to return the identity from a PK column after doing an insert with the Sql Data source component.  Since this is done at the data source level it can be used with any databound control like the FormView, DetailsView or any other 2-way databound control.

To make this happen just do the following:

  1. Enter your insert command and setup your datasource for inserting.
  2. Append to the end of the insert (this can be done in the Source view of the page) the following:
    • ; select @PK=SCOPE_IDENTITY();

  3. Now add a new parameter to the <InsertParameters> node of the SqlDataSource (Change the datatype to match):
    • <asp:Parameter Name="PK" Direction="Output" Type="Int16" />

  4. No you can access the new PK value in the 'Inserted' event after the data has been inserted eg:
    • this.Response.Write(e.Command.Parameters["@PK"].Value.ToString());

So what happenes is after the insert the sql sets the new identity to the parameter @PK which is declared on the data source as an output parameter.  Then the value can be read after the insert through the Inserted event.

I hope this helps.

Happy coding.

 


 
copyright 2007 Ataye.com.au