0

i need the users of my web system to do uploads ( up 4 ).

the upload itself its working , but i dont want all for box appering at start, so tried to hide the 2nd ,3rd and 4th upload boxes until the user need them .

here the aspx code part

<fieldset>
    <div class="frm tam">
    </div>
    <div class="lin">
        <asp:FileUpload ID="FileUpload1" runat="server" /></div>
    <div class="lin">
        <asp:FileUpload ID="FileUpload2" runat="server" Visible="false" /></div>
    <div class="lin">
        <asp:FileUpload ID="FileUpload3" runat="server" Visible="false" /></div>
    <div class="lin">
        <asp:FileUpload ID="FileUpload4" runat="server" Visible="false" /></div>
    <div class="lin">
        <asp:Button ID="btnUpload" runat="server" Text="Upload"OnClick="btnUpload_Click" />
        <asp:Button ID="btnAdd" runat="server" Text="ADD File" OnClick="btnAdd_Click" />
        <asp:Button ID="btnRem" runat="server" Text="Remove File" OnClick="btnRem_Click" />
    </div>
</fieldset>

and the code behind to show and hide the boxes

protected void btnAdd_Click(object sender, EventArgs e)
{
    if (FileUpload2.Visible == false)
    {
        FileUpload2.Visible = true;
    }
    else if (FileUpload3.Visible == false)
    {
        FileUpload3.Visible = true;
    }
    else if (FileUpload4.Visible == false)
    {
        FileUpload4.Visible = true;
    }
}

protected void btnRem_Click(object sender, EventArgs e)
{
    if (FileUpload4.Visible == true)
    {
        FileUpload4.Visible = false;
    }
    else if (FileUpload3.Visible == true)
    {
        FileUpload3.Visible = false;
    }
    else if (FileUpload2.Visible == true)
    {
        FileUpload2.Visible = false;
    }
}

and when i click on Add File, the new box appear but the filepaths selected on the other boxes are cleared. Can i avoid this clear ?

*edit: i´m using net framework 4.0 .

4

2 回答 2

0

该文件在回发时丢失。或者,您可以使用 JavaScript 来实现

<div id="div1">
   <asp:FileUpload ID="FileUpload1" runat="server" />
</div>

<div id="div2" style="display:none">   <!-- visibility:hidden -->
   <asp:FileUpload ID="FileUpload2" runat="server" />
</div>

<div id="div3" style="display:none">
   <asp:FileUpload ID="FileUpload3" runat="server" />
</div>

<div id="div4" style="display:none">
   <asp:FileUpload ID="FileUpload4" runat="server" />
</div>

然后将每个 FileUpload 控件放在每个 div 中。

<input type="button" valud="Add" onclick="addControls()" />

然后使用 JavaScript 使它们可见

function addControls()
{
   document.getElementById('div2').style.display = 'inline-block';
   //$('#div2').show(); <--- JQuery
}
于 2013-04-24T13:57:07.777 回答
0

这里的问题是上传文件的工作方式与大多数输入控件略有不同......

它不是发布输入的文件路径值,而是实际发布文件内容,因此在回发时该值会丢失。

在我看来,您最好的选择是在客户端上使用 javascript 来显示/隐藏这些上传控件。这也将是更好的用户体验,因为用户不必每次都等待回发。

你可以用jQuery做到这一点......

http://jsfiddle.net/BAwfH/2/

<fieldset>
<div class="frm tam">
</div>
<div class="lin">
    <asp:FileUpload ID="FileUpload1" runat="server" />
</div>
<div class="lin">
    <asp:FileUpload ID="FileUpload2" runat="server" />
</div>
<div class="lin">
    <asp:FileUpload ID="FileUpload3" runat="server" />
</div>
<div class="lin">
    <asp:FileUpload ID="FileUpload4" runat="server" />
</div>
<div>
    <asp:Button ID="btnUpload" runat="server" Text="Upload"OnClick="btnUpload_Click" />
    <asp:Button ID="btnAdd" class="add-button" runat="server" Text="ADD File" OnClick="btnAdd_Click" />
    <asp:Button ID="btnRem" runat="server" Text="Remove File" OnClick="btnRem_Click" />
</div>

<script type="text/javascript">
$(document).ready(function(){
var lines = $('.lin');
lines.hide();
lines.filter(':first').show();

$('.add-button').click(function(){
    lines.filter(':hidden:first').show();

    if(lines.filter(':hidden').length == 0)
        $(this).hide();
});

});

于 2013-04-24T13:53:04.430 回答