How to call a class method in webforms code-Behind?

Asked

Viewed 260 times

1

It is possible to call a class method in the webforms code-Behind?

Default.aspx:

<div class="row" style="margin: 50px">
    <div class="col-md-6">
        <asp:TextBox ID="txtElemento" runat="server" CssClass="form-control" placeholder="Inserir Elemento"></asp:TextBox>
    </div>
    <div class="col-md-6">
        <asp:Button ID="btnInserir" runat="server" CssClass="btn btn-primary form-control" Text="Inserir" OnClick="btnInserir_Click" />
    </div>
</div>

Default.aspx.Cs is working like this:

static List<Pilha> listaElementos = new List<Pilha>();
protected void btnInserir_Click(object sender, EventArgs e)
{
    Pilha pilha = new Pilha();
    pilha.Elemento = txtElemento.Text;
    listaElementos.Add(pilha);
    Session["ELEMENTOS"] = listaElementos;
}

Only I want to call the class method, so I tried to do it this way:

protected void btnInserir_Click(object sender, EventArgs e)
{
    Pilha pilha = new Pilha();        
    pilha.Inserir(txtElemento.Text);        
}

Class:

public class Pilha
{
    public string Elemento { get; set; }
    static List<Pilha> listaElementos = new List<Pilha>();

    public void Inserir(string elemento)
    {
        listaElementos.Add(elemento);
        Session["ELEMENTOS"] = listaElementos;
    }
}
  • You want to call the method from the listaElementos ? listaElementos[0].Inserir(txtElemento.Text);?

  • @Augustovasques It is a list that receives several elements

  • I know it’s a list. I just can’t understand your question. What exactly do you want?

  • @Augustovasques I wanted to do in the class what I can do in Default.aspx.Cs, I wanted Default.aspx.Cs to just call the method within the class, it is possible to do this?

  • Yes you can and you have two options. One is to replicate the code within the class and another is to call the event btnInserir_Click(this, EventArgs.Empty);.

  • Okay Rodrigo, I hadn’t noticed that your list was static in class.

  • The Stack class is the Stack List element so it is not the correct one to do, the correct one is to add the Stack element. You can save to the session the stack list and the session recover and add, remove or edit items from your List. Is that not what you want.?

  • @Virgilionovic Yes that’s what I’d like to do, but I’m not sure how to do it.

Show 3 more comments

1 answer

1


I propose a solution:

Pilha Class:

public class Pilha
{
    public Pilha(string elemento)
    {
        Elemento = elemento;
    }
    public string Elemento { get; }
}

Page code:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        if (Session["ELEMENTOS"] == null)
        {
            //inicia a lista de pilha 
            Session.Add("ELEMENTOS", new List<Pilha>());
        }
        //carrega a grid com os valores
        Load_GridView();
    }
}
protected void Load_GridView()
{
    if (Session["ELEMENTOS"] != null && ((List<Pilha>)Session["ELEMENTOS"]).Count > 0)
    {
        GridViewItemsPilha.DataSource = ((List<Pilha>)Session["ELEMENTOS"]).ToList();
        GridViewItemsPilha.DataBind();
    }
}
protected void BtnInserir_Click(object sender, EventArgs e)
{
    if (Session["ELEMENTOS"] != null)
    {
        ((List<Pilha>)Session["ELEMENTOS"])?.Add(new Pilha(TxtElemento.Text));
        Load_GridView();
    }
}

Code aspx:

<form id="form1" runat="server">
<div>
    <div class="row" style="margin: 50px">
        <div class="col-md-6">
            <asp:TextBox ID="TxtElemento" runat="server" CssClass="form-control" placeholder="Inserir Elemento"></asp:TextBox>
        </div>
        <div class="col-md-6">
            <asp:Button ID="BtnInserir" runat="server" CssClass="btn btn-primary form-control" Text="Inserir" OnClick="BtnInserir_Click" />
        </div>
    </div>
</div>
<div>
    <asp:GridView ID="GridViewItemsPilha" runat="server"></asp:GridView>
</div>
</form>

basically this retrieves your session list and add elements in the list and the data is loaded into a GridView, example:

inserir a descrição da imagem aqui

  • 1

    That’s what I was looking for, thanks for your help.

Browser other questions tagged

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