adding a XML control to a web custom control

B

Brian Henry

I want to create a web custom control (not a user web control) that I can
add a XML translation control into, how would I do this? I am using the web
custom control as a template that I can add to the page, but it depends on
other Web Form controls inside it but I do not know how to add the other web
form controls such as a XML web form control into this programatticly
created control. thanks
 
D

Derek Harmon

Brian Henry said:
I want to create a web custom control (not a user web control) that I can
add a XML translation control into, how would I do this?

So you've created this WebCustomControl1 type from the default web
custom control project, right? Now you want to add an Xml web control
into it (or perhaps several Xml web controls into it). OK, two steps.

In your control's OnInit override (or this can also be done externally
from the Page_Init event handler) you need to create an instance of
the Xml web control and add it to the child Controls collection of the
WebCustomControl1 class.

e.g.,

protected override void OnInit( EventArgs e)
{
// Always call base class's OnInit( ) in case it does anything.
base.OnInit( e);

// Create an Xml control.
System.Web.UI.WebControls.Xml xmlControl1 = new Xml( );

// Set some properties on it; in this case my XML is really xHTML so
// it'll show up in the browser w/o too much hassle.
xmlControl1.ID = "XmlControl1";
xmlControl1.DocumentContent = "<span style='color:red;'>text</span>";
xmlControl1.TransformSource = "./IdentityTransform.xsl";

// Add the Xml control as one of this control's children.
this.Controls.Add( xmlControl1);
}

Next step is that when the ASP.NET Framework tells WebCustomControl1
to render itself, WebCustomControl1 needs to tell any child controls that they
should render themselves.

protected override void Render(HtmlTextWriter output)
{
// This line is generated automatically by the Code Wiz.
output.Write( Text);

// Render all child controls to this HtmlTextWriter.
this.RenderChildren( output);
}

Now you should be set to go. The technique is known as "composite controls,"
if you have further questions about them.


Derek Harmon
 
B

Brian Henry

thanks!

Derek Harmon said:
So you've created this WebCustomControl1 type from the default web
custom control project, right? Now you want to add an Xml web control
into it (or perhaps several Xml web controls into it). OK, two steps.

In your control's OnInit override (or this can also be done externally
from the Page_Init event handler) you need to create an instance of
the Xml web control and add it to the child Controls collection of the
WebCustomControl1 class.

e.g.,

protected override void OnInit( EventArgs e)
{
// Always call base class's OnInit( ) in case it does anything.
base.OnInit( e);

// Create an Xml control.
System.Web.UI.WebControls.Xml xmlControl1 = new Xml( );

// Set some properties on it; in this case my XML is really xHTML
so
// it'll show up in the browser w/o too much hassle.
xmlControl1.ID = "XmlControl1";
xmlControl1.DocumentContent = "<span
style='color:red;'>text</span>";
xmlControl1.TransformSource = "./IdentityTransform.xsl";

// Add the Xml control as one of this control's children.
this.Controls.Add( xmlControl1);
}

Next step is that when the ASP.NET Framework tells WebCustomControl1
to render itself, WebCustomControl1 needs to tell any child controls that
they
should render themselves.

protected override void Render(HtmlTextWriter output)
{
// This line is generated automatically by the Code Wiz.
output.Write( Text);

// Render all child controls to this HtmlTextWriter.
this.RenderChildren( output);
}

Now you should be set to go. The technique is known as "composite
controls,"
if you have further questions about them.


Derek Harmon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top