protected void btnGuardar_Click(object sender, EventArgs e)
        {
            if (Session["Cadena"] == null)
            {
                string alerta = "<script>alert('La sesión ha expirado, debe volver a iniciar sesión');window.location.href='Login.aspx'</script>";
                Session["Tab"] = "Salir";
                Response.Write(alerta);
                return;
            }

            //Actualiza los permisos de los botones en grid
            PermisosUsuario();

            int total_representantes = 0;


            if (Session["Grid"] != null && ((DataTable)Session["Grid"]).Rows.Count > 0)
            {
                total_representantes = ((DataTable)Session["Grid"]).Rows.Count;
            }


            //Obtener valores de fechas
            DateTime?dateDesde = string.IsNullOrEmpty(ApartirDe.Text) ? (DateTime?)null : DateTime.Parse(ApartirDe.Text);
            DateTime?dateHasta = string.IsNullOrEmpty(Hasta.Text) ? (DateTime?)null : DateTime.Parse(Hasta.Text);


            string valida = string.Empty;

            //Validar que el dato a guardar o modificar no exista
            if (txt_Nombre.Text.Trim() == string.Empty)
            {
                valida = "Para poder guardar escriba un nombre";
            }

            else if (txt_Paterno.Text.Trim() == string.Empty)
            {
                valida = "Para poder guardar escriba un apellido paterno";
            }

            else if (txt_Materno.Text.Trim() == string.Empty)
            {
                valida = "Para poder guardar escriba un apellido materno";
            }

            else if (txt_rfc.Text.Trim() == string.Empty)
            {
                valida = "Para poder guardar escriba un RFC valido";
            }

            //Validar fechas que existan
            else if ((dateDesde == null || dateHasta == null))
            {
                valida = "Debe seleccionar un rango de fechas validas";
            }

            //Validar fechas rangos correctos
            else if ((dateDesde != null && dateHasta != null) && dateDesde > dateHasta)
            {
                valida = "La fecha a partir de, no puede ser mayor a la fecha hasta";
            }

            else if (Session["Grid"] != null && ((DataTable)(Session["Grid"])).Rows.Count > 0)
            {
                foreach (DataRow fila in ((DataTable)(Session["Grid"])).Rows)
                {
                    DateTime?dateDesde_f = string.IsNullOrEmpty(fila["APARTIRDE"].ToString().Trim()) ? (DateTime?)null : DateTime.Parse(fila["APARTIRDE"].ToString().Trim());
                    DateTime?dateHasta_f = string.IsNullOrEmpty(fila["HASTA"].ToString().Trim()) ? (DateTime?)null : DateTime.Parse(fila["HASTA"].ToString().Trim());

                    //Nuevo
                    if (ModalTitulo.InnerText.Contains("Nuevo") &&
                        fila["NOMBRE"].ToString().ToUpper().Trim() == txt_Nombre.Text.ToUpper().Trim() &&
                        fila["APELLIDOPATERNO"].ToString().ToUpper().Trim() == txt_Paterno.Text.ToUpper().Trim() &&
                        fila["APELLIDOMATERNO"].ToString().ToUpper().Trim() == txt_Materno.Text.ToUpper().Trim() &&
                        fila["RFC"].ToString().ToUpper().Trim() == txt_rfc.Text.ToUpper().Trim() &&
                        dateDesde_f <dateDesde && dateHasta_f> dateDesde)
                    {
                        valida = "Ya existe este representante legal en un rango de fechas a las seleccionadas: Nombre(" + txt_Nombre.Text.Trim() + "), Ap Paterno(" + txt_Paterno.Text.Trim() +
                                 "), Ap Materno(" + txt_Materno.Text.Trim() + "), RFC(" + txt_rfc.Text.Trim() + "), Fecha apartir de(" + fila["APARTIRDE"].ToString().Trim() + "), Fecha hasta(" + fila["HASTA"].ToString().Trim() + ") ";
                        break;
                    }


                    //Editar
                    if (ModalTitulo.InnerText.Contains("Editar") &&
                        fila["REPRESENTANTEKEY"].ToString().ToUpper().Trim() != Session["REPRESENTANTEKEY"].ToString().Trim().ToUpper() &&
                        fila["NOMBRE"].ToString().ToUpper().Trim() == txt_Nombre.Text.ToUpper().Trim() &&
                        fila["APELLIDOPATERNO"].ToString().ToUpper().Trim() == txt_Paterno.Text.ToUpper().Trim() &&
                        fila["APELLIDOMATERNO"].ToString().ToUpper().Trim() == txt_Materno.Text.ToUpper().Trim() &&
                        fila["RFC"].ToString().ToUpper().Trim() == txt_rfc.Text.ToUpper().Trim() &&
                        dateDesde_f <dateDesde && dateHasta_f> dateDesde)
                    {
                        valida = "Ya existe este representante legal en un rango de fechas a las seleccionadas: Nombre(" + txt_Nombre.Text.Trim() + "), Ap Paterno(" + txt_Paterno.Text.Trim() +
                                 "), Ap Materno(" + txt_Materno.Text.Trim() + "), RFC(" + txt_rfc.Text.Trim() + "), Fecha apartir de(" + fila["APARTIRDE"].ToString().Trim() + "), Fecha hasta(" + fila["HASTA"].ToString().Trim() + ") ";
                        break;
                    }
                }
            }


            if (valida.Length > 0)
            {
                //Mantiene el modal
                MostrarModal();

                AlertError(valida);
                return;
            }

            var imagen = BinaryImage.Value;

            //Guardar
            string    mensaje = "";
            DataTable dt      = new DataTable();

            if (ModalTitulo.InnerText.Contains("Nuevo"))
            {
                dt = catalogo.Guardar_Representante_Legal(txt_Nombre.Text.ToUpper().Trim(), txt_Paterno.Text.ToUpper().Trim(), txt_Materno.Text.ToUpper().Trim(),
                                                          txt_rfc.Text.ToUpper().Trim(), txt_correo.Text.Trim(), txt_tel.Text.Trim(), dateDesde, dateHasta,
                                                          imagen, lblCadena.Text, ref mensaje);
            }
            else if (ModalTitulo.InnerText.Contains("Editar"))
            {
                dt = catalogo.Editar_Representante_Legal(int.Parse(Session["REPRESENTANTEKEY"].ToString()), txt_Nombre.Text.ToUpper().Trim(), txt_Paterno.Text.ToUpper().Trim(),
                                                         txt_Materno.Text.ToUpper().Trim(), txt_rfc.Text.ToUpper().Trim(), txt_correo.Text.Trim(), txt_tel.Text.Trim(), dateDesde,
                                                         dateHasta, imagen, lblCadena.Text, ref mensaje);
            }

            if (dt != null && dt.Rows.Count > 0)
            {
                Grid.DataSource = Session["Grid"] = dt;
                Grid.DataBind();
                Grid.Settings.VerticalScrollableHeight = 330;
                Grid.Settings.VerticalScrollBarMode    = ScrollBarMode.Visible;
                Grid.SettingsPager.PageSize            = 20;

                //Selecccionar el primer registro del grid
                if (Session["Grid"] != null)
                {
                    Grid.Selection.SelectRow(0);
                }

                //AlertSuccess("El representante legal se " + (ModalTitulo.InnerText.Contains("Editar") ? "actualizó" : "agregó") + ".");
            }
            else
            {
                Grid.DataSource = Session["Grid"];
                Grid.DataBind();
            }

            Grid.Settings.VerticalScrollableHeight = 330;
            Grid.Settings.VerticalScrollBarMode    = ScrollBarMode.Visible;

            //Actualiza los permisos de los botones en grid
            PermisosUsuario();
        }