Reputation: 623
I am trying to use a dictionary to hold the values of file upload control. Users can upload a total of 5 documents. One document at a time. I have used C# and ASP.NET. I have tried the below, but I am not sure how to add the dictionary data to viewstate. // Declaration of Dictionary
private new Dictionary<string, string> AttachmentNames
{
get
{
if (Session["AttachmentNames"] != null)
{
return (Dictionary<string, string>)Session["AttachmentNames"];
}
else
{
return new Dictionary<string, string>();
}
}
set
{
Session["AttachmentNames"] = value;
}
}
Every time the file is uploaded, I want to add the filename , file path to the dictionary.
I have kept a loop to check the file item count and based on that I am adding the files to dictionary. Code as below:
if (!hlAttachment1.Visible)
{
hdnAttachment1.Text = this.GetFilePath(fuAttachment);
AttachmentNames.Add(fileName, hdnAttachment1.Text);
}
else if (!hlAttachment2.Visible)
{
hdnAttachment2.Text = this.GetFilePath(fuAttachment);
AttachmentNames.Add(fileName, hdnAttachment2.Text);
}
// like this till 5
Since the data is not added to viewstate, everytime the file is uploaded, only the last file is getting added to dictionary. How to add the dictionary to view state and utilize it to add items to dictionary. Now, on every post back the data is lost. How to add and update the dictionary data to view state? Thanks
Upvotes: 0
Views: 1691
Reputation: 903
I think the property can be simplified as such. This optimization allows the state of the session variable be completely managed in the property simplifying the callers code. This code should be compatible with your calling code.
private new Dictionary<string, string> AttachmentNames
{
get
{
/* may want to lock if multi threads is a possibility */
if (Session["AttachmentNames"] == null)
Session["AttachmentNames"] = new Dictionary<string, string>();
return (Dictionary<string, string>) Session["AttachmentNames"];
}
}
This code is creating a property that references a session variable. Note that there's only a get accessor on this property, there is no set to simplify things.
When we access this property, the get accessor checks the Session variable to see if it's been initialized (is not null). If it hasn't been initialized (is null) it sets the session variable to a new dictionary.
The second part of the accessor simply returns the session variable, casting it to a dictionary.
The beauty of this code is that it abstracts the session management to the property.
If your code is only adding/updating values, you can use the property as such.
AttachmentNames[fileName] = hdnAttachment1.Text;
Note that the fileName is the key to the dictionary.
Check the documentation for more info on dictionaries.
https://msdn.microsoft.com/en-us/library/xfhwa508(v=vs.110).aspx
Upvotes: 2
Reputation: 156978
add the dictionary data to viewstate
Ehh, that seems a bad idea. The view state is encrypted and put in the HTML. That content send back and forth on every request. So if you upload a 100MB file, every request and response with have a size of at least 100MB...
A better storage location is the Session
object, where you can save the uploaded files for the time being. If necessary, you can put the key of the file in the view state, so you can always relate it back to the stored file in the Session
.
Upvotes: 1