Thursday, January 3, 2008

Make HTML controls discoverable in Volta Control

 

When a Volta control is rendered, the ID attribute of the generated HTML is changed to something like _vcId_1_DivName which is inconvenient to find from code. But the ID attribute stays the same in case of Volta Page, so it is discoverable by ID like this:

Div divContent = Document.GetById<Div>("divContent");


However, if you add HTML controls to the control like the following, the ID is not changed during the rendering:



public VoltaControl1() : base("VoltaControl1.html")
{
InitializeComponent();

Button btnClick = new Button();
btnClick.InnerText = "Click!";
btnClick.Id = "btnClick";
this.Add(btnClick);
}


If you don't prefer this way and seriously want to write your own HTML in the control's html page, you might find the following snippet useful. But, remember in this case you will use name attribute of the html element instead of ID.



// Usage: var element = GetElementByName(Document.GetElementsByTagName("div"), "divWidget");
private HtmlElement GetElementByName(HtmlElementCollection elements, string name)
{
foreach (var element in elements)
{
DomAttribute nameAttribute = element.Attributes.GetNamedItem("name");
if (nameAttribute != null)
if (nameAttribute.Value == name)
return element;
}

return null;
}

No comments: