开发者

page lifecycle and not being able to display controls outside of page_init when trying to create them programatically

开发者 https://www.devze.com 2023-01-27 02:20 出处:网络
I\'m creating a page that users can upload a file to the webserver. After upload the page will then have a link to the file that has just been uploaded, along with any other files that have already be

I'm creating a page that users can upload a file to the webserver. After upload the page will then have a link to the file that has just been uploaded, along with any other files that have already been uploaded.

As I am programatcially creating links to the files which have been uploaded, I have to do this in page_init or else the link button won't fire off it's event when clicked. MY web page does all this - it creates the link buttons and when I click on them, it calls the event method requi开发者_如何学JAVAred i.e. a sub to download the file.

OK, the problem I've come accross is: when I click upload (to upload the file) - the page_init sub is called, displaying all the previously uploaded files as link buttons. Then my btnUpload_click sub is called, which uploads my current file.

The only prob is the current file hasn't been displayed? I can only display links in the page_init, but because btnUpload is called after the page_init, the current file isn't uploaded until after page_init and therefore not dislayed?

Any ideas how to get around this?


  1. Have list of all the server side links as member of your class: List<LinkButton> myLinks = new List<LinkButton>();

  2. When you build the links don't add them to the page yet, add them to the List instead: myLinks.Add(oNewLink);

  3. In the btnUpload_Click method, add new link to the global list with the proper values.

  4. Add the links to the page in the Page_PreRender function, which happens after the button click.

If you need further help implementing this logic let me know. :)


if you are getting/saving the list of previously uploaded files from Session variable or Database, then at the end of btnUpload_click event, you can simply redirect the page to itself. Like Response.Redirect("PageName.aspx");

0

精彩评论

暂无评论...
验证码 换一张
取 消