如何在重新加载页面/进行回发后保留ActiveTabIndex?

时间:2018-05-13 11:20:59

标签: javascript c# asp.net ajaxcontroltoolkit

我试图创建一个UI,在上传图像之后,页面必须重新加载/回发,而没有TabContainer(ajaxcontroltoolkit)更改其ActiveTabIndex,但默认的按钮回发(asp:按钮)当我尝试通过浏览器手动实现页面时,我会遇到麻烦,如果我不使用下一个代码(重新加载页面),它会尝试重新执行按钮使用的最后一个方法:

  

Administrar.aspx.cs:

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

JavaScript相关功能:

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

但是!如果我在重新加载页面时使用此代码,则会丢失ActiveTabIndex。 我想要做的就是,在将图像上传到数据库时保留ActiveTabIndex而不会有浏览器手册重新加载的麻烦,你能告诉我我做错了什么或我能做什么?我已经坚持了几天。

这里有更好理解的代码:

前端

<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(只需添加按钮):

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.
        }
    }
}

0 个答案:

没有答案