kwilliams Posted October 13, 2006 Share Posted October 13, 2006 I found a great article titled "Creating Dynamic ASP.NET Server Controls Using XML" at http://www.dnzone.com/showDetail.asp?TypeI...m&offset=10.But all of the code was written in C#. I've tried using the C# to VB.NET conversion tool at http://www.kamalpatel.net/ConvertCSharp2VB.aspx, but I'm still running into errors. This is the latest error:BC30205: End of statement expected.Line 19: Line 20: Public Class WebForm1Line 21: Inherits System.Web.UI.Page Public class WebForm1 : System.Web.UI.PageLine 22: Line 23: Protected Title As System.Web.UI.WebControls.TextBoxAnd since I don't know C#, and I'm a newbie to VB.NET, I'm not sure how to proceed. If anyone can give me some help and/or some converted code, that would be great. Thanks for any help.Original C# Code: using System;using System.Collections;using System.ComponentModel;using System.Data;using System.Drawing;using System.Web;using System.Web.SessionState;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.HtmlControls;// add references for XSLTusing System.Xml;using System.Xml.Xsl;using System.Xml.XPath;// add references for Streamsusing System.IO;namespace CustomSurveys{ /// <summary> /// This is a custom survey page, where the questions /// are created at runtime /// </summary> public class WebForm1 : System.Web.UI.Page { protected System.Web.UI.WebControls.TextBox Title; protected System.Web.UI.WebControls.PlaceHolder survey; protected System.Web.UI.WebControls.Literal ThankYouLabel; private void Page_Load(object sender, System.EventArgs e) { if (IsPostBack) { ProcessSurveyResults(); survey.Visible = false; ThankYouLabel.Visible = true; } else { survey.Visible = true; ThankYouLabel.Visible = false; } } private void CreateSurvey() { // Load the data source XPathDocument surveyDoc = new XPathDocument(Server.MapPath("ExSurvey.xml")); // Load the xslt to do the transformations XslTransform transform = new XslTransform(); transform.Load(Server.MapPath("MakeControls.xslt")); // Get the transformed result StringWriter sw = new StringWriter(); transform.Transform(surveyDoc, null, sw); string result = sw.ToString(); // remove the namespace attribute result = result.Replace("xmlns:asp=\"remove\"", ""); // parse the control(s) and add it to the page Control ctrl = Page.ParseControl(result); survey.Controls.Add(ctrl); } private void ProcessSurveyResults() { // Load the data source XPathDocument surveyDoc = new XPathDocument(Server.MapPath("ExSurvey.xml")); // create an iterator XPathNodeIterator itr = surveyDoc.CreateNavigator().Select("//question"); // string builder for survey body System.Text.StringBuilder sb; sb = new System.Text.StringBuilder(); // submission information sb.Append("Survey submitted on " + DateTime.Now + Environment.NewLine); // foreach question while (itr.MoveNext()) { // get the control name string controlName = itr.Current.GetAttribute("name", ""); // append question information sb.Append(controlName); sb.Append(" : "); // get the control object ctrl = FindControl(controlName); // append the correct filled out information if (ctrl is TextBox) { sb.Append(((TextBox)ctrl).Text); } if (ctrl is RadioButtonList) { // the selected item might be null if (((RadioButtonList)ctrl).SelectedItem != null) { sb.Append(((RadioButtonList)ctrl).SelectedItem.Value); } } sb.Append(Environment.NewLine); } string body = sb.ToString(); // send the results System.Web.Mail.SmtpMail.SmtpServer = "your.smtp.server"; System.Web.Mail.SmtpMail.Send("survey@somewhere.com", "your@address.com", "Survey result", body); } #region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN: This call is required by the ASP.NET Web Form Designer. // InitializeComponent(); CreateSurvey(); base.OnInit(e); } /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.Load += new System.EventHandler(this.Page_Load); } #endregion }} Converted VB.NET Code: Imports SystemImports System.CollectionsImports System.ComponentModelImports System.DataImports System.DrawingImports System.WebImports System.Web.SessionStateImports System.Web.UIImports System.Web.UI.WebControlsImports System.Web.UI.HtmlControls' add references for XSLTImports System.XmlImports System.Xml.XslImports System.Xml.XPath ' add references for StreamsImports System.IO Public Class WebForm1 Inherits System.Web.UI.Page Public class WebForm1 : 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 '***THIS SECTION BELOW WOULD NOT CONVERT*** private void Page_Load(Object sender, System.EventArgs e) { if (IsPostBack) { ProcessSurveyResults(); survey.Visible = false; ThankYouLabel.Visible = true; } else { survey.Visible = true; ThankYouLabel.Visible = false; } } '***THIS SECTION ABOVE WOULD NOT CONVERT*** 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 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) End While Dim body As String = sb.ToString() ' send the results System.Web.Mail.SmtpMail.SmtpServer = "your.smtp.server" System.Web.Mail.SmtpMail.Send("survey@somewhere.com", "your@address.com", "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 Private Sub InitializeComponent() Me.Load += New System.EventHandler(Me.Page_Load) End Sub #End Region End Class Link to comment Share on other sites More sharing options...
aspnetguy Posted October 13, 2006 Share Posted October 13, 2006 try this 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 Imports System.Xml Imports System.Xml.Xsl Imports System.Xml.XPath Imports System.IO Namespace CustomSurveys 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) If IsPostBack Then ProcessSurveyResults survey.Visible = False ThankYouLabel.Visible = True Else survey.Visible = True ThankYouLabel.Visible = False End If End Sub Private Sub CreateSurvey() Dim surveyDoc As XPathDocument = New XPathDocument(Server.MapPath("ExSurvey.xml")) Dim transform As XslTransform = New XslTransform transform.Load(Server.MapPath("MakeControls.xslt")) Dim sw As StringWriter = New StringWriter transform.Transform(surveyDoc, Nothing, sw) Dim result As String = sw.ToString result = result.Replace("xmlns:asp=""remove""", "") Dim ctrl As Control = Page.ParseControl(result) survey.Controls.Add(ctrl) End Sub Private Sub ProcessSurveyResults() Dim surveyDoc As XPathDocument = New XPathDocument(Server.MapPath("ExSurvey.xml")) Dim itr As XPathNodeIterator = surveyDoc.CreateNavigator.Select("//question") Dim sb As System.Text.StringBuilder sb = New System.Text.StringBuilder sb.Append("Survey submitted on " + DateTime.Now + Environment.NewLine) While itr.MoveNext Dim controlName As String = itr.Current.GetAttribute("name", "") sb.Append(controlName) sb.Append(" : ") Dim ctrl As Object = FindControl(controlName) If TypeOf ctrl Is TextBox Then sb.Append(CType(ctrl, TextBox).Text) End If If TypeOf ctrl Is RadioButtonList Then If Not (CType(ctrl, RadioButtonList).SelectedItem Is Nothing) Then sb.Append(CType(ctrl, RadioButtonList).SelectedItem.Value) End If End If sb.Append(Environment.NewLine) End While Dim body As String = sb.ToString System.Web.Mail.SmtpMail.SmtpServer = "your.smtp.server" System.Web.Mail.SmtpMail.Send("survey@somewhere.com", "your@address.com", "Survey result", body) End Sub Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs) InitializeComponent CreateSurvey MyBase.OnInit(e) End Sub Private Sub InitializeComponent() AddHandler Me.Load, AddressOf Me.Page_Load End Sub End Class End Namespace I used this website http://www.developerfusion.co.uk/utilities...csharptovb.aspx Link to comment Share on other sites More sharing options...
kwilliams Posted October 13, 2006 Author Share Posted October 13, 2006 Wow aspnetguy. Thanks for doing that. I'll definitely bookmark that site, and I'll add an ending post once the pages are complete. I appreciate your help, and I hope that you have a great weekend. Link to comment Share on other sites More sharing options...
aspnetguy Posted October 14, 2006 Share Posted October 14, 2006 you too...I plan on having a good weekend Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now