2012-04-25 14 views
0

FileUploadボタンのテキストを変更する方法がないため、FileUploadコントロールを非表示にして、自分の偽のボタンとテキストボックスを使用してそれを行うことができます。IEでサブミットした後に望ましくないFileUploadテキストボックスがクリアされる

<script language="JavaScript" type="text/javascript"> 
    function HandleFileButtonClick(sender) { 
    var parentDiv = $(sender).parent(); 
    $(parentDiv).children('[id*=fuBoutiqueImage]').click(); 
    } 
    function LoadFakeField(sender) { 
    $(sender).parent().find("input[id$='txtFakeText']").val($(sender).val()); 
    } 
</script> 

<asp:Panel ID="pnlCommandButtons" runat="server" CssClass="commandButtons"> 
<div class="uploader"> 
    <asp:Label ID="lblUploadFile" EnableViewState="false" runat="server" Text="<%$Resources:Common, BoutiqueGallery_UploadFile %>" /> 
    <asp:FileUpload ID="fuBoutiqueImage" runat="server" style="" onchange="LoadFakeField(this);" /> 

    <input ID="txtFakeText" type="text" name="txtFakeText" readonly="true" runat="server" /> 
    <input ID="btnFakeButton" type="button" onclick="HandleFileButtonClick(this);" value="<% $Resources:Common, ButtonName_Browse %>" runat="server" /> 
</div> 

<asp:Panel ID="pnlDeleteButton" class="delete" runat="server"> 
    <ef:ButtonExt ID="btnDelete" runat="server" Text="<%$Resources:Common, BoutiqueGallery_Delete %>" OnClick="btnDelete_Click" CausesValidation="false" Color="Red" Icon="Delete" Width="60" /> 
</asp:Panel> 
<div id="pnlAddButton" class="add"> 
    <ef:ButtonExt ID="btnAdd" runat="server" Text="<%$Resources:Common, UploadImage %>" OnClick="btnAdd_Click" ValidationGroup="emailSend" Width="104" /> 
    <asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="fuBoutiqueImage" ErrorMessage="<%$Resources:Common, Attachment_FileToLarge %>" Text="<%$Resources:Common, Attachment_FileToLargeTextBox %>" Display="Dynamic" OnServerValidate="CustomValidator1_ServerValidate" ValidationGroup="emailSend"></asp:CustomValidator> 
<asp:RegularExpressionValidator ID="FileUpLoadValidator" runat="server" ErrorMessage="<%$Resources:Common, Attachment_FileFormat %>" Text="<%$Resources:Common, Attachment_FileFormatTextbox %>" ValidationExpression=".*(\.jpg|\.png|\.gif|\.tif|\.jpeg|\.JPG|\.JPEG|\.PNG|\.GIF|\.TIF)$" ControlToValidate="fuBoutiqueImage" Display="Dynamic" ValidationGroup="emailSend" /> 
</div> 
<div class="isActiveCheckbox"> 
<asp:CheckBox ID="cbImageIsActive" class="chkItem" OnCheckedChanged="cbImageIsActive_CheckedChanged" Checked='<%# Eval("IsActive") %>' AutoPostBack="true" runat="server" Text="<%$Resources:Common, BoutiqueGallery_IsImageActive %>" /> 
</div> 
</asp:Panel> 

私btnFakeButtonはのFileUploadのクリックアクションをトリガし、その後パス/ファイル名は、偽のテキストボックスにコピーされます。それから私はbtnAddをクリックすることができ、すべてがffでは正常に動作しますが、IEでは正常に動作しません。

ファイルを選択してダイアログボックスを閉じると、パス/ファイル名がコピーされますが、btnAdd(またはチェックボックスをクリック)を押すと、FileUploadテキストボックスが消去され、何も起こりません。 btnAddを2回押すと、btnAdd_Clickは開始されますが、FileUploadは空であり、エラーで終了します。私はtxtFakeTextからFileUploadテキストボックスを復元できないので、FileUploadのクリアを防ぐ方法はありますか?

答えて

0

これは私がやった方法ですが、他のブラウザではこれをテストしていないので、IE用に設計する必要があります。また、選択したファイル名を表示するために、テキストボックスの代わりにラベルを使用しました。私は正しい道に私を開始するためにこのサイトへの信用を与える必要があり

http://www.codeproject.com/Tips/296593/To-trigger-Choose-file-to-Upload-dialogue-box-with

を基本的に、私がやったことは、あなたが行っているように、2つのボタンを作成しました。私はCSSを使って、ファイルアップロードコントロールボタンを私の「偽の」ボタンと同じサイズにしました。次に、私の "偽の"ボタンのZ-インデックスを-1に設定し、ファイルアップロードコントロールボタンのすぐ後ろに配置しました。次に、ファイルアップロードコントロールボタンの不透明度を0に設定します。この方法では、「偽の」ボタンは使用されず、サブミットをクリックすると実際のファイルアップロードボックスがクリアされるという問題にはなりません。リンクされた記事で詳しく説明されていない最後のステップは、ファイルアップロードボタンの "onchange"をファイル名ラベルの値を更新する関数に変更することです。ここで

はコードです:

function updateUploadLabel() { 
     document.getElementById("<%= FileUpload1.ClientID %>").click(); 
     document.getElementById("<%= lblFileName.ClientID %>").innerHTML = document.getElementById("<%= FileUpload1.ClientID %>").value; 
     document.getElementById("txtInstructions").disabled = "true"; 
     document.getElementById("removeUpload").style.display = 'inline'; 
    } 
<asp:FileUpload id="FileUpload1" onchange="updateUploadLabel()" runat="server" style="position: relative; opacity: 0; filter: alpha(opacity = 0);left:0px;font-size:24px; z-index:50; width:100px;/*display:none;*/"/> 
<input type="button" id="btnChooseDoc" value="Choose File..." onclick="updateUploadLabel()" style="height:30px; width:100px; z-index: -1; position:relative; left: -105px; top: -10px;" /> 
<asp:Label id="lblFileName" runat="server" text='No File Chosen' style="position:relative; left: -105px; top: -10px;"></asp:Label> 
関連する問題