简体   繁体   中英

Get value from Session or ViewState in AjaxFileUpload events from AjaxToolkit

i'm trying to upload files by AjaxFileUpload, i have got it done. But now i need also to know 2 parameters, 1 is given on the URL adreess myPage.aspx?parameter=2

the second is in ViewState.

But on event OnUploadStart or OnUploadComplete

i dont have access to these values event when i tried session.

How can i solve this problem ??

Here is my ASPX:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AutoUpload.aspx.cs" Inherits="AutoUpload" %>
<%@ Register TagPrefix="ajaxToolkit" Namespace="AjaxControlToolkit" Assembly="AjaxControlToolkit, Version=15.1.4.0, Culture=neutral, PublicKeyToken=28f01b0e84b6d53e" %>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
      <title></title>
     <script type="text/javascript" src="scripts/jquery-1.3.2.min.js">    </script>
</head>
<body>
<form id="form1" runat="server">
<div>
    <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>

    <asp:TextBox runat="server" ID="txtValue"></asp:TextBox>
    <asp:Button runat="server" ID="btnSetValue" Text="set" OnClick="btnSetValue_OnClick"/>
    <asp:HiddenField runat="server" ID="hiddenValue"/>

    <ajaxToolkit:AjaxFileUpload ID="AjaxFileUpload" MaximumNumberOfFiles="50" AllowedFileTypes="jpg,jpeg"  OnUploadComplete="AjaxFileUpload_OnUploadComplete" OnUploadStart="AjaxFileUpload_OnUploadStart" runat="server" />
</div>
</form>

And here is my server side

     using System;
     using AjaxControlToolkit;

     public static class Validators
     {
          public static bool IsNumeric(this string value)
          {
               int myInt;
               bool isNumerical = int.TryParse(value, out myInt);
               return isNumerical;
          }
      }

public partial class AutoUpload : System.Web.UI.Page
{
    public int Recid
    {
        get
        {
            if ((ViewState["Recid"] != null) && ((ViewState["Recid"].ToString()).IsNumeric()))
                return Convert.ToInt32(ViewState["Recid"]);
            return 0;
        }
        set { ViewState["Recid"] = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        string id = Request.QueryString["id"];
        if (!string.IsNullOrEmpty(id) && id.IsNumeric())
            Recid = Convert.ToInt32(id);
    }

    protected void btnSetValue_OnClick(object sender, EventArgs e)
    {
        hiddenValue.Value = txtValue.Text.ToString();
    }


    protected void AjaxFileUpload_OnUploadStart(object sender, AjaxFileUploadStartEventArgs e)
    {
        int myId = Recid; // THIS IS ALWAYS 0
        string otherValue = hiddenValue.Value;
    }

    protected void AjaxFileUpload_OnUploadComplete(object sender, AjaxFileUploadEventArgs e)
    {
        int myId = Recid; // THIS IS ALWAYS 0
        string otherValue = hiddenValue.Value;

        AjaxFileUpload.SaveAs(Server.MapPath("~/" + e.FileName));
    }


}

If you try to run the aspx with parameter for example ?id=12 it should be stored in Recid, also the value from input field should be stored in hiddenValue.

But when i'm uploading files (event OnUploadStart or OnUploadComplete) i dont have access to these values, I also tried Session but it also didnt worked.

AjaxFileUpload does not preserve query string parameters in the latest released version 15.1.4 up to this moment.

However, you can download and compile the source code where this issue is already fixed and AjaxFileUpload keeps query string parameters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM