问题
This is a strange one. So I have an Ajax Toolkit file uploader called AsyncFileUpload located in an update panel. This AsyncFileUpload control, as soon as you put a file there, begins to upload it and calls the server. I then store this file as a BLOB and obtain the row ID from a database table using SELECT @@IDENTITY. So far so good, I now have a row ID and I wish to store it. I placed it inside a hidden field but when AsyncFileUpload calls OnClientUploadComplete, hidden field is blank! So I thought "okay, I'll just store it in a ViewState". Much to my surprise, same thing happened; the viewstate gets cleared. Finally I had success with using a Session variable. My question is, why does Session variable work and ViewState or HiddenField do not?
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()); }
EDIT: Found a similar post that sort of explains it. Thank you everyone! Not getting value in Viewstate in asp.net using C#?
来源:https://stackoverflow.com/questions/17580602/persisting-variable-after-asyncfileupload-call-to-onuploadedcomplete