页面生命周期并且在尝试以编程方式创建控件时无法显示 page_init 之外的控件
我正在创建一个页面,用户可以将文件上传到网络服务器。上传后,页面将有一个指向刚刚上传的文件以及已上传的任何其他文件的链接。
由于我正在以编程方式创建指向已上传文件的链接,因此我必须在 page_init 中执行此操作,否则单击链接按钮时不会触发其事件。我的网页完成了这一切 - 它创建了链接按钮,当我单击它们时,它调用所需的事件方法,即下载文件的子方法。
好的,我遇到的问题是:当我单击上传(上传文件)时 - page_init 子被调用,将所有以前上传的文件显示为链接按钮。然后我的 btnUpload_click 子函数被调用,它上传我当前的文件。
唯一的问题是当前文件尚未显示?我只能在page_init中显示链接,但是因为btnUpload是在page_init之后调用的,所以当前文件直到page_init之后才上传,因此不显示?
有什么想法可以解决这个问题吗?
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 required 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将所有服务器端链接的列表作为类的成员:
List; myLinks = new List();
构建链接时,不要将它们添加到页面,而是将它们添加到列表中:
myLinks.Add(oNewLink);< /code>
在 btnUpload_Click 方法中,使用正确的值将新链接添加到全局列表。
在 Page_PreRender 函数中添加到页面的链接,这在单击按钮后发生。
如果您需要进一步帮助实现此逻辑,请告诉我。 :)
Have list of all the server side links as member of your class:
List<LinkButton> myLinks = new List<LinkButton>();
When you build the links don't add them to the page yet, add them to the List instead:
myLinks.Add(oNewLink);
In the btnUpload_Click method, add new link to the global list with the proper values.
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. :)
如果您要从会话变量或数据库获取/保存先前上传的文件列表,则在 btnUpload_click 事件结束时,您可以简单地将页面重定向到其本身。像 Response.Redirect("PageName.aspx");
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");