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);
?– Augusto Vasques
@Augustovasques It is a list that receives several elements
– Rodrigo Santos
I know it’s a list. I just can’t understand your question. What exactly do you want?
– Augusto Vasques
@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?
– Rodrigo Santos
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);
.– Augusto Vasques
Okay Rodrigo, I hadn’t noticed that your list was static in class.
– bfavaretto
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.?
– novic
@Virgilionovic Yes that’s what I’d like to do, but I’m not sure how to do it.
– Rodrigo Santos