Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Custom Webcontrol with Nodes

Reply
Thread Tools

Custom Webcontrol with Nodes

 
 
STech
Guest
Posts: n/a
 
      01-24-2005
Hi,

I would like to create a webcontrol where data for the control is contained
in the control itself. Example:

<foo:MyCustomControl id="Test0001">
<Data>
<item>
<itemcaption>Caption1</itemcaption>
<itembody>CaptionBody1</itembody>
</item>
<item>
<itemcaption>Caption2</itemcaption>
<itembody>CaptionBody2</itembody>
</item>
<item>
<itemcaption>Caption3</itemcaption>
<itembody>CaptionBody3</itembody>
</item>
</Data>
</foo:MyCustomControl>

The webcontrol must able to read this data nested within itself and render a
customized <table> with multiple rows. My problem is I do not know where to
start. How do I read the data in the "Render" method?

Thanks.
 
Reply With Quote
 
 
 
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      01-25-2005
Hi Stech,

Thanks for your posting. As for the problem you mentioned, I think we need
to create a custom ControlBuilder for our custom control in such scenario.
Which is used to help parse the content inside our contro body.

Here is a very good codeproject article discussing this:

http://codeproject.com/aspnet/MikEll...ery.asp?df=100

In addition, here is a simple example control which parse the nested xml
data and load them as xmldocmente and retrieve the child nodes inside it :

===============================
namespace MyWebApp.customcontrols
{

public class XMLControlBuilder : ControlBuilder
{
public override Type GetChildControlType(String tagName,
IDictionary attributes)
{
if (String.Compare(tagName, "XMLDATA", true) == 0)
return typeof(XMLData);

return null;
}

}


[ControlBuilder(typeof(XMLControlBuilder))]
public class XMLControl : Control
{
private String header;
private XmlDocument xmldoc = null;

public String Header
{
get
{
return header;
}
set
{
header = value;
}
}



public XmlDocument XMLDoc
{
get
{
return xmldoc;
}
set
{
xmldoc = value;
}
}

protected override void AddParsedSubObject(Object obj)
{

if (obj is XMLData)
{
XMLData data = obj as XMLData;

if(data != null)
{
xmldoc = new XmlDocument();
xmldoc.LoadXml(data.Text);


}
}

}


protected override void CreateChildControls()
{



Table table = new Table();

TableRow htrTitle = new TableRow();

TableHeaderCell hcTitle = new TableHeaderCell();
Label label = new Label();
label.Text = Header;
label.BackColor = System.Drawing.Color.Beige;
label.ForeColor = System.Drawing.Color.Red;
hcTitle.Controls.Add(label);
htrTitle.Cells.Add(hcTitle);




TableRow htr = new TableRow();

TableHeaderCell hcell1 = new TableHeaderCell();
hcell1.Text = "itemcaption";
htr.Cells.Add(hcell1);

TableHeaderCell hcell2 = new TableHeaderCell();
hcell2.Text = "itembody";
htr.Cells.Add(hcell2);


table.Rows.Add(htr);

table.BorderWidth = 2;
table.BackColor = System.Drawing.Color.Beige;
table.ForeColor = System.Drawing.Color.Red;


XmlNodeList nodes = xmldoc.SelectNodes("/DATA/ITEM");

foreach(XmlNode node in nodes)
{
TableRow tr = new TableRow();

XmlNode caption = node.SelectSingleNode("ITEMCAPTION");
XmlNode body = node.SelectSingleNode("ITEMBODY");


TableCell cell1 = new TableCell();
cell1.Text = caption.InnerText;
tr.Cells.Add(cell1);

TableCell cell2 = new TableCell();
cell2.Text = body.InnerText;
tr.Cells.Add(cell2);


table.Rows.Add(tr);
}
Controls.Add(table);

}
}

public class XMLData : System.Web.UI.WebControls.Literal
{}
}
================================================== ===

Then, we can use it like:


<%@ Register TagPrefix="cc" Namespace="MyWebApp.customcontrols" Assembly =
"MyWebApp" %>
..............
.................
<form id="Form1" method="post" runat="server">
<cc:XMLControl Header="Sample" id="prop" runat="server">
<XMLDATA>
<DATA>
<ITEM>
<ITEMCAPTION>Caption1</ITEMCAPTION>
<ITEMBODY>CaptionBody1</ITEMBODY>
</ITEM>
<ITEM>
<ITEMCAPTION>Caption2</ITEMCAPTION>
<ITEMBODY>CaptionBody2</ITEMBODY>
</ITEM>
<ITEM>
<ITEMCAPTION>Caption3</ITEMCAPTION>
<ITEMBODY>CaptionBody3</ITEMBODY>
</ITEM>
</DATA>
</XMLDATA>
</cc:XMLControl>
</form>



Hope helps. Thanks.


Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Custom webcontrol that references another webcontrol Fabricio ASP .Net Building Controls 0 09-21-2005 02:55 AM
Text nodes and element nodes query asd Java 3 05-23-2005 10:01 AM
Looking A Nodes From Within Nodes Johnny Ooi XML 10 11-14-2004 06:55 PM
selecting nodes between other nodes Timo Nentwig XML 1 06-17-2004 04:54 AM
Reality check: Is it sensible to link XML nodes to other XML nodes in the same file? gavnosis XML 0 08-02-2003 08:22 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57