AsyncFileUpload调用OnUploadedComplete后保持变量

时间:2013-07-10 20:49:41

标签: c# session viewstate asyncfileupload

这是一个奇怪的。所以我在更新面板中有一个名为AsyncFileUpload的Ajax Toolkit文件上传器。这个AsyncFileUpload控件,只要你把文件放在那里,就会开始上传它并调用服务器。然后,我将此文件存储为BLOB,并使用SELECT @@ IDENTITY从数据库表中获取行ID。到目前为止这么好,我现在有一个行ID,我希望存储它。我把它放在一个隐藏的字段中但是当AsyncFileUpload调用OnClientUploadComplete时,隐藏字段为空!所以我想“好吧,我只是将它存储在ViewState中”。令我惊讶的是,同样的事情发生了;视图状态被清除。最后,我成功使用了Session变量。我的问题是,为什么Session变量起作用而Vie​​wState或HiddenField不起作用?

FRONT:

<script type="text/javascript">
    function UploadComplete(sender, args) {
        var fileSize = args.get_length();
        if (fileSize > 2000000) {
            alert("Logo size must be smaller than 2MB");
        }

        $("[id*=hfUploadSuccessful]").val("1");
        //Calls a postback and thus lvMembers_PreRender gets executed to get results
        __doPostBack('<%=UpdatePanel1.ClientID %>', null);
    }
</script>

                                    <div id="divClassFileUpload">
                                        <asp:AsyncFileUpload ID="AsyncFileUpload1" runat="server" Width="350" CssClass="FileUploadClass floatLeft"
                                            UploaderStyle="Modern"
                                            CompleteBackColor="Lime"
                                            ErrorBackColor="Red"
                                            ThrobberID="Throbber"
                                            UploadingBackColor="#66CCFF"
                                            OnClientUploadError="uploadError" 
                                            OnClientUploadStarted="StartUpload"
                                            OnClientUploadComplete="UploadComplete"
                                            OnUploadedComplete="AsyncFileUpload1_UploadedComplete" />
                                    </div>

BACK:

    protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
    {

            //hfSelectedArticleId.Value = dt.Rows[0]["ArticleId"].ToString());
            //ViewState.Add("ArticleId", dt.Rows[0]["ArticleId"].ToString());
            Session.Add("ArticleId", dt.Rows[0]["ArticleId"].ToString());

        }
编辑:找到一个类似的帖子,解释它。谢谢大家! Not getting value in Viewstate in asp.net using C#?

0 个答案:

没有答案