| |||||||
This is a discussion on Some useful functions... within the ASP/ASP.NET section, part of the Programming category; Hai mates . Here i am starting a new thread for useful functions which can be used in classic asp/asp.net..If ...
![]() |
|
| | LinkBack | Thread Tools | Display Modes |
|
#1
| ||||
| ||||
| Some useful functions...
Hai mates . Here i am starting a new thread for useful functions which can be used in classic asp/asp.net..If you have any scripts that you think may be helpful to others please post here .... ![]() Function for format date(asp): Code: Function FormatMediumDate(DateValue)
Dim strYYYY
Dim strMM
Dim strDD
strYYYY = CStr(DatePart("yyyy", DateValue))
strMM = CStr(DatePart("m", DateValue))
If Len(strMM) = 1 Then strMM = "0" & strMM
strDD = CStr(DatePart("d", DateValue))
If Len(strDD) = 1 Then strDD = "0" & strDD
FormatMediumDate = strYYYY & "/" & strMM & "/" & strDD
End Function
Function for generate random string(asp) Code: function GenerateRandomString(intLength) Randomize strRndString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" For i = 0 to intLength intPos = Int(36 * Rnd + 1) strRandom = strRandom & mid(strRndString,intPos,1) GenerateRandomString = strRandom Next end function Function for generate random number (asp) Code: Function RandomNumber(intHighestNumber) Randomize RandomNumber = Int(Rnd * intHighestNumber) + 1 End Function Spec_tray
__________________ " There are no failures - just experiences and your reactions to them. New members Please read before u post General Tech6 Rules !!! CSS Validator | Markup Validator | RSS Validator |
|
#2
| ||||
| ||||
|
301 redirect is the most efficient and Search Engine Friendly method for webpage redirection. It's not that hard to implement and it should preserve your search engine rankings for that particular page. If you have to change file names or move pages around, it's the safest option. The code "301" is interpreted as "moved permanently". ASP Redirect:- Code: <%@ Language=VBScript %> <% Response.Status="301 Moved Permanently" Response.AddHeader "Location","http://www.your-new-url.com/" %> Code: <script runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location","http://www.your-new-url.com");
}
</script>
__________________ Would you like to Link To Us | Support TECH6 by going Premium Know more about me at Vinayaks.com | Follow TECH6 at Twitter |
|
#3
| ||||
| ||||
| For replacing single quotes and reversing the replacement (it can be used to remove single quotes from sql insert statement) Code: function rep_sq(stg)
if isnull(stg)=false then
rep_sq=replace(stg,"'","’")
else
rep_sq=""
end if
end function
function rep_sq_rev(stg)
if isnull(stg)=false then
rep_sq_rev=replace(stg,"’","'")
else
rep_sq_rev=""
end if
end function
Return value - the submitted string after cleaning up Code:
function stripHTML(str)
stripHTML = Replace(Replace(str, "<", "<", 1, -1, 1), "<", "<", 1, -1, 1)
end function
__________________ " There are no failures - just experiences and your reactions to them. New members Please read before u post General Tech6 Rules !!! CSS Validator | Markup Validator | RSS Validator |
|
#4
| ||||
| ||||
| Return value - the string after cleaning up Code: Private Function characterStrip(strTextInput)'Dimension variable Dim intLoopCounter 'Holds the loop counter 'Loop through the ASCII characters For intLoopCounter = 0 to 37 strTextInput = Replace(strTextInput, CHR(intLoopCounter), "", 1, -1, 0) Next 'Loop through the ASCII characters For intLoopCounter = 39 to 44 strTextInput = Replace(strTextInput, CHR(intLoopCounter), "", 1, -1, 0) Next 'Loop through the ASCII characters numeric characters to lower-case characters For intLoopCounter = 65 to 94 strTextInput = Replace(strTextInput, CHR(intLoopCounter), "", 1, -1, 0) Next 'Loop through the extended ASCII characters For intLoopCounter = 123 to 125 strTextInput = Replace(strTextInput, CHR(intLoopCounter), "", 1, -1, 0) Next 'Loop through the extended ASCII characters For intLoopCounter = 127 to 255 strTextInput = Replace(strTextInput, CHR(intLoopCounter), "", 1, -1, 0) Next 'Strip individul ASCII characters left out from above left over strTextInput = Replace(strTextInput, CHR(59), "", 1, -1, 0) strTextInput = Replace(strTextInput, CHR(60), "", 1, -1, 0) strTextInput = Replace(strTextInput, CHR(62), "", 1, -1, 0) strTextInput = Replace(strTextInput, CHR(96), "", 1, -1, 0) 'Return the string characterStrip = strTextInputEnd Function
__________________ " There are no failures - just experiences and your reactions to them. New members Please read before u post General Tech6 Rules !!! CSS Validator | Markup Validator | RSS Validator |
|
#5
| ||||
| ||||
| ASP Auto responder(Using CDONT).. Simple ASP script invoking CDONTS mail component (asp) Code:
Dim objCDONTS ' Email object
Dim strFromName ' From persons' real name
Dim strFromEmail, strToEmail ' Email addresses
Dim strSubject, strBody ' Message
strSubject = "Dear "
strFromName = "Webadministrator"
strFromEmail = "Fromemail@gmail.com"
strToEmail = "Toemail@gmail.com"
strBody = "Thank you for... "& vbcrlf & vbcrlf &"Body of email goes here..bla..bla..bla"& vbcrlf & vbcrlf &"With regards"
Set objCDONTS = Server.CreateObject("CDONTS.NewMail")
objCDONTS.From = strFromName & " <" & strFromEmail & ">"
objCDONTS.To = strToEmail
objCDONTS.Subject = strSubject
objCDONTS.Body = "--" & vbcrlf & vbcrlf & strbody & vbcrlf & vbcrlf & vbcrlf & "--"
objCDONTS.Send
Set objCDONTS = Nothing
__________________ " There are no failures - just experiences and your reactions to them. New members Please read before u post General Tech6 Rules !!! CSS Validator | Markup Validator | RSS Validator Last edited by Spec_tray; 17-03-2008 at 01:31 PM. |
|
#6
| ||||
| ||||
| Hey Spectray, how does the above asp code acts as a auto responder to an email ?? coz i find it to be a simple ASP script invoking CDONTS mail component used to send emails. As far as I know, autoresponder means a system implemented to send AUTOMATIC mail in reply to an email. I don't think the above script can send an automatic reply as strToEmail variable has a custom email add. as a value. please correct me if i'm wrong !! thankss
__________________ Would you like to Link To Us | Support TECH6 by going Premium Know more about me at Vinayaks.com | Follow TECH6 at Twitter |
|
#7
| ||||
| ||||
|
This ASP script will send mail using Persits ASPEmail component. Requirements:- ASPEmail to be installed on server Note:- You can check what components are installed on your server. Simply refer to this thread here. Code: <%
' Instantiate the COM Object
Set Mail = Server.CreateObject("Persits.MailSender")
' This is the Your IP Address
Mail.Host = "mail.yoursite.com"
Mail.Username = "you@yoursite.com"
Mail.Password = "your_password"
' This is the Hosts Station Internal SMTP Port
Mail.Port = "25"
' This is the FROM email ADDRESS
Mail.From = "you@yoursite.com"
' This is the FROM email NAME
Mail.FromName = "Yoursite.com"
' This is who the email will be sent to
Mail.AddAddress "mailme@yoursite.com"
' This is the SUBJECT of the email
Mail.Subject = "Testing Email"
' This is the BODY of the email
Mail.Body = "Thanks for registering with Yoursite.com................"
'Mail.AddAttachment "c:\dir\receipt.doc
' This is Error Checking and the "Mail.Send"
' is the actual action of sending the email
On Error Resume Next
Mail.Send
If Err <> 0 Then
Response.Write "An error occurred: " & Err.Description
End If
%>
__________________ Would you like to Link To Us | Support TECH6 by going Premium Know more about me at Vinayaks.com | Follow TECH6 at Twitter |
|
#8
| ||||
| ||||
| Find string position using InStr Code: DIM strpos Dim strexmp strexmp = "Few cool vb scripts for you." strpos = InStr(1, strexmp, "scripts", 1) Response.Write strpos Code: Dim Array_word Dim N_Str N_Str = "tech6 forums" Array_word = Split(N_Str, " ") Code: Function BreakString(strInput, intBreakPoint)intStart = 1 intEnd = intBreakPoint intStringLength = Len(strInput) strOutput = "" If intStringLength > intBreakPoint Then Do While intStart < intStringLength if(intStart + intBreakPoint > intStringLength) Then strOutput = strOutput & Mid(strInput, intStart) else strOutput = strOutput & Mid(strInput, intStart, intEnd) & "<BR />" End If intStart = intStart + intBreakPoint Loop End If BreakString = strOutputEnd Function
__________________ " There are no failures - just experiences and your reactions to them. New members Please read before u post General Tech6 Rules !!! CSS Validator | Markup Validator | RSS Validator |
|
#9
| ||||
| ||||
As Said by admin regards spec_tray
__________________ " There are no failures - just experiences and your reactions to them. New members Please read before u post General Tech6 Rules !!! CSS Validator | Markup Validator | RSS Validator |
|
#10
| ||||
| ||||
| Code: Function GetWordCount(strInput)
Dim str_word
str_word = Replace(strInput, vbTab, " ")
str_word = Replace(str_word, vbCr, " ")
str_word = Replace(str_word, vbLf, " ")
str_word = Trim(str_word)
Do While InStr(1, str_word, " ", 1) <> 0
str_word = Replace(str_word, " ", " ")
Loop
GetWordCount = UBound(Split(str_word, " ", -1, 1)) + 1
End Function
Function GetCharCount(strInput)
GetCharCount = Len(strInput)
End Function
![]() Spec _tray
__________________ " There are no failures - just experiences and your reactions to them. New members Please read before u post General Tech6 Rules !!! CSS Validator | Markup Validator | RSS Validator |
|
#11
| ||||
| ||||
| If you want to display your copyright information, instead of trying to remember to change your HTML, display it as a dynamic include file. This is in class ASP. The following will display the copyright symbol Company Name, the year you first copyrighted through to the current year. So now, when the server year updates, so does the copyright script. Code: © Copyright Your Company Name 1994 - <% with response curr_year = Year(now) .write(curr_year) end with %>
__________________ Would you like to Link To Us | Support TECH6 by going Premium Know more about me at Vinayaks.com | Follow TECH6 at Twitter |
|
#12
| ||||
| ||||
| A simple routine that allows you to quickly set up a numerical based dropdown box via a quick call to a sub routine. Code: <%
Sub BuildDropdownNumericalList(ddName, ddStart, ddEnd, ddIncrement)
Dim output, i
For i = ddStart To ddEnd Step ddIncrement
output = output & "<option value=""" & i & """>" & i & "</option>"
Next
output = "<select name=""" & ddName & """>" & output & "</select>"
Response.Write output
End Sub
%>
<%
'DISPLAY THE DROPDOWN BOX
Call BuildDropdownNumericalList("testdd", 1975, 2008, 1)
%>
__________________ Would you like to Link To Us | Support TECH6 by going Premium Know more about me at Vinayaks.com | Follow TECH6 at Twitter |
|
#13
| ||||
| ||||
| Ever wondered how those "This page was processed in 3 seconds" messages were generated? Here's the code ... Code: ' At the top of the page put...
<%
Dim strStartTime
Dim strEndTime
strStartTime = Timer 'Starts timer
%>
' and then this at the bottom (same page)...
<%
' Start our End timer
strEndTime = Timer
Response.Write ("Page Processed in: ")
Response.Write FormatNumber(strEndTime - strStartTime, 4)
Response.Write (" Seconds.")
%>
__________________ Would you like to Link To Us | Support TECH6 by going Premium Know more about me at Vinayaks.com | Follow TECH6 at Twitter |
|
#14
| ||||
| ||||
| Sometimes, when you are returning search items you may only want to show a part of the search result followed by a series of dots to indicate that there is more to see if the visitor follows a link. The Snippet below limits the display to the first 20 Characters, but you can change the 20 to any value you like... Code: <%
Dim CutShort
CutShort = rsYourRecordset.Fields.Item("YourField").Value
Response.Write LEFT (CutShort, 20) & "........"
%>
__________________ Would you like to Link To Us | Support TECH6 by going Premium Know more about me at Vinayaks.com | Follow TECH6 at Twitter |
|
#15
| ||||
| ||||
| This lets you display a default picture (staticPic.gif) if there is no picture held in the database field that you call your dynamic images from. "PicShow" is your variable. If you use this routine more than once on a page make sure that you give all the variables different names or you will get a "Variable redefined" error. Code: <%Dim PicShow
PicShow = rsShowHide.Fields.Item("shMainPix").Value
IF PicShow <>"" THEN%>
<img src="<%=rsShowHide.Fields.Item("shMainPix").Value%>">
<%ELSE%>
<img src="StaticPic.gif">
<%End If%>
__________________ Would you like to Link To Us | Support TECH6 by going Premium Know more about me at Vinayaks.com | Follow TECH6 at Twitter |
|
#16
| ||||
| ||||
| This bit of code displays the message "THIS DATA FIELD IS EMPTY" if the datafield ("YourDataField") is empty. Code: <%
Dim strShowHide
strShowHide = rsYourRecordset.Fields.Item("YourDataField").Value
IF strShowHide <>"" THEN%>
THIS DATA FIELD IS EMPTY
<%END IF%>
__________________ Would you like to Link To Us | Support TECH6 by going Premium Know more about me at Vinayaks.com | Follow TECH6 at Twitter |
|
#17
| ||||
| ||||
|
Hai Good job ...keep it going...
__________________ " There are no failures - just experiences and your reactions to them. New members Please read before u post General Tech6 Rules !!! CSS Validator | Markup Validator | RSS Validator |
|
#18
| ||||
| ||||
| |
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
| |