Pergunta

i am working with String Builder and a Tree view control which i am generating Dynamically in my page.

This is what i have tried with :

Public sbMenu As New StringBuilder

In Page Load Event

    Dim TreeView1 As New TreeView()
    TreeView1.ID = "tree1"
    TreeView1.ShowCheckBoxes = TreeNodeTypes.All
    TreeView1.ShowLines = True
    TreeView1.Nodes.Clear()

I am loading the TreeView1 using Database data and atlast appending to my string builser object as

    sbMenu.Append("<div>")
    sbMenu.Append(TreeView1)
    sbMenu.Append("</div>")

But the sbMenu doesn't contain the TreeView1 instead it is storing

System.Web.UI.WebControls.TreeView

Can you please help me , By how my stringBuilder can hold the TreeView control and can make use of it..

I will use this in my .aspx page as

<%= sbMenu.Tostring() %>

here i need the TreeView control..

Foi útil?

Solução

You can get a control's HTML by calling RenderControl. E.g.

var sb = new StringBuilder();

using (var sw = new StringWriter(sb)) 
using (var writer = new HtmlTextWriter(sw))
{
     myControl.RenderControl(writer);
}

string html = sb.ToString();

Or in VB:

Dim sb = New StringBuilder()

Using sw As New StringWriter(sb)
    Using writer As New HtmlTextWriter(sw)
        myControl.RenderControl(writer)
    End Using
End Using

Dim html As String = sb.ToString()
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top