Here is a snapshot of the functionality I'm trying to achive:
1) To create a custom webcontrol that will allow the upload of documents (3 at the time).
2) To pass in the path to the Directory where these documents should be saved.
3) To show the existing documents in the saved folder and allow to delete these documents.
Problem:
For the first time I load the control, the "To_Directory" (which is the one that contains the To Directory Path) is passed in, so all is OK.
However, as soon as I press any of the buttons, postback happens and I loose the original value of the (To_Directory) textbox. Therefore nothing is loaded from the Directory where the documents were saved off.
I'm probably missing something fundamental, or I'm going about it the wrong way. Any help would be appreciated.
Many Thanks,
Andrea
Here is the picture of the control when first loaded:
well if you wont to seve on memery.. you could try to save it on seve/loadControlState by object array you could use your EventSubmitKey as array and retrive values: like EventSubmitKey = MYSaveingtThing- (on the event) save it on session, coocky...
Not sure if I got that, would you have an example? - sorry, but here is another explanation to this problem:
<div mce_keep="true">First I get the ToDirectory text from the properties is assigned.</div>
<div mce_keep="true">Second the loadChildControls happens, so I create the controls - all is good because at this time the toDirectory text contains my path!</div>
<div mce_keep="true">Third the the viewstate is loaded (not overriding anything here).</div>
<div mce_keep="true">Fourth the rendering(again, not overriding it).</div>
Then if the button (any of them) gets pressed again the cycle starts again - only this time in the createchildcontrols, I don't have my text for the ToDirectory set anymore because the loadViewstate happens after this method. But I need my value here so
that I can create the extra controls I need.
andreaban
Member
1 Points
7 Posts
How to keep value in control after postback? ViewState?
Mar 03, 2009 09:41 AM|LINK
Here is a snapshot of the functionality I'm trying to achive:
1) To create a custom webcontrol that will allow the upload of documents (3 at the time).
2) To pass in the path to the Directory where these documents should be saved.
3) To show the existing documents in the saved folder and allow to delete these documents.
Problem:
For the first time I load the control, the "To_Directory" (which is the one that contains the To Directory Path) is passed in, so all is OK.
However, as soon as I press any of the buttons, postback happens and I loose the original value of the (To_Directory) textbox. Therefore nothing is loaded from the Directory where the documents were saved off.
I'm probably missing something fundamental, or I'm going about it the wrong way. Any help would be appreciated.
Many Thanks,
Andrea
Here is the picture of the control when first loaded:
http://crazycompany.co.uk/tinc?key=mUsEEFJy&preview=1
The code:
namespace CustomFileUpload { [ AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal), AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal), DefaultEvent("Submit"), DefaultProperty("ButtonText"), ToolboxData("<{0}:Register runat=\"server\"> </{0}:Register>"), ] public class Register : CompositeControl { private FileUpload f1; private FileUpload f2; private FileUpload f3; private TextBox To_Directory = new TextBox(); private Button submitButton; private static readonly object EventSubmitKey = new object(); // The following properties are delegated to // child controls. [ Bindable(true), Category("Appearance"), DefaultValue(""), Description("Upload Files.") ] public string ButtonText { get { EnsureChildControls(); return submitButton.Text; } set { EnsureChildControls(); submitButton.Text = value; } } [ Bindable(false), Category("FileUpload"), DefaultValue(""), Description("Destination Directory") ] public string ToDirectory { set { To_Directory.Text = value; } } [ Bindable(false), Category("FileUpload"), DefaultValue(""), Description("F1 Value"), ReadOnly(true) ] public FileUpload F1Value { get { return f1; } } [ Bindable(false), Category("FileUpload"), DefaultValue(""), Description("F2 Value") ] public FileUpload F2Value { get { return f2; } } [ Bindable(false), Category("FileUpload"), DefaultValue(""), Description("F3 Value") ] public FileUpload F3Value { get { return f3; } } // The Submit event. [ Category("Action"), Description("Raised when the user clicks the button.") ] public event EventHandler Submit { add { Events.AddHandler(EventSubmitKey, value); } remove { Events.RemoveHandler(EventSubmitKey, value); } } // The method that raises the Submit event. protected virtual void OnSubmit(EventArgs e) { EventHandler SubmitHandler = (EventHandler)Events[EventSubmitKey]; if (SubmitHandler != null) { SubmitHandler(this, e); } } // Handles the Click event of the Button and raises // the Submit event. private void _button_Click(object source, EventArgs e) { OnSubmit(EventArgs.Empty); //save files if (f1.PostedFile.FileName != "") { string fileName = ""; fileName = Path.GetFileName(f1.PostedFile.FileName); f1.PostedFile.SaveAs(To_Directory.Text.ToString() + "/" + fileName); } if (f2.PostedFile.FileName != "") { string fileName = ""; fileName = Path.GetFileName(f2.PostedFile.FileName); f2.PostedFile.SaveAs(To_Directory.Text.ToString() + "/" + fileName); } if (f3.PostedFile.FileName != "") { string fileName = ""; fileName = Path.GetFileName(f3.PostedFile.FileName); f3.PostedFile.SaveAs(To_Directory.Text.ToString() + "/" + fileName); } } // Handles the Click event of the Button and raises // the Submit event. private void _bDelete_Click(object source, EventArgs e, int s) { //OnSubmit(EventArgs.Empty); //delete file here } protected override void RecreateChildControls() { EnsureChildControls(); } protected override void CreateChildControls() { Controls.Clear(); f2 = new FileUpload(); f1 = new FileUpload(); f3 = new FileUpload(); f1.Style.Add("font-family", " Arial, Helvetica, sans-serif"); f1.Style.Add("font-size", "11px"); f1.Style.Add("color", "#333333"); f1.Style.Add("text-decoration", "none"); Controls.Add(new LiteralControl("<br>")); this.Controls.Add(f1); f2.Style.Add("font-family", " Arial, Helvetica, sans-serif"); f2.Style.Add("font-size", "11px"); f2.Style.Add("color", "#333333"); f2.Style.Add("text-decoration", "none"); Controls.Add(new LiteralControl("<br>")); this.Controls.Add(f2); f3.Style.Add("font-family", " Arial, Helvetica, sans-serif"); f3.Style.Add("font-size", "11px"); f3.Style.Add("color", "#333333"); f3.Style.Add("text-decoration", "none"); Controls.Add(new LiteralControl("<br>")); this.Controls.Add(f3); submitButton = new Button(); submitButton.ID = "button1"; submitButton.Text = "Upload Documents"; submitButton.Style.Add("font-family", " Arial, Helvetica, sans-serif"); submitButton.Style.Add("font-size", "11px"); submitButton.Style.Add("color", "#333333"); submitButton.Style.Add("text-decoration", "none"); Controls.Add(new LiteralControl("<br>")); submitButton.Click += new EventHandler(_button_Click); this.Controls.Add(submitButton); Controls.Add(new LiteralControl("<br>")); if (To_Directory.Text.ToString() != "") { DirectoryInfo dir = new System.IO.DirectoryInfo(To_Directory.Text.ToString()); if (dir.Exists) { FileInfo[] names = dir.GetFiles(); for (int i = 0; i < names.Length; i++) { Label l = new Label(); l.ID = "l" + i.ToString(); l.Text = Path.GetFileName(names[i].ToString()); l.Style.Add("font-family", " Arial, Helvetica, sans-serif"); l.Style.Add("font-size", "11px"); l.Style.Add("color", "#333333"); l.Style.Add("text-decoration", "none"); this.Controls.Add(l); Button b = new Button(); b.ID = "button" + i.ToString(); b.Text = "Delete..."; b.Style.Add("font-family", " Arial, Helvetica, sans-serif"); b.Style.Add("font-size", "11px"); b.Style.Add("color", "#333333"); b.Style.Add("text-decoration", "none"); b.Click += delegate(object sender, EventArgs e) { _bDelete_Click(sender, e, 0); }; this.Controls.Add(b); Controls.Add(new LiteralControl("<br>")); } } } } } }Composite Control Creating custom controls
andreaban
Member
1 Points
7 Posts
Re: How to keep value in control after postback? ViewState?
Mar 03, 2009 08:36 PM|LINK
Would ChildControlsCreated = False; solve my problem? Has anyone got any samples that I could look at? Thanks.
yani
Member
104 Points
125 Posts
Re: How to keep value in control after postback? ViewState?
Mar 03, 2009 09:14 PM|LINK
andreaban
Member
1 Points
7 Posts
Re: How to keep value in control after postback? ViewState?
Mar 03, 2009 09:25 PM|LINK
Not sure if I got that, would you have an example? - sorry, but here is another explanation to this problem:
Then if the button (any of them) gets pressed again the cycle starts again - only this time in the createchildcontrols, I don't have my text for the ToDirectory set anymore because the loadViewstate happens after this method. But I need my value here so that I can create the extra controls I need.
I hope this make sense. Many Thanks.
Allen Chen –...
All-Star
40924 Points
4944 Posts
Re: How to keep value in control after postback? ViewState?
Mar 05, 2009 02:52 AM|LINK
Hi,
It seems To_Directory is not added to the page. Where do you use this control?
Allen Chen
Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
andreaban
Member
1 Points
7 Posts
Re: How to keep value in control after postback? ViewState?
Apr 05, 2009 06:13 PM|LINK
Thank, it's been resolved since thanks anyway!