How to run client side javascript on a server control

Here is some code to run a javascript function on the client side when a value changes in a drop down box. In this case I have a list of dates populating a drop down box. When the date changes the value of a DIV section changes to a corresponding end date.

In the appSetting section of the web.config file:

<add key=StartDates value=12/06/06, 02/27/07, 05/09/07, 07/03/07, 10/10/07, 01/08/08, 03/02/08/>
<
add key=EndDates value=10/08/07, 12/21/07, 03/18/08, 05/29/08, 08/18/08, 10/29/08, 01/27/09/>

In the page load section:

 ‘ Get the start and end dates from the web.config file
 
 Dim StartDates As Array
 StartDates = ConfigurationManager.AppSettings(“StartDates”).Split(“,”)
 EndDates = “‘” & ConfigurationManager.AppSettings(“EndDates”).Replace(“, “, “‘, ‘”) & “‘”  ‘ format the string for a javascript array
 EndDate = EndDates.Substring(0, EndDates.IndexOf(“,”)).Replace(“‘”, “”)
 
 ‘ Populate the Start Date drop down / combo box
 
 ddlStartDate.AppendDataBoundItems = True
 Dim Item As String
 For Each Item In StartDates
  Items = New ListItem
  Items.Text = Item
  Items.Value = Item
  ddlStartDate.Items.Add(Items)
 Next
 
 ‘ Register the javascript code to run on the client side
 
 ddlStartDate.Attributes.Add(“onchange”, “javascript: doAction(” + ddlStartDate.ClientID + “)”)

Then in the web form:

<table border =”0″><tr><td><asp:DropDownList ID=”ddlStartDate” runat=”server”></asp:DropDownList></td><td><div id=”EstCompDate”>Est. Comp. Date: <b><%=EndDate%></b></div></td></tr></table>

Then at the bottom of the HTML of the page (just above the </html> tag):

<script language=”javascript”>
var
endDates = new Array(<%=EndDates %>);
function doAction(id) {
   var selection = id.options[id.selectedIndex].value;
   EstCompDate.innerHTML =
‘Est. Comp. Date: <b>’ + endDates[id.selectedIndex] + ‘</b>’;
}
</script>

How to send an email by authenticating with any mail server

If you want to send an email from an application that might not be on a server that allows relaying you can use the System.Net.NetworkCredential. Just add your account login and password and your email will route through the remote server.

    Dim sTo As String = “someone@someemail.com
    Dim sFrom As String = “me@myemail.com
    Dim sSubject As String = “Using the new SMTP client.”
    Dim sBody As String = “Using this new feature, you can send an e-mail message from an application very easily.”
    Dim msg As New Net.Mail.MailMessage(sFrom, sTo, sSubject, sBody)
 
    Dim client As New Net.Mail.SmtpClient(“mail.myemail.com”, 25)
    Dim SMTPUserInfo As New System.Net.NetworkCredential(“me@myemail.com“, “password”)
    client.UseDefaultCredentials = False
    client.Credentials = SMTPUserInfo
 
    client.Send(msg)

Restore Database and Database User

When restoring an SQL Server 2005 database from a different database instance the database login will be orphaned. Use this SQL script to fix the orphaned login.

sp_change_users_login ‘Auto_Fix’, ‘user’, NULL, ‘password’

Actual syntax:

sp_change_users_login [ @Action = ] ‘action’     [ , [ @UserNamePattern = ] ‘user’ ]     [ , [ @LoginName = ] ‘login’ ]         [ , [ @Password = ] ‘password’ ]

See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_sp_ca-cz_8qzy.asp

 

PHP 5 on Windows 2003 Server (IIS)

Here are my notes on setting up PHP 5.1.6 on a Windows 2003 server (SBS).

  1. Must have “c:/php/” in system PATH environment variables (must reboot server when changing).
  2. In IIS under Web Service Extentions must have c:\php\php5isapi.dll Enabled.
  3. In IIS under the default web site (and particular web site to use PHP), configure the Application extention for .php to have path “C:\PHP\php5isapi.dll”, limit verbs to “GET,HEAD,POST”.
  4. Put php.ini in the c:\windows directory. The ISAPI extention didn’t find the ini file in the php directory.
  5. Add IUSR_Machinename to the PHP directory.
  6. In the php.ini file, hard code the extentions path, i.e. extension_dir = “c:/php/ext/”.
  7. In the php.ini file, comment out the doc_root line, i.e. ‘;doc_root = “c:\Inetpub\wwwroot” ‘.
  8. In the php.ini file, hard code the path to the session.save_path, i.e. ‘session.save_path = “c:/php/sessiondata” ‘.
  9. Add IUSR_Machinename write permission to the sessiondata and file upload folders.
  10. In the php.ini folder, turn display_errors = On to see error information.
  11. Always restart IIS when making changes to the php.ini file.
  12. Use this function to test PHP:
    <?php
    phpinfo();
    ?>
  13. Run PHP from a dos prompt at the PHP folder to view any missing DLL’s. This does not test the ISAPI extention pathing though.

Hope this helps someone. I found the PHP documentaion to be a bit lacking. At the time of this writing there was not an installer for PHP 5 (at least that I could find). I was also upgrading from PHP 4 (cgi) and MySQL 4 to MySQL 5.

Some of the trouble I encountered was a completely white screen (no error message at all), MySQL would not load into PHP, and “No input file specified” (doc_root).

 

Permanent Redirect

Instead of doing a Response.Redirect to point to a page in a new location (I.e. the page has been renamed), do a permanent redirect. Using this code tells the search engines to no longer index the old page but to index the new page.

   Response.Status = “301 Moved Permanently”
   Response.AddHeader “Location”, “new-page-name.asp”

CSS Alignment Tip

When positioning text on a page in CSS add this code to view the element borders:

border-width: thin; border-style: solid;

That way you can see where the spaces are and can adjust the elements spacing.