En este caso voy a validar la cédula uruguaya y la foto ante de subirla al servidor nuestro.. (Yo siempre valido todos ah codigo, y despues uso ajax, oh jquery.)
protected void btningresar_Click(object sender, EventArgs e)
{
int ocedula = 0;
string oFotos = "";
string sExt = "";
registros pRegis = null;
string oError = "";
try
{
if (int.Parse(txtcedula.Text) < 1000000 || int.Parse(txtcedula.Text) > 9999999)
{
oError += "<li>El número de cédula debe contener 7 dígitos sin puntos ni guión<br/></li>";
txtcedula.Focus();
txtcedula.Text = "";
}
else
{
ocedula = Convert.ToInt32(txtcedula.Text);
}
}
catch
{
oError += "<li>Ingrese un número de cédula de 7 dígitos sin puntos ni guión<br/></li>";
txtcedula.Focus();
txtcedula.Text = "";
}
try
{
Random r = new Random();
int numeros = r.Next(10000000, 99999999);
oFotos = Path.GetFileName(imgSubida.PostedFile.FileName);
sExt = Path.GetExtension(oFotos);
oFotos = oFotos.Substring(oFotos.LastIndexOf(".") + 1).ToLower();
oFotos = DateTime.Now.ToString("yyyyMMdd_hhmmss") + "_" + numeros + sExt;
if (imgSubida.HasFile)
{
if (!ValidaExtension(sExt))
{
oError += "<li>El archivo no es de tipo imagen: .jpg,.png. <br/></li>";
}
else if (imgSubida.FileBytes.Length > 100000)
{
oError += "<li>El limite de imagen tiene que tener un tamaño de 100kb <br/></li>";
}
else if (oError != "")
{
oError += "<li>Selecciones una imagen con extensiones: .jpg,.png, Tamaño máximo permitido 100kb <br/></li>";
}
else
{
imgSubida.PostedFile.SaveAs(Server.MapPath("~/imagenes") + "\\" + oFotos);
//imgFotoChica.ImageUrl = "~/imagenes/" + ofoto;// para mostrar la foto abajo
oFotos = "~/imagenes/" + oFotos;
}
}
else
{
oError += "<li>Selecciones una imagen con extensiones: .jpg,.png, Tamaño máximo permitido 100kb <br/></li>";
}
}
catch
{
oError += "<li>Selecciones una imagenes con las extensiones jpg, png<br/></li>";
}
if (oError != "")
{
lblError.Text = oError;
}
// termino del tray catch y el string de error
else
{
try
{
pRegis = new registros(ocedula, oNombre, oFotos);
if (LogicaRegistros.Agregar(pRegis))
{
lblError.Text = "<li>Se ingreso correctamente, nuevo funcionario administrativo: <b> <font color='red'> " + pRegis.Nombre + " " + pRegis.Apellido + "</br></font></li>";
Limpiar();
}
else
{
lblError.Text = "<li>No se puede agregar el funcionario administrativo porque su CI ya existe</li>";
txtcedula.Focus();
txtcedula.Text = "";
}
}
catch (ApplicationException oAux)
{
lblError.Text = oAux.Message;
}
catch
{
lblError.Text = "<li>problema en la base de dato al agregar funcionario administrativo</li>";
Limpiar();
}
}
}
private bool ValidaExtension(string sExt)
{
Boolean rel = false;
switch (sExt)
{
case ".jpg":
case ".jpeg":
case ".png":
rel = true;
break;
default:
rel = false;
break;
}
return rel;
}