Thursday, March 24, 2005

Programmatically Load User Controls

Programmatically Load User Controls

 

 This sample consists of 3 pages:

  • default.aspx -- the standard ASP.NET page
  • cbDefault.vb -- the codebehind page for default.aspx
  • ucHeading.ascx -- a usercontrol with a property called page heading.

default.aspx simply has a panel object as a placeholder, and references the code behind file.

default.aspx

<%@ Page EnableSessionState="false" explicit="true" strict="true" MaintainState="false" inherits="cbDefault" src="cbDefault.vb"%>
<html>
<head>
<title></title>
</head>
<body bgcolor="#FFFFFF" >
<asp:panel id="pnlHeading" runat="server" />
</body>
</html>

 

 

ucHeading has a single property called PageHeading. PageHeading is a string that is used to display the heading of the page for the web surfer.

ucHeading.ascx

<script runat="server">
Private _pageheading as String = ""
Public Property PageHeading As String
Get
    Return _pageheading
End Get
Set
    _pageheading = value
End Set
End Property
</script>
<h1><%=_pageheading%></h1>

 

cdDefault.vb is the code behind file for default.aspx. It contains the meat of this demo. The logic of cbDefault.vb is:

  • Load the UserControl
  • Get the type of UserControl
  • Get access to the property "PageHeader"
  • Set the property "PageHeader"
  • Add the UserControl to the panel object on default.aspx

cbDefault.vb

Option Strict On
Option Explicit On

Imports System
Imports System.Reflection
Imports System.Web.UI
Imports System.Web.UI.WebControls

Public Class cbDefault : Inherits Page
Public pnlHeading as Panel

Public Sub Page_Load(Src As Object, E As EventArgs)
  ' Load the control
  Dim myUC as UserControl = LoadControl("ucHeading.ascx")

  ' Set the Usercontrol Type
  Dim ucType as Type = myUC.GetType()

  ' Get access to the property
  Dim ucPageHeadingProperty as PropertyInfo = ucType.GetProperty("PageHeading")

  ' Set the property
  ucPageHeadingProperty.SetValue(myUC,"Access a Usercontrol from Code Behind",Nothing)

  pnlHeading.Controls.Add ( myUC )
End Sub

End Class

 

 

 



Have a Nice Day
 
Ritesh Kumar Kesharwani
Software Professional
Cell  :  011-91-9845657636

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

Monday, March 21, 2005

How To Read/Write Into XML

Private sub WriteXML()

Try

Dim doc As

XmlDocument

doc = New XmlDocument()

'Chk whether xml files is exists or not

Try

doc.Load(Server.MapPath("book.xml"))

Catch ex As

Exception

doc.LoadXml("<Books></Books>")

End

Try

Dim node As XmlElement, pNode As

XmlElement

pNode = doc.CreateElement("Book")

doc.FirstChild.AppendChild(pNode)

node = doc.CreateElement("Title")

node.InnerText = txtTitle.Text

pNode.AppendChild(node)

node = doc.CreateElement("Author")

node.InnerText = txtAuthor.Text

pNode.AppendChild(node)

node = doc.CreateElement("Year")

node.InnerText = txtYear.Text

pNode.AppendChild(node)

'save the output to a file

doc.Save(Server.MapPath("book.xml"))

ReadXml()

Catch Eh As

Exception

Response.Write("Exception: {0}" & Eh.ToString())

End

Try

End

Sub

Private Sub

ReadXml()

Try

Dim myDataSet As New

DataSet()

myDataSet.ReadXml(Server.MapPath("book.xml"))

dgBooksPretty.DataSource = myDataSet

dgBooksPretty.DataBind()

Catch ex As

Exception

Response.Write("Exception: {0}" & ex.ToString())

End

Try

Note: some more way of Reading / Writting XML using C#.net

http://riteshk.blogspot.com/2007/07/how-to-readwrite-xml-string-in.html