/
Cliente.aspx.cs
312 lines (245 loc) · 9.29 KB
/
Cliente.aspx.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Web.Security;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
public partial class Cliente : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
// Carga los datos en la grid
FacadeCliente Fcliente = new FacadeCliente();
DataSet dsetCliente = Fcliente.buscarClienteAll();
tablaCliente.DataSource = dsetCliente;
tablaCliente.DataBind(); // Carga los datos en la grid
Div_TablaCliente.Visible = true;
Div_NuevoCliente.Visible = false;
Div_editarCliente.Visible = false;
}
}
protected void OnRowDataBound1(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(tablaCliente, "Select$" + e.Row.RowIndex);
e.Row.Attributes["style"] = "cursor:pointer";
}
}
protected void btn_BuscaCliente_Click(object sender, EventArgs e)
{
string columna = DropList_columnaCliente.SelectedValue;
//SELECCIONA LA FORMA DE FILLTRAR DE ACUERDO A LAS COLUMNAS
string buscarX = "";
if (columna == "rut")
{
buscarX = "buscar_ClienteXRut";
}
else if (columna == "nombre")
{
buscarX = "buscar_ClienteXNombre";
}
else if (columna == "direcion")
{
buscarX = "buscar_ClienteXDireccion";
}
else if (columna == "fono")
{
buscarX = "buscar_ClienteXFono";
}
else if (columna == "email")
{
buscarX = "buscar_ClienteXEmail";
}
else if (columna == "estado")
{
buscarX = "buscar_ClienteXEstado";
}
FacadeCliente FC = new FacadeCliente();
//SI Se PRESIONA BOTON BUSCAR SIN TEXTO SE LLENA TABLA
//if (txt_texto.Text == "")
if (txt_BuscaCliente.Text == "")
{
RecargarTablaCliente();
}
else
{
//Response.Write("<script language=javascript>alert('Buscar Cliente por:"+buscarX+"');</script>)");
DataSet dset = FC.buscarClienteX(txt_BuscaCliente.Text, buscarX);
tablaCliente.DataSource = dset;
tablaCliente.DataBind(); // Carga los datos en la grid
//SI NO SE ENCUENTRAN PRODUCTOS SE MUESTRA TABLA VACIA
if (dset.Tables[0].Rows.Count == 0)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[6] { new DataColumn("Rut"), new DataColumn("Nombre"), new DataColumn("Direccion"), new DataColumn("Fono"), new DataColumn("Email"), new DataColumn("Estado") });
dt.Rows.Add(1, "NO SE ", "ENCONTRARON CLIENTES ", "", "", "");
tablaCliente.DataSource = dt;
tablaCliente.DataBind();
}
}
}
public void RecargarTablaCliente()
{
FacadeCliente fc = new FacadeCliente();
DataSet dset = fc.buscarClienteAll();
tablaCliente.DataSource = dset;
tablaCliente.DataBind();
}
protected void btx_RegistrarCliente_Click(object sender, EventArgs e)
{
FacadeCliente fc = new FacadeCliente();
int g = fc.IngresarCliente(txt_RutCliente.Text, txt_contraseCiente.Text, Drop_nombreCiudad.SelectedValue, txt_nombreCliente.Text, txt_direccionCliente.Text, txt_FonoCliente.Text, txt_emailCliente.Text, drop_estadoCliente.SelectedValue);
if (g == 1)
{
Response.Write("<script language=javascript>alert('Cliente Guardado');</script>)");
RecargarTablaCliente();
Div_TablaCliente.Visible = true;
Div_NuevoCliente.Visible = false;
Div_editarCliente.Visible = false;
}
else
{
Response.Write("<script language=javascript>alert('El Rut esta ingresado');</script>)");
}
LimpiarRegistroCliente();
}
public bool validarRut(string rut)
{
bool validacion = false;
try
{
rut = rut.ToUpper();
// rut = rut.Replace(".", "");
//rut = rut.Replace("-", "");
int rutAux = int.Parse(rut.Substring(0, rut.Length - 1));
char dv = char.Parse(rut.Substring(rut.Length - 1, 1));
int m = 0, s = 1;
for (; rutAux != 0; rutAux /= 10)
{
s = (s + rutAux % 10 * (9 - m++ % 6)) % 11;
}
if (dv == (char)(s != 0 ? s + 47 : 75))
{
validacion = true;
}
}
catch (Exception)
{
}
return validacion;
}
public void LimpiarRegistroCliente()
{
txt_RutCliente.Text = "";
txt_nombreCliente.Text = "";
txt_direccionCliente.Text = "";
txt_FonoCliente.Text = "";
txt_emailCliente.Text = "";
}
//VA A FORMULARIO EDITAR DATO
protected void btn_editaCliente_Click(object sender, EventArgs e)
{
Div_TablaCliente.Visible = false;
Div_editarCliente.Visible = true;
Div_NuevoCliente.Visible = false;
//LLENA LOS CAMPOS DE FORMULARIO EDITAR
string rut = tablaCliente.SelectedRow.Cells[0].Text;
txt_rutClienteEditar.Enabled = false;
txt_rutClienteEditar.Text = rut;
FacadeCliente contrasena = new FacadeCliente();
txt_contrasenaClienteEditar.Attributes.Add("Value", contrasena.getContrasenaCliente(rut));
txt_nombreClienteEditar.Text = tablaCliente.SelectedRow.Cells[1].Text;
txt_dirClienteEditar.Text = tablaCliente.SelectedRow.Cells[2].Text;
txt_fonoClienteEditar.Text = tablaCliente.SelectedRow.Cells[3].Text;
txt_emailClienteEditar.Text = tablaCliente.SelectedRow.Cells[4].Text;
}
//ACTUALIZAR REGISTRO DE CLIENTE
protected void btn_actualizarCliente_Click(object sender, EventArgs e)
{
string rutCliente = tablaCliente.SelectedRow.Cells[0].Text;
string ciudad = droplist_ciudadClienteEditar.SelectedValue;
string nombre = txt_nombreClienteEditar.Text;
string dir = txt_dirClienteEditar.Text;
string email = txt_emailClienteEditar.Text;
string fono = txt_fonoClienteEditar.Text;
string estado = dropList_estadoClienteEditar.SelectedValue;
string contrasena = txt_contrasenaClienteEditar.Text;
FacadeCliente FC = new FacadeCliente();
FC.ActualizarCliente(rutCliente, contrasena, ciudad, nombre, dir, fono, email, estado);
Response.Write("<script language=javascript>alert('Cliente Actualizado');</script>)");
RecargarTablaCliente();
Div_TablaCliente.Visible = true;
Div_NuevoCliente.Visible = false;
Div_editarCliente.Visible = false;
}
protected void btn_EliminarCliente_Click(object sender, EventArgs e)
{
string rut = tablaCliente.SelectedRow.Cells[0].Text;
FacadeCliente FC = new FacadeCliente();
FC.EliminarCliente(rut);
Response.Write("<script language=javascript>alert('Cliente Eliminado');</script>)");
RecargarTablaCliente();
Div_TablaCliente.Visible = true;
Div_NuevoCliente.Visible = false;
Div_editarCliente.Visible = false;
}
protected void btn_NuevoCliente_Click(object sender, EventArgs e)
{
Div_TablaCliente.Visible = false;
Div_NuevoCliente.Visible = true;
Div_editarCliente.Visible = false;
}
//PAGINACION TABLA Producto
protected void Tabla_Cliente_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
tablaCliente.PageIndex = e.NewPageIndex;
RecargarTablaCliente();
}
protected void txt_RutCliente_TextChanged(object sender, EventArgs e)
{
}
protected void CustomValidator1_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
{
bool validacion = false;
try
{
string rut = args.Value;
rut = rut.ToUpper();
// rut = rut.Replace(".", "");
//rut = rut.Replace("-", "");
int rutAux = int.Parse(rut.Substring(0, rut.Length - 1));
char dv = char.Parse(rut.Substring(rut.Length - 1, 1));
int m = 0, s = 1;
for (; rutAux != 0; rutAux /= 10)
{
s = (s + rutAux % 10 * (9 - m++ % 6)) % 11;
}
if (dv == (char)(s != 0 ? s + 47 : 75))
{
validacion = true;
}
}
catch (Exception)
{
}
args.IsValid = validacion;
}
protected void btn_cancelarEditar_Click(object sender, EventArgs e)
{
Div_editarCliente.Visible = false;
Div_TablaCliente.Visible = true;
Div_editarCliente.Visible = false;
}
}