Fórum Duvidas com OO .NET #398693
04/04/2011
0
Na minha view tenho dois botoes, um que faz com que o Banco de 10 reais pro Joe e um que o banco receba 5 conto do Bob. Mas no meu metodo pageload ele cria o objeto denovo quando clicko no botão sendo assim resetando os valores para os valores do estado normal, como faço pra nao fazer isso, tentei o Page.isPostBack mas nao se se usei da forma correta.
1. Defaut.aspx.cs
1 2 3 4 5 | using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { Guy joe; Guy bob; int banco = 100; protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { joe = new Guy(); bob = new Guy(); joe.Nome = "Joe"; bob.Nome = "Bob"; joe.dinheiroCaixa = 50; bob.dinheiroCaixa = 50; UpdateSite(); } } public void UpdateSite() { joeLabel.Text = joe.Nome + " tem $" + joe.dinheiroCaixa; bobLabel.Text = bob.Nome + " tem $" + bob.dinheiroCaixa; bancoLabel.Text = "O banco tem " + banco; } protected void btnBobDar_Click(object sender, EventArgs e) { banco += bob.DarDinheiro(5); UpdateSite(); } protected void btnJoeRecebe_Click(object sender, EventArgs e) { if (banco >= 10) { banco -= joe.ReceberDinheiro(10); UpdateSite(); } else { System.Web.HttpContext.Current.Response.Write("< script language = 'javascript' >"); System.Web.HttpContext.Current.Response.Write("alert('O Banco esta sem grana')"); System.Web.HttpContext.Current.Response.Write("</ script >"); } }} |
2. Default.aspx
1 2 3 4 | <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> </ head >< body > < form id = "form1" runat = "server" > < div > < asp:label id = "joeLabel" runat = "server" >Joe:</ asp:label >< br /> < asp:label id = "bobLabel" runat = "server" >Bob:</ asp:label >< br /> < asp:label id = "bancoLabel" runat = "server" >Banco:</ asp:label >< br /> < asp:Button runat = "server" id = "btnJoeRecebe" Text = "Dar R$ 10 Pro Joe" onclick = "btnJoeRecebe_Click" /> < asp:Button runat = "server" id = "btnBobDar" Text = "Recebe $5 de bob" onclick = "btnBobDar_Click" /> </ div > </ form ></ body ></ html > |
me ajudem

Renato Castro
Curtir tópico
+ 0Posts
04/04/2011
Netasper
Gostei + 0
04/04/2011
Renato Castro
Gostei + 0
06/04/2011
Alexandre Santos
Para maiores informações:
http://msdn.microsoft.com/pt-br/library/98f28cdx%28v=vs.90%29.aspx
Att,
Alexandre Luiz dos Santos
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)