protected void btnNuevo_Click(object sender, ImageClickEventArgs e)
        {
            CAMPO campo = new CAMPO()
            {
                CAM_REQUERIDO = bool.Parse(((RadioButtonList)this.grvCampo.FooterRow.FindControl("rblRequeridoFooter")).SelectedValue),
                MEN_CODIGO=int.Parse(((HiddenField)this.frmMensaje.FindControl("hdnCodigoMensaje")).Value)
            };

            CAMPO_PLANTILLA campoPlantilla=new CAMPO_PLANTILLA()
            {
                CMP_CODIGO = int.Parse(((DropDownList)this.grvCampo.FooterRow.FindControl("drlCampoPlantillaFooter")).SelectedValue)
            };
            campo.CAMPO_PLANTILLA = campoPlantilla;

            MENSAJE mensaje = new MENSAJE()
            {
                MEN_CODIGO=int.Parse(((HiddenField)this.frmMensaje.FindControl("hdnCodigoMensaje")).Value)
            };

            campo.MENSAJE = mensaje;

            EstadoOperacion estado = BusinessLayer.Mensajeria.CampoMensajeBL.insertarCampoPorCampoPlantilla(campo);
            if (estado.Estado)
            {
                this.grvCampo.DataBind();
            }
            else
            {
                this.lblMensaje.Text = estado.Mensaje;
            }
        }
        protected override void AgregarCabeceraMensaje(CAMPO_PLANTILLA campoPlantilla, Valor valorCabecera, Valor valorCuerpo)
        {
            EnumTipoDatoCampo tipoDato = (EnumTipoDatoCampo)
            Enum.ToObject(typeof(EnumTipoDatoCampo), campoPlantilla.TIPO_DATO.TDT_CODIGO);

            Campo campo = new Campo(campoPlantilla.CMP_CODIGO, campoPlantilla.CMP_CABECERA, true, campoPlantilla.CMP_POSICION_RELATIVA
                , campoPlantilla.CMP_NOMBRE, false, 0, null, campoPlantilla.CMP_LONGITUD, valorCuerpo, tipoDato, false);

            Campos.Add(campo);
        }
        public static EstadoOperacion eliminarCampoPlantilla(CAMPO_PLANTILLA CampoPlantilla)
        {
            DbFactory Factoria = DataAccessFactory.ObtenerProveedor();
            try
            {
                using (Switch contexto = new Switch())
                {
                    using (contexto.CreateConeccionScope())
                    {
                        string query =
                            "DELETE FROM [CAMPO_PLANTILLA]" +
                            " WHERE [CMP_CODIGO] = @codigo";

                        DbCommand Comando = contexto.CreateCommand(query, CommandType.Text);
                        Comando.Parameters.Add(Factoria.CrearParametro("@codigo", CampoPlantilla.CMP_CODIGO));

                        if (Comando.ExecuteNonQuery() != 1)
                        {
                            return new EstadoOperacion(false, null, null);
                        }

                        return new EstadoOperacion(true, null, null);
                    }
                }
            }
            catch (DbException e)
            {
                DbExceptionProduct exception = Factoria.CrearException(e);
                if (exception.ForeignKeyError())
                {
                    return new EstadoOperacion(false, "El Campo Plantilla corresponde a un campo en el sistema y no se puede eliminar", e, true);
                }
                else
                {
                    return new EstadoOperacion(false, e.Message, e);
                }
            }
            catch (Exception e)
            {
                return new EstadoOperacion(false, e.Message, e);
            }
        }
        public static EstadoOperacion insertarCampoPlantilla(CAMPO_PLANTILLA CampoPlantilla)
        {
            if (!CampoPlantilla.CMP_CABECERA)
            {
                if (DataAccess.Mensajeria.CampoPlantillaDA.
                obtenerCampoPlantillaPorPosicionRelativaPorGrupoMensaje(CampoPlantilla.CMP_POSICION_RELATIVA, CampoPlantilla.GRUPO_MENSAJE.GMJ_CODIGO) != null)
                {
                    return new EstadoOperacion(false, "La Posición Relativa ya ha sido asignada", null, true);
                }
            }
            try
            {
                using (Switch contexto = new Switch())
                {
                    using (contexto.CreateConeccionScope())
                    {
                        CampoPlantilla.CMP_CODIGO = (from c in contexto.CAMPO_PLANTILLA
                                                                        orderby c.CMP_CODIGO descending
                                                                        select c.CMP_CODIGO).FirstOrDefault() + 1;

                        string query =
                            "INSERT INTO [CAMPO_PLANTILLA]" +
                            "([CMP_CODIGO]" +
                            ",[CMP_NOMBRE]" +
                            ",[CMP_LONGITUD]" +
                            ",[CMP_SELECTOR]" +
                            ",[CMP_VARIABLE]" +
                            ",[CMP_PROTEGIDO_LOG]" +
                            ",[CMP_ALMACENADO]" +
                            ",[CMP_POSICION_RELATIVA]" +
                            ",[CMP_LONGITUD_CABECERA]" +
                            ",[GMJ_CODIGO]" +
                            ",[CMP_CABECERA]" +
                            ",[CMP_BITMAP]" +
                            ",[CMP_TRANSACCIONAL]" +
                            ",[TDT_CODIGO])" +
                            "VALUES" +
                            "(@codigo" +
                            ",@nombre" +
                            ",@longitud" +
                            ",@selector" +
                            ",@variable" +
                            ",@protegidolog" +
                            ",@almacenado" +
                            ",@posicionrelativa" +
                            ",@longitudCabecera" +
                            ",@grupomensaje_codigo" +
                            ",@cabecera" +
                            ",@bitmap" +
                            ",@transaccional" +
                            ",@tipodato_codigo)";

                        DbCommand Comando = CampoPlantillaDA.crearComando(contexto, CampoPlantilla, query);

                        if (Comando.ExecuteNonQuery() != 1)
                        {
                            return new EstadoOperacion(false, null, null);
                        }

                        return new EstadoOperacion(true, null, null);
                    }
                }
            }
            catch (Exception e)
            {

                return new EstadoOperacion(false, e.Message, e);
            }
        }
        public static EstadoOperacion modificarCampoPlantilla(CAMPO_PLANTILLA CampoPlantilla)
        {
            if (!CampoPlantilla.CMP_CABECERA)
            {
                CAMPO_PLANTILLA CampoPlantillaAnterior = DataAccess.Mensajeria.CampoPlantillaDA.obtenerCampoPlantillaSinRelaciones(CampoPlantilla.CMP_CODIGO);

                if (CampoPlantilla.CMP_POSICION_RELATIVA != CampoPlantillaAnterior.CMP_POSICION_RELATIVA)
                {
                    if (DataAccess.Mensajeria.CampoPlantillaDA.
                        obtenerCampoPlantillaPorPosicionRelativaPorGrupoMensaje(CampoPlantilla.CMP_POSICION_RELATIVA, CampoPlantilla.GRUPO_MENSAJE.GMJ_CODIGO) != null)
                    {
                        return new EstadoOperacion(false, "La Posición Relativa ya ha sido asignada", null, true);
                    }
                }
            }
            try
            {
                using (Switch contexto = new Switch())
                {
                    using (contexto.CreateConeccionScope())
                    {
                        string query =
                            "UPDATE [CAMPO_PLANTILLA]" +
                            " SET [CMP_NOMBRE]   =  @nombre" +
                            ",[CMP_LONGITUD] = @longitud" +
                            ",[CMP_SELECTOR] = @selector" +
                            ",[CMP_VARIABLE] = @variable" +
                            ",[CMP_PROTEGIDO_LOG] = @protegidolog" +
                            ",[CMP_POSICION_RELATIVA]= @posicionrelativa" +
                            ",[CMP_LONGITUD_CABECERA]= @longitudCabecera" +
                            ",[CMP_ALMACENADO] = @almacenado" +
                            ",[CMP_CABECERA] = @cabecera" +
                            ",[CMP_BITMAP] = @bitmap" +
                            ",[CMP_TRANSACCIONAL] = @transaccional" +
                            ",[TDT_CODIGO] = @tipodato_codigo" +
                            " WHERE [CMP_CODIGO] = @codigo";

                        DbCommand Comando = CampoPlantillaDA.crearComando(contexto, CampoPlantilla, query);

                        if (Comando.ExecuteNonQuery() != 1)
                        {
                            return new EstadoOperacion(false, null, null);
                        }

                        return new EstadoOperacion(true, null, null);
                    }
                }
            }
            catch (SqlException ex)
            {
                return new EstadoOperacion(false, ex.Errors[0].Message, ex, true);
            }
            catch (Exception e)
            {

                return new EstadoOperacion(false, e.Message, e);
            }
        }