Creation of dynamic controls in ASP.Net

Asked

Viewed 502 times

4

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

Additional information: Undefined object reference for an object instance.

Code of my group register:

    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 Padrao class:

    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 is rotated:

pagina.pnBotao.Controls.Add(btnPesquisar);

  • I don’t think your button has Viewstate. With this, the page loses the button every post - the button you every page load is not the button that was there before. Give a Viewstate to the button you create and see if it resolves.

  • It seems the property pnBotao is null, you can show the class code CadastroPadrao?

  • This guy I work with, I create on the page. aspx:&#xA;&#xA;<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="CadastroPadrao.aspx.cs" Inherits="TulipaCB.Cadastro.CadastroPadrao" %>&#xA;<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server"> <div> <H2> <Asp:Literal ID="ltTitulo" runat="server"></Asp:Literal> </H2> </div> <div> <Asp:Panel ID ="pnBotao" runat="server"> </Asp:Panel> </div<#Xa; <#Xa; <#Xa; <Asp:Panel ID ="pnGrid" runat="server"> </Asp:Panel> </div> </Asp:Content>

1 answer

1


Browser other questions tagged

You are not signed in. Login or sign up in order to post.