Programmatically place a control inside a div

I want to put my own tree in a specific one <div>on my aspx page, programmatically in C #. Ideas?

+3
source share
2 answers

Or:

Use the runat = "Server" directive on the div

OR

use Asp: Panel (which displays as a div) <- That would be my preference.

and then

dynamically add a control using a standard method .

with line

myDiv.Controls.Add(myTreeview);
+6
source

I assume that most of your asp.net so far have been on the declarative side of things?

First of all you need a div that ASP.net can see, I would use asp: Panel for this.

TreeView .

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        TreeView treeView = new TreeView();

        // Do whatever you need to fill out your TreeView

        Panel1.Controls.Add(treeView);
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Test</title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:Panel ID="Panel1" runat="server">
    </asp:Panel>
    </form>
</body>
</html>

, , , .FindControl( "Panel1" ) , .

0

Source: https://habr.com/ru/post/1758201/


All Articles