Creating Dynamic Controls in ASP.Net

3

I'm looking to create a component for a default master record. In the project I'm developing, I'm using Web Forms, where I have my masterPage with the site layout (menu, header, and footer). But to facilitate future maintenance, I created a class and inside it I created a method where this page dynamically creates my buttons, title of that register and so on. But when I'm going to call him from my customer registration page for example, an error occurs because my customer registration panel has not yet been created:

  

Additional information: Object reference not set to an instance of an object.

My group registration code:

    protected void Page_Load(object sender, EventArgs e)
    {
        /****************************
        *   Declarando os Objetos   *
        ****************************/

        CadastroPadrao cp = new CadastroPadrao();
        Grupo gp = new Grupo();

        /********************************************
        *   Metodo que cria os objetos na pagina    *
        ********************************************/

        cp.GeraCadastro("Cadastro de Grupo", gp);                           
    }

Code of my class Padrao:

    public void GeraCadastro(string titulo, Object tela) 
    {
        Button btnPesquisar = new Button();

        btnPesquisar.Text = "Pesquisar";
        btnPesquisar.ID = "btnPesquisar";
        btnPesquisar.Width = 100;

        if (Convert.ToString(tela) == "TulipaCB.Grupo")
        {
            CadastroPadrao pagina = new CadastroPadrao();

            pagina.pnBotao.Controls.Add(btnPesquisar);
        }

Error occurs at the exact moment the line runs:

pagina.pnBotao.Controls.Add(btnPesquisar);

    
asked by anonymous 11.09.2014 / 16:12

1 answer

0

I was able to solve my situation ... I used the User Control, in it I draw like I want the buttons, grid and everything. This works as a component, then ready to just drag it to the screen and you're done:)

If anyone needs to have the walkthrough here:

link

    
11.09.2014 / 20:42