Total Pageviews

Monday, May 03, 2010

ASP.NET dynamically generating javascript from a database for a div tag in pageload , vb code

ASP.NET dynamically generating javascript from a database in pageload , vb code

Here is an example to dynamically generate a javascript in pageload event for ur asp.net web application.

i have a div tag in mt default.aspx like this in body. remember this tag has already two .js files defined in head element. so my code may not work for you.. so write your own jscript.

<div id="fadeshow1" runat="server"></div>

i want to assign jscript for bday wishes of employees for that div tag.
in page load i call a function loadwishes to create bday wish by opening the database in getwishemp function

do this in page load event


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

If Not Page.IsPostBack Then

'this will be executed only first time any event happen on web page

ClientScript.RegisterClientScriptBlock(Me.GetType(), "vinscript", loadwishes())

End If

End Sub

Private Function loadwishes() As String

Dim wishscript As String = "" & _

" "

'" ['database/pics/p009383.gif', '', '', 'Sr. Engineer']," & _

' " ['database/pics/p009633.gif', '', '', 'Engineer']," & _

' " ['database/pics/p085525.gif', '', '', 'A Engineer']" & _

'getwishemp() & _

Return (wishscript)

End Function

Private Function getwishemp() As String

connection.Close()

connection.Open()

Dim sqlComm As SqlCommand

Dim sqlReader As SqlDataReader

Dim dt As New DataTable()

Dim i As Integer

Dim emparray As String = ""

Try

sqlComm = New SqlCommand("select eid, fname, lname, dob, designation from empdetail WHERE (MONTH(dob) = MONTH(GETDATE())) ORDER BY DAY(dob) ", connection) ' fname + ' ' + lname AS name

sqlReader = sqlComm.ExecuteReader()

dt.Load(sqlReader)

i = dt.Rows.Count

''Strings.Format(Session("name"), "capetalize")

If i > 0 Then

'capetalize the name.

Dim picpath As String

Dim j As Integer = 0

While j <>

picpath = "database/pics/p" + Trim(dt.Rows(j).Item("eid")) + ".gif"

If FileIO.FileSystem.FileExists(Server.MapPath("~/database/pics/") + "p" + Trim(dt.Rows(j).Item("eid")) + ".gif") Then

picpath = "database/pics/p" + Trim(dt.Rows(j).Item("eid")) + ".gif"

Else

picpath = "database/pics/default.gif"

End If

emparray = emparray + " ['" + picpath + "', 'departments.aspx?showemp=" + Trim(dt.Rows(j).Item("eid")) + "', '', '" + Str(Day(dt.Rows(j).Item("dob"))) + " " + MonthName(Month(dt.Rows(j).Item("dob"))) + "-" + UCase(Left(dt.Rows(j).Item("fname"), 1)) + ". " + UCase(Left(dt.Rows(j).Item("lname"), 1)) + LCase(Mid(dt.Rows(j).Item("lname"), 2)) + "']"

j = j + 1

If j < style="color:blue">Then

emparray = emparray + "," & _

""

End If

End While

connection.Close()

getwishemp = emparray

Else

connection.Close()

getwishemp = "Database not open " + i

End If

sqlReader.Close()

Catch e As Exception

'lblDebug.text = e.Message

connection.Close()

getwishemp = "Database not open " + e.Message

End Try

connection.Close()

End Function


- Vinod K

No comments: