protected void btnGuardar_Click(object sender, ImageClickEventArgs e)
    {
        int    registroUpdate = 0;
        string cleanMessage   = string.Empty;
        BL_TBL_RequerimientoSubDetalle obj = new BL_TBL_RequerimientoSubDetalle();
        DataTable dtResultado = new DataTable();

        if (GridView1.Rows.Count == 0)
        {
            cleanMessage = "No existe registros";
            ScriptManager.RegisterStartupScript(this, typeof(Page), "invocarfuncion", "doAlert('" + cleanMessage + "');", true);
        }
        else
        {
            string Reqs_Correlativo;
            string Requ_Numero;
            string Reqd_CodLinea;
            string ide_valor;
            string Proyecto;

            foreach (GridViewRow row in GridView1.Rows)
            {
                Requ_Numero      = GridView1.DataKeys[row.RowIndex].Values[0].ToString(); // extrae key
                Reqd_CodLinea    = GridView1.DataKeys[row.RowIndex].Values[1].ToString(); // extrae key
                Reqs_Correlativo = GridView1.DataKeys[row.RowIndex].Values[2].ToString(); // extrae key
                ide_valor        = GridView1.DataKeys[row.RowIndex].Values[3].ToString(); // extrae key
                Proyecto         = GridView1.DataKeys[row.RowIndex].Values[4].ToString(); // extrae key

                TextBox txtInicio    = row.FindControl("txtInicio") as TextBox;           //(TextBox)GridView1.HeaderRow.FindControl("txtInicio");
                TextBox txtTarifaDia = row.FindControl("txtTarifaDia") as TextBox;        //  (TextBox)GridView1.HeaderRow.FindControl("txtTarifaDia");
                TextBox txtFin       = row.FindControl("txtFin") as TextBox;              //
                TextBox txtUltimaVal = row.FindControl("txtUltimaVal") as TextBox;        //


                DropDownList ddlTarifa = row.FindControl("ddlTarifa") as DropDownList;//



                TextBox txtDia_inicio = row.FindControl("txtDia_inicio") as TextBox; //
                TextBox txtDia_fin    = row.FindControl("txtDia_fin") as TextBox;    //

                string inicio = string.IsNullOrEmpty(txtDia_inicio.Text) ? "0" : txtDia_inicio.Text;
                txtDia_fin.Text = (Convert.ToInt32(inicio) - 1).ToString();


                //DateTime fechaActual = DateTime.Now;
                //int anyo = fechaActual.Year;
                //int mes = fechaActual.Month;
                //int MES = Convert.ToInt32(mes) - 1;
                //int MES_SGTE = Convert.ToInt32(mes);
                //int ANIO = Convert.ToInt32(anyo);
                //int ANIO_SGTE = Convert.ToInt32(anyo);

                //if (MES == 0)//enero
                //{
                //    MES = 12;
                //    ANIO = ANIO - 1;
                //}


                //MES.ToString().Trim().PadLeft(2, '0');
                //MES_SGTE.ToString().Trim().PadLeft(2, '0');


                //txtInicio.Text= "21/" + MES.ToString().Trim().PadLeft(2, '0') + "/" + ANIO.ToString();
                //txtfin.Text = "20/" + MES_SGTE.ToString().Trim().PadLeft(2, '0') + "/" + ANIO_SGTE.ToString();

                Label lblMoneda = row.FindControl("lblMoneda") as Label;//

                string IDE_MONEDA, TIPO_TARIFA;
                IDE_MONEDA = lblMoneda.Text;
                if (ddlTarifa.SelectedIndex == 0)
                {
                    TIPO_TARIFA = string.Empty;
                }
                else
                {
                    TIPO_TARIFA = ddlTarifa.SelectedValue.ToString();
                }



                if (txtInicio.Text != string.Empty && txtFin.Text != string.Empty)
                {
                    if (EsFecha(txtInicio.Text) == false)
                    {
                        cleanMessage = "Error de fecha de inicio del req." + Requ_Numero + "." + Reqd_CodLinea + "-" + Reqs_Correlativo;
                        ScriptManager.RegisterStartupScript(this, typeof(Page), "invocarfuncion", "doAlert('" + cleanMessage + "');", true);
                    }
                    else if (EsFecha(txtFin.Text) == false)
                    {
                        cleanMessage = "Error de fecha de termino del req." + Requ_Numero + "." + Reqd_CodLinea + "-" + Reqs_Correlativo;
                        ScriptManager.RegisterStartupScript(this, typeof(Page), "invocarfuncion", "doAlert('" + cleanMessage + "');", true);
                    }
                    else if (txtDia_inicio.Text == string.Empty)
                    {
                        cleanMessage = "Ingresar día de incio del periodo del " + Requ_Numero + "." + Reqd_CodLinea + "-" + Reqs_Correlativo;
                        ScriptManager.RegisterStartupScript(this, typeof(Page), "invocarfuncion", "doAlert('" + cleanMessage + "');", true);
                    }
                    else if (txtDia_fin.Text == string.Empty)
                    {
                        cleanMessage = "Ingresar día de termino del periodo del " + Requ_Numero + "." + Reqd_CodLinea + "-" + Reqs_Correlativo;
                        ScriptManager.RegisterStartupScript(this, typeof(Page), "invocarfuncion", "doAlert('" + cleanMessage + "');", true);
                    }

                    else
                    {
                        if (EsFecha(txtInicio.Text) == true && EsFecha(txtFin.Text) == true)
                        {
                            if (Convert.ToDateTime(txtInicio.Text) > Convert.ToDateTime(txtFin.Text))
                            {
                                cleanMessage = "La fecha de inicio del Req. " + Requ_Numero + "." + Reqd_CodLinea + "-" + Reqs_Correlativo + ", no puede ser mayor a su fecha de finalización";
                                ScriptManager.RegisterStartupScript(this, typeof(Page), "invocarfuncion", "doAlert('" + cleanMessage + "');", true);
                            }
                            else
                            {
                                string PRECIO = string.IsNullOrEmpty(txtTarifaDia.Text) ? "0.00" : txtTarifaDia.Text;
                                if (txtUltimaVal.Text == string.Empty)
                                {
                                    // REGISTRAR INFO
                                    dtResultado = obj.USP_UPDATE_TBL_VALORIZACION_FECHA(ide_valor, Requ_Numero, Reqd_CodLinea, Reqs_Correlativo, Convert.ToDateTime(txtInicio.Text).ToString("dd/MM/yyyy"), Convert.ToDateTime(txtFin.Text).ToString("dd/MM/yyyy"), PRECIO, IDE_MONEDA, TIPO_TARIFA, txtDia_inicio.Text, txtDia_fin.Text);
                                    if (dtResultado.Rows.Count > 0)
                                    {
                                        if (Convert.ToInt32(dtResultado.Rows[0]["ID"].ToString()) > 0)
                                        {
                                            registroUpdate++;
                                        }
                                    }
                                }
                                else
                                {
                                    if (Convert.ToDateTime(txtUltimaVal.Text) > Convert.ToDateTime(txtFin.Text))
                                    {
                                        cleanMessage = "La fecha de fin del Req. " + Requ_Numero + "." + Reqd_CodLinea + "-" + Reqs_Correlativo + ", no puede ser menor a su fecha de su ultima valorización";
                                        ScriptManager.RegisterStartupScript(this, typeof(Page), "invocarfuncion", "doAlert('" + cleanMessage + "');", true);
                                    }
                                    else
                                    {
                                        // REGISTRAR INFO
                                        dtResultado = obj.USP_UPDATE_TBL_VALORIZACION_FECHA(ide_valor, Requ_Numero, Reqd_CodLinea, Reqs_Correlativo, Convert.ToDateTime(txtInicio.Text).ToString("dd/MM/yyyy"), Convert.ToDateTime(txtFin.Text).ToString("dd/MM/yyyy"), PRECIO, IDE_MONEDA, TIPO_TARIFA, txtDia_inicio.Text, txtDia_fin.Text);
                                        if (dtResultado.Rows.Count > 0)
                                        {
                                            if (Convert.ToInt32(dtResultado.Rows[0]["ID"].ToString()) > 0)
                                            {
                                                registroUpdate++;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                else if (txtInicio.Text != string.Empty && txtFin.Text == string.Empty)
                {
                    if (EsFecha(txtInicio.Text) == true)
                    {
                        string PRECIO = string.IsNullOrEmpty(txtTarifaDia.Text) ? "0.00" : txtTarifaDia.Text;
                        dtResultado = obj.USP_UPDATE_TBL_VALORIZACION_FECHA(ide_valor, Requ_Numero, Reqd_CodLinea, Reqs_Correlativo, Convert.ToDateTime(txtInicio.Text).ToString("dd/MM/yyyy"), "", PRECIO, IDE_MONEDA, TIPO_TARIFA, txtDia_inicio.Text, txtDia_fin.Text);
                        if (dtResultado.Rows.Count > 0)
                        {
                            registroUpdate++;
                        }
                    }
                    else
                    {
                        cleanMessage = "Error de fecha de inicio del req." + Requ_Numero;
                        ScriptManager.RegisterStartupScript(this, typeof(Page), "invocarfuncion", "doAlert('" + cleanMessage + "');", true);
                    }
                }
            }
        }



        if (registroUpdate > 0)
        {
            cleanMessage = "Actualización correcta";
            ScriptManager.RegisterStartupScript(this, typeof(Page), "invocarfuncion", "doAlert('" + cleanMessage + "');", true);
            Listar();
        }
        else
        {
            cleanMessage = "Completar datos para la valorización";
            ScriptManager.RegisterStartupScript(this, typeof(Page), "invocarfuncion", "doAlert('" + cleanMessage + "');", true);
            Listar();
        }
    }