Asked by:
proyecto web

Question
-
public class Cliente
{
public string Rut { get; set; }
public string Nombre { get; set; }
public int Edad { get; set; }
public bool TieneTC { get; set; }
public bool TieneTD { get; set; }
public bool TieneCA { get; set; }
public Cliente()
{
this.Init();
}
private void Init()
{
this.Rut = string.Empty;
this.Nombre = string.Empty;
this.Edad = 0;
this.TieneCA = false;
this.TieneTC = false;
this.TieneTD = false;
}
}- Moved by Yong LuMicrosoft contingent staff Tuesday, September 26, 2017 5:19 AM
Monday, September 25, 2017 9:10 PM
All replies
-
public class ClienteCollection : List<Cliente>
{
public Cliente BuscarPorRut(string rut)
{
try
{
var consulta = from c in this
where c.Rut == rut
select c;
return consulta.First();
} catch(Exception ex)
{
return null;
}
}
public bool Editar(Cliente clienteNuevo)
{
bool sinErrores = false;
Cliente clienteViejo = this.BuscarPorRut(clienteNuevo.Rut);
if (clienteViejo != null)
{
this.Remove(clienteViejo);
this.Add(clienteNuevo);
sinErrores = true;
}
return sinErrores;
}
public List<Cliente> Listar()
{
var consulta = from c in this
orderby c.Rut ascending
select c;
return consulta.ToList();
}
}Monday, September 25, 2017 9:11 PM -
Agregar
protected void btnGuardar_Click(object sender, EventArgs e)
{
HabilitaFormulario(false);
Cliente clienteNuevo = new Cliente();
Cliente buscar = ((SitioBanco)this.Master).Clientes.BuscarPorRut(txtRut.Text);
if (buscar == null)
{
clienteNuevo.Rut = txtRut.Text;
clienteNuevo.Nombre = txtNombre.Text;
clienteNuevo.Edad = int.Parse(txtEdad.Text);
clienteNuevo.TieneCA = cbTieneCA.Checked;
clienteNuevo.TieneTC = cbTieneTC.Checked;
clienteNuevo.TieneTD = cbTieneTD.Checked;
((SitioBanco)this.Master).Clientes.Add(clienteNuevo);
lblMensaje.Text = "Cliente agregado con exito";
}
else
{
lblMensaje.Text = "El cliente ya existe";
HabilitaFormulario(true);
}
}
private void HabilitaFormulario(bool accion)
{
txtRut.Enabled = accion;
txtNombre.Enabled = accion;
txtEdad.Enabled = accion;
btnGuardar.Enabled = accion;
cbTieneCA.Enabled = accion;
cbTieneTC.Enabled = accion;
cbTieneTD.Enabled = accion;
}Monday, September 25, 2017 9:12 PM -
boton buscar
if(txtRutBuscar.Text.Trim().Length > 0)
{
Cliente = ((SitioBanco)this.Master).Clientes.BuscarPorRut(txtRutBuscar.Text);
LlenarFormulario(Cliente);
}Monday, September 25, 2017 9:13 PM -
private void LlenarFormulario(Cliente cliente)
{
if (cliente != null)
{
txtRut.Text = cliente.Rut;
txtEdad.Text = cliente.Edad.ToString();
txtNombre.Text = cliente.Nombre;
cbTieneCA.Checked = cliente.TieneCA;
cbTieneTC.Checked = cliente.TieneTC;
cbTieneTD.Checked = cliente.TieneTD;
HabilitaFormulario(true);
pnlCliente.Visible = true;
}
}Monday, September 25, 2017 9:13 PM -
protected void btnGuardar_Click(object sender, EventArgs e)
{
HabilitaFormulario(false);
Cliente clienteNuevo = new Cliente();
clienteNuevo.Rut = txtRut.Text;
clienteNuevo.Nombre = txtNombre.Text;
clienteNuevo.Edad = int.Parse(txtEdad.Text);
clienteNuevo.TieneCA = cbTieneCA.Checked;
clienteNuevo.TieneTC = cbTieneTC.Checked;
clienteNuevo.TieneTD = cbTieneTD.Checked;
bool seEdito = ((SitioBanco)this.Master).Clientes.Editar(clienteNuevo);
if(seEdito)
{
lblMensaje.Text = "Cliente editado con exito";
}
else
{
lblMensaje.Text = "ha ocurrido un error el editar al cliente";
HabilitaFormulario(true);
}
}
private void HabilitaFormulario(bool accion)
{
txtRut.Enabled = accion;
txtNombre.Enabled = accion;
txtEdad.Enabled = accion;
btnGuardar.Enabled = accion;
btnEliminar.Enabled = accion;
cbTieneCA.Enabled = accion;
cbTieneTC.Enabled = accion;
cbTieneTD.Enabled = accion;
}
protected void btnEliminar_Click(object sender, EventArgs e)
{
HabilitaFormulario(false);
Cliente cliente = ((SitioBanco)this.Master).Clientes.BuscarPorRut(txtRut.Text);
if(cliente != null)
{
((SitioBanco)this.Master).Clientes.Remove(cliente);
lblMensaje.Text = "Cliente eliminado con exito";
}
}Monday, September 25, 2017 9:14 PM -
SESION
MASTER PAGE
public partial class SitioBanco : System.Web.UI.MasterPage
{
public ClienteCollection Clientes
{
get
{
if(Session["clientes"] == null)
{
Session["clientes"] = new ClienteCollection();
}
return (ClienteCollection)Session["clientes"];
}
set
{
Session["clientes"] = value;
}
}Monday, September 25, 2017 9:19 PM -
GRIDVIEW
protected void Page_Load(object sender, EventArgs e)
{
gvClientes.DataSource = ((SitioBanco)this.Master).Clientes.Listar();
gvClientes.DataBind();
}- Edited by ChalecoAzul Monday, September 25, 2017 9:21 PM
Monday, September 25, 2017 9:21 PM -
Hi ChalecoAzul,>> protected void Page_Load(object sender, EventArgs e)
{
gvClientes.DataSource = ((SitioBanco)this.Master).Clientes.Listar();
gvClientes.DataBind();
}thanks for posting here.
The Visual C# discuss and ask the C# programming language, IDE, libraries, samples and tools. For your case about ASP.NET. please go to the ASP.NET forum for getting suitable help.
Your understanding and cooperation will be grateful.
Best Regards,
Yohann Lu
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.Tuesday, September 26, 2017 5:17 AM