2012-12-28 32 views
0

我在UpdatePanel中有一個FileUpload。updateupnel的部分回發之後,fileupload的FileName是否爲空?

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always"> 
<Triggers> 
    <asp:PostBackTrigger ControlID="SaveButton" /> 
</Triggers> 
<ContentTemplate> 
<cc1:TabContainer CssClass="visoft__tab_xpie7" runat="server" ID="tab" ActiveTabIndex="0" Width="100%" Font-Size="11px"> 
<cc1:TabPanel runat="server" HeaderText="اطلاعات پایه" ID="TabPanel1"> 
    <ContentTemplate> 
     <div class="row"> 
     <span style="width: 100px" class="rowtitle">تصویر </span> 
     <asp:FileUpload ID="ImageFileUpload" runat="server"/> 
     <asp:ImageButton ID="RemoveImageButton" runat="server" ImageUrl="~/images/delete.png" /> 
     </div> 
     <div class="row"> 
     <span style="width: 100px" class="rowtitle">Category</span> 
     <asp:DropDownList ID="CategoryDropDownList" runat="server" Width="200px" AutoPostBack="True" DataTextField="Name" DataValueField="ID" OnSelectedIndexChanged="CategoryDropDownList_SelectedIndexChanged" /> 
     <asp:RequiredFieldValidator ID="CategoryRFV" runat="server" ControlToValidate="CategoryDropDownList" ForeColor="Red" ValidationGroup="Save" ErrorMessage="مجموعه را انتخاب کنید." Text="*" /> 
     </div> 
     <div class="row" style="height: 300px"> 
     <span style="width: 100px" class="rowtitle">توضیحات کامل</span> 
     <editor:HtmlEditor Style="float: left" runat="server" Width="600px" ID="DescriptionHtmlEditor" /> 
     <asp:RequiredFieldValidator Text="*" ID="DescriptionRFV" runat="server" ControlToValidate="DescriptionHtmlEditor" ForeColor="Red" ValidationGroup="Save" ErrorMessage="توضیحات را وارد کنید ." /> 
     </div> 
    </ContentTemplate> 
    </cc1:TabPanel> 
    </cc1:TabContainer> 
    <div class="row" style="text-align: center"> 
    <asp:Button ID="SaveButton" class="button" Width="100px" OnClick="SaveButton_Click" runat="server" ValidationGroup="Save" Text="ذخیره" /> 
    </div> 
    </ContentTemplate> 
    </asp:UpdatePanel> 

當我試圖在UpdatePanel的局部回傳後訪問的FileUpload FileName屬性,它是空的。

我想在onc​​ange事件中獲取uploadpanel的Filenme javascrip,但我沒有得到文件的Fullpath。

function FileChange() 
    { 
    var filename = document.getElementById('<%= FileNameUpload.ClientID %>'); 
    var file = document.getElementById('fileupload'); 
    filename.value = file.value; 
    } 

如何在部分回發後獲取fileupload的文件名?

回答

0

當我試圖訪問 部分回發updatepanel後FileUpload FileName屬性,它是空的。

更新面板簡單地攔截常規形式提交,並且使用XMLHTTP請求執行FULL回發。因此,最初在input:file元素上進行的選擇已在初始回發中提交,之後您將無法再讀取它。爲了證明我在說什麼,請嘗試在最初的回傳中讀取文件名,並且您會看到您將能夠。

看着你的代碼,它似乎也嘗試以編程方式設置fileupload元素的值。由於安全原因,這是不可能的 - 你不能從Javascript訪問用戶的硬盤,並決定用戶應該上傳到你的網站的文件。一旦用戶選擇了文件並提交了表單,選擇就會消失;用戶將不得不自己重新選擇文件。

你可以嘗試很多黑客來使UpdatePanels工作,但從長遠來看最清潔的是使用jQuery Ajax來實現這一點,因爲你完全控制了什麼被提交,什麼不是和什麼時候。

我希望這有助於澄清爲什麼你的方法將無法正常工作。

相關問題