0

I'm trying to make an UI where, after uploading an image, the page must do a reload/postback without a TabContainer(ajaxcontroltoolkit) changing its ActiveTabIndex, but the default postback of the button(asp:button) causes me troubles when i try to realod the page via browser manually, it tries to re-do the last method used by the button if i don't use the next code (to reload the page):

Administrar.aspx.cs:

ScriptManager.RegisterStartupScript(this, this.GetType(), "key", "ShowMessage()", true);

JavaScript related function:

function ShowMessage()
{
    alert('Exito al cargar imagen.');
    window.location.href = 'Administrar.aspx';
}

BUT!, if i use this code when the page is reloading i loss the ActiveTabIndex. All i want to do is, to keep an ActiveTabIndex when uploading an image to the database without having troubles with the browser manual reload, can you please tell me what i'm doing wrong or what can i do? i'm stuck on this for days.

here's the code for a better understanding:

FrontEnd:

<asp:TabContainer ID="TabContainer1" CssClass="cssTab" runat="server">

            <asp:TabPanel runat="server" ID="TabPanel1" HeaderText="INFORMACIÓN">
                        <ContentTemplate>
                            <div class="divCNTRL1">...</div>
                            <div class="divDSPLY1">...</div>
                        </ContentTemplate>
                    </asp:TabPanel>

            <asp:TabPanel runat="server" ID="TabPanel2" HeaderText="IMÁGENES">
                        <ContentTemplate>
                            <div class="divCNTRL2">
                                <h3>Agregar Imagenes</h3>
                                <label class="lbl">Relación:</label>
                                <asp:DropDownList CssClass="elementControl2" DataValueField="idInformacion" DataTextField="RelGal" ID="ddlRelacionGaleria" runat="server"></asp:DropDownList>

                                <label class="lbl">Imagen:
                                    <asp:RequiredFieldValidator ValidationGroup="grpImg" ID="RequiredFieldValidator12" runat="server" ErrorMessage="*Imagen Invalida." ControlToValidate="FilUplIMG" ForeColor="#CC3300"></asp:RequiredFieldValidator>
                                </label>
                                <asp:FileUpload ID="FilUplIMG" runat="server" CssClass="elementControl2" />
                                <br />
                                <asp:Button runat="server" ID="btnAgrImg" OnClick="btnAgrImg_Click" CssClass="btnAgregarPropiedad" Text="AGREGAR" />
                            </div>
                            <div class="divDSPLY2">
                                <asp:UpdatePanel runat="server" UpdateMode="Conditional">
                                    <ContentTemplate>
                                       <asp:GridView runat="server" ID="gvImg" CssClass="gvImg2">...</asp:GridView>
                                       <asp:Label ID="lblPage2" CssClass="lblPage" runat="server" ForeColor="Blue"></asp:Label>
                                    </ContentTemplate>
                                </asp:UpdatePanel>
                            </div>
                        </ContentTemplate>
                    </asp:TabPanel>
 </asp:TabContainer>

BackEnd(Just the button for adding):

public void btnAgrImg_Click(object sender, EventArgs e)
{
    if (FilUplIMG.PostedFile != null)
    {
        string FileName = Path.GetFileName(FilUplIMG.PostedFile.FileName);

        MySqlConnection con = new MySqlConnection(strConnString);

        string strQuery = "insert into inmobiliarialrz.galeria (fkrelation, Relation, FileName, FilePath) values (@fkrelation, @Relation, @FileName, @FilePath)";

        MySqlCommand cmd = new MySqlCommand(strQuery);
        if (File.Exists(Server.MapPath("~/ImagenesPROP/") + FileName))
        {
            string script = "alert(\"Ya existe esta imagen.\");";
            ScriptManager.RegisterStartupScript(this, GetType(), "ServerControlScript", script, true);
        }
        else
        {
            //save files in the disk
            FilUplIMG.SaveAs(Server.MapPath("~/ImagenesPROP/" + FileName));

            cmd.Connection = con;
            cmd.Parameters.AddWithValue("@fkrelation", ddlRelacionGaleria.SelectedValue);
            cmd.Parameters.AddWithValue("@Relation", ddlRelacionGaleria.SelectedItem.Text);
            cmd.Parameters.AddWithValue("@FileName", FileName);
            cmd.Parameters.AddWithValue("@FilePath", "~/ImagenesPROP/" + FileName);
            cmd.CommandType = CommandType.Text;
            try
            {
                con.Open();
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                lblFail.Visible = true;
                lblFail.Text = ex.Message;
            }
            finally
            {
                con.Close();
                con.Dispose();
            }
            this.MostrarImagen2();//this is a method to show data in a gridview.
            lblFail.Text = "";
            ScriptManager.RegisterStartupScript(this, this.GetType(), "key", "ShowMessage()", true);
            TabContainer1.ActiveTabIndex = 1;//here i'm trying a desperate move.
        }
    }
}
Lopez93
  • 35
  • 8
  • 1
    You could try keeping the current Tab index in a session (serverside) or even local storage (client side) and then restoring the tab index. (if I get time before my work day ends I'll create an example) – Mayhem50 May 17 '18 at 04:41
  • thanks, that'd help, i'm going to try something like that too, too see what can i do. – Lopez93 May 17 '18 at 22:37

0 Answers0