(via Instant
VB)
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
' add references for XSLT
Imports System.Xml
Imports System.Xml.Xsl
Imports System.Xml.XPath
' add references for Streams
Imports System.IO
Namespace CustomSurveys
''' <summary>
''' This is a custom survey page, where the questions
''' are created at runtime
''' </summary>
Public Class WebForm1
Inherits System.Web.UI.Page
Protected Title As System.Web.UI.WebControls.TextBox
Protected survey As System.Web.UI.WebControls.PlaceHolder
Protected ThankYouLabel As System.Web.UI.WebControls.Literal
Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
If IsPostBack Then
ProcessSurveyResults()
survey.Visible = False
ThankYouLabel.Visible = True
Else
survey.Visible = True
ThankYouLabel.Visible = False
End If
End Sub
Private Sub CreateSurvey()
' Load the data source
Dim surveyDoc As XPathDocument = New XPathDocument(Server.MapPath("ExSurvey.xml"))
' Load the xslt to do the transformations
Dim transform As XslTransform = New XslTransform()
transform.Load(Server.MapPath("MakeControls.xslt") )
' Get the transformed result
Dim sw As StringWriter = New StringWriter()
transform.Transform(surveyDoc, Nothing, sw)
Dim result As String = sw.ToString()
' remove the namespace attribute
result = result.Replace("xmlns:asp=""remove""", "")
' parse the control(s) and add it to the page
Dim ctrl As Control = Page.ParseControl(result)
survey.Controls.Add(ctrl)
End Sub
Private Sub ProcessSurveyResults()
' Load the data source
Dim surveyDoc As XPathDocument = New XPathDocument(Server.MapPath("ExSurvey.xml"))
' create an iterator
Dim itr As XPathNodeIterator = surveyDoc.CreateNavigator().Select("//question")
' string builder for survey body
Dim sb As System.Text.StringBuilder
sb = New System.Text.StringBuilder()
' submission information
sb.Append("Survey submitted on " & DateTime.Now & Environment.NewLine)
' foreach question
Do While itr.MoveNext()
' get the control name
Dim controlName As String = itr.Current.GetAttribute("name", "")
' append question information
sb.Append(controlName)
sb.Append(" : ")
' get the control
Dim ctrl As Object = FindControl(controlName)
' append the correct filled out information
If TypeOf ctrl Is TextBox Then
sb.Append((CType(ctrl, TextBox)).Text)
End If
If TypeOf ctrl Is RadioButtonList Then
' the selected item might be null
If Not (CType(ctrl, RadioButtonList)).SelectedItem Is Nothing Then
sb.Append((CType(ctrl, RadioButtonList)).SelectedItem.Value)
End If
End If
sb.Append(Environment.NewLine)
Loop
Dim body As String = sb.ToString()
' send the results
System.Web.Mail.SmtpMail.SmtpServer = "your.smtp.server"
System.Web.Mail.SmtpMail.Send("
[email protected] m", "
[email protected]", "Survey result", body)
End Sub
#Region "Web Form Designer generated code"
Overrides Protected Sub OnInit(ByVal e As EventArgs)
'
' CODEGEN: This call is required by the ASP.NET Web Form Designer.
'
InitializeComponent()
CreateSurvey()
MyBase.OnInit(e)
End Sub
''' <summary>
''' Required method for Designer support - do not modify
''' the contents of this method with the code editor.
''' </summary>
Private Sub InitializeComponent()
' Me.Load += New System.EventHandler(Me.Page_Load);
End Sub
#End Region
End Class
End Namespace