我正在开发一个带有上传控件的页面。该控件相当复杂,并且在上传文件时不会回发。当不在带有 runat="server"
的表单标签中时它工作得很好,因为它有自己的表单,带有将发布到 ashx 文件的 post 操作。
问题是我现在想将此控件放在通过 runat="server"
工作的其他 asp.net 控件中的一个页面中。这意味着它周围的所有控件都需要位于带有 runat="server"
的表单标记内。
这是控件工作的表单:
<form action="MyFileTransfer.ashx" method="post" enctype="multipart/form-data">
// buttons etc.
</form>
虽然如果我在母版页中放入
包装其整个内容,我会遇到问题。
解决这个问题的最佳方法是什么?您有什么建议?
I'm developing a page that will have an upload control on it. The control is quite complex and doesn't postback whilst uploading files. It works perfectly fine when not in a form tag with runat="server"
as it has it's own form with a post action that will post to a ashx file.
The problem is that I now want to put this control within a page amongst other asp.net controls that work via runat="server"
. This means that all the controls around it will need to be within a form tag with runat="server"
.
Here is the form that the control works within :
<form action="MyFileTransfer.ashx" method="post" enctype="multipart/form-data">
// buttons etc.
</form>
Althought if I put in in my master page with <form runat="server"></form>
wrapping it's entire content, I get issues.
What is the best way to approach this and what do you recommend?
发布评论
评论(4)
我不确定我是否理解到底是什么触发了上传,但假设某个按钮正在执行表单提交。通常,这是所有控件都打开的页面,但您希望将其发布到
MyFileTransfer.ashx
。因此,一种解决方案是以下步骤,您必须使用 JavaScript 在客户端执行这些步骤。
动态创建不可见的 IFRAME
将表单的目标设置为 IFRAME 的 ID
存储旧表单操作
将表单的操作设置为
MyFileTransfer.ashx
提交表单
从表单中删除目标
恢复表单的旧操作。
>
对于您的场景,上述步骤可能过于简单,但适用于一般情况。希望这能让您朝着解决问题的正确方向前进。
顺便提一句。我认为您已经在使用不允许的嵌套表单。 FORM 元素不能嵌套,某些浏览器会让您这样做,但这并不意味着您应该这样做。
I am not sure if I understand what exactly triggers the upload, but let's say some button is doing the form submit. Normally this is to the page where all of your controls are on, but you want it to post to
MyFileTransfer.ashx
.So one solution would be the following steps, which you will have to perform on the clientside with JavaScript.
Dynamically create a invisible IFRAME
Set the target of the form to the ID of the IFRAME
Store the old form action
Set the action of the form to
MyFileTransfer.ashx
Submit form
Remove target from form
Restore the old action of the form.
The above steps could be to simple for your scenario but works in the general case. Hope this puts you in the right direction of solving your problem.
BTW. I presume you're already using nested FORMS which isn't allowed. A FORM element can't be nested, some browser will let you do it but that doesn't mean you should.
我认为你可以将 2 个表格放在一页上。只要只有一个 runat="server" 就不会出现错误
以下文章描述了更多详细信息: ASP.NET 中基于表单的编程。
I think you can put 2 forms on one page. There is no error as long as only one has runat="server"
Following article describe more details: Form-based Programming in ASP.NET.
我不认为您对此很幸运,因为以太重定向在您的情况下无法工作,以太您无法动态更改表单的参数,因为回发已准备好上传。 此外,您不能对所有发布数据使用相同的编码类型,因为您在此处尝试上传文件。
一个可能的解决方案是在 iframe 内添加上传控件。并且由于该控件在更新后可能会发生变化,因此您可以编写一个脚本,在回发后自动更新 iframe 的高度和宽度。
相对于更改 iframe 高度:
使用主页侧边栏滚动 iframe < /一>
<一href="https://stackoverflow.com/questions/9162933/make-iframe-height-dynamic-based-on-content-inside-jquery-javascript/9163087#9163087">根据内部内容使 iframe 高度动态 - JQUERY /Javascript
顺便说一句,iframe 也不错,其他类似的控件使用 iframe 内部的 flash load 来进行上传。您还可以使用内部对话框来打开此 iframe,如本示例中的 iframe 内容 http://highslide.com/ #例子
I do not that you have luck with this one because ether the redirect can not work in your case, ether you cant change the parameters of the form on fly because the post back is all ready done for the upload. Also you can not use the same enctype for all post data because here you try to upload files.
One Possible solution is to add your upload control inside an iframe. And because the control can change after the update, you make a script that update the height and width of the iframe after the post back automatically.
Relative to change the iframe height:
Scrolling iframe with main page side bar
make iframe height dynamic based on content inside- JQUERY/Javascript
By the way, the iframe is not bad idea, other similar controls use flash load inside iframe to make the upload. You can also do it with inside dialog for open this iframe like the iframe content in this examples http://highslide.com/#examples
最好在上传过程中使用ajax更新面板
Better to use ajax update panel around the upload