Unwanted FileUpload textbox clearing after submit in IE - c#

Since there is no way to change FileUpload button text I hide FileUpload control and used my own fake button and textbox to be able to do it.
<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>
My btnFakeButton triggers FileUpload click action and after that the path/fileName is copied to fake textbox. Then I can click btnAdd and everything works fine in ff but not in IE.
In IE after choosing a file and closing dialog box the path/fileName is copied but when I press btnAdd (or click checkbox) the FileUpload textbox is cleared and nothing happens. After second press of btnAdd, btnAdd_Click starts but FileUpload is empty and it ends with error. Since I can't restore FileUpload textbox from txtFakeText is there any way to prevent FileUpload clearing?

This is how I did it, although I only have to design for IE so I haven't tested this in other browsers. Also, I used a label instead of a textbox to display the selected filename.
I have to give credit to this site for starting me on the right path: http://www.codeproject.com/Tips/296593/To-trigger-Choose-file-to-Upload-dialogue-box-with
Essentially, what I did was create two buttons, as you have done. I used CSS to make the file upload control button the same size as my "fake" button. Then, I set the z-index of my "fake" button to -1, and positioned it directly behind the file upload control button. Then, I set the opacity of the file upload control button to 0. This way, the "fake" button is not used and I do not run into the problem of the real file upload box being cleared when clicking submit. The last step, not detailed in the linked article, is to change the "onchange" for the file upload button to be a function that updates the value of the filename label.
Here is the code:
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>

Related

Textbox gets validated by two buttons

I am making some web app using ASP.net and for look of the site I am using Bootstrap. But I have this problem that I can't solve by googling
So this is the deal .. I have panel and inside I have simple textbox and button
<asp:Panel runat="server" ID="pnlCompany">
<div style="display: inline-flex; overflow: hidden; float: left">
<asp:TextBox ID="txtClient" runat="server" CssClass="text-primary"
Wrap="true" Width="300px" required=""></asp:TextBox>
<asp:Button ID="btnInsertClient" runat="server" OnClick="btnInsertClient_Click"
CssClass="btn btn-primary" data-target="#MainContent_pnlCompany_txtClient"
Text="Dodaj klijenta" />
</div>
</asp:Panel>
But my problem show up here ... Under that panel I have another panel, with Button (that works fine, it collapsing table at end of page), then I have FileUpload button and Button (btnUploadUsers) that is giving me hard time, idea of this button is to go with upload of file, but once i click it, it keeps validating textbox (txtClient) from above panel ... I am sure this button fire it's event "btn_UploadUsers_click" because once i enter data in textbox, program stop on my break point
<asp:Panel runat="server" ID="pnlImportUsers">
<div style="display: inline-flex; overflow: hidden; float: left">
<asp:Button ID="btnCompanyList" runat="server" class="btn btn-lg btn-primary"
data-target="#MainContent_pnlCompanyList" Text="Lista klijenata" />
<asp:FileUpload ID="fuUsersUpload" runat="server" Enabled="false"
CssClass="btn-group-vertical" />
<asp:Button ID="btnUploadUsers" runat="server" class="btn btn-lg btn-primary"
Text="Upload" OnClick="btnUploadUsers_Click" data-target="#MainContent_UsersId"
ValidationGroup="fuUsersUpload" />
</div>
</asp:Panel>
Let me know if I can do anything to describe this problem better, thank you
Thank you #Benni_mac_b for advice, but i figured out what was a problem, so I'll post answer in case anyone else be in this situation.
So with using Bootstrap css and js, as you can see 'required' on the form, it will always be check if has value on every post_back
<asp:TextBox ID="txtClient" runat="server" CssClass="text-primary"
Wrap="true" Width="300px" required=""></asp:TextBox>
So, as I didn't wanted my btnUploadUsers to do PostBack, simply adding
UseSubmitBehavior="false"
In my code snippet fixed my problem

Execute server side code on ModalPopupExtender okcontrolid clicked

I am using ASP.NET and C#. I want to popup this little screen, then when the OK button is clicked, I want to update the main screen based on the input to the popup. Sounds like it should be a regular thing. Is it possible, and if so, how?
<cc1:modalpopupextender id="ModalPopupExtender1" runat="server"
cancelcontrolid="btnCancel" okcontrolid="btnOkay"
targetcontrolid="txtCosCodeExpCode" popupcontrolid="Panel1"
popupdraghandlecontrolid="PopupHeader" drag="true"
backgroundcssclass="ModalPopupBG">
</cc1:modalpopupextender>
<asp:panel id="Panel1" style="display: none" runat="server">
<div class="CostCentreExpenseCodePopup" style="background-color:White ; border-style :solid;">
<div class="PopupHeader" id="PopupHeader">Select Cost Centre / Expense Code</div>
<div class="PopupBody">
<p>Cost Centre<asp:DropDownList
ID="ddlCostCentres1"
runat="server"
CssClass="SVSComboBox1"
AppendDataBoundItems ="True"
AutoPostBack="True"
style = "width :152px;"
OnSelectedIndexChanged="ddlCostCentres1_SelectedIndexChanged">
<asp:ListItem Text="Please select" Value="0"></asp:ListItem>
</asp:DropDownList></p>
<p>Expense Code <asp:DropDownList ID="ddlExpCode1" runat="server" CssClass="SVSComboBox1" style = "width :152px;"
AppendDataBoundItems ="True" Enabled="False" Visible ="False">
<asp:ListItem Text="Please select" Value="0"></asp:ListItem>
</asp:DropDownList></p>
</div>
<div class="Controls">
<input id="btnOkay" type="button" value="Done" />
<input id="btnCancel" type="button" value="Cancel" />
</div>
</div>
Don't set OkControlID but use ModalPopupExtender1.Hide() on server-side. Then you're able to call your update-code first.
You should also use server-buttons instead of <input id="btnOkay" type="button" value="Done" /> and handle their click-event.
First of all add the asp:Button as an Ok button and add click event in your aspx.cs page as mentioned below:
ASPX:
<asp:Button runat="server" ID="btnOkay" Text="OK" OnClick="btnOkay_Click"/>
ASPX.cs
protected void btnOkay_Click(object sender, EventArgs e)
{
// Code to write on ok click event
}
And then remove okcontrolid="btnOkay" from your modal popup extender.

Make User Control Button as default on Enter key press [duplicate]

I am trying set a button on a user control as defaultbutton on a page but its not working. Here is the code for my user control
<asp:Panel ID="pnlTopicPicker" runat="server" DefaultButton="btnSubmit">
<div class="PadBottom">
<div id="divTopic" class="FloatLeft PadDiv">
<div class="SectionHeader FloatLeft PadRightMediumSmall">Topic:</div>
<asp:DropDownList ID="ddlSelectedTopic" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlSelectedTopic_OnSelectedIndexChanged" >
</asp:DropDownList>
</div>
<div class="FloatLeft PadLeftMediumSmall">
<asp:Button ID="btnClear" runat="server" Text="Clear" OnClick="btnClear_Click" CssClass="DefaultButton" />
</div>
<div class="FloatRight PadRightMediumSmall">
<asp:Button ID="btnSubmit" runat="server" Text="View Report" OnClick="btnSubmit_Click" OnClientClick="return ViewReportSubmit(event);" CssClass="DefaultButton" />
</div>
</div>
</asp:Panel>
I am using the above control : TopicPicker in a page:
<asp:UpdatePanel ID="pnlFind" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<div class="PadLeft">
<Incapnet:TopicPicker ID="incTopicPicker" runat="server" OnClearClick="btnClear_Click" OnSubmitClick="btnSubmit_Click" OnSelectedTopicChanged="ddlSelectedTopic_Changed" />
</div>
<div class="ClearBoth" />
<div class="PadTop PadLeft" >
<asp:GridView ID="gvFindGrid" runat="server" AutoGenerateColumns="False" OnRowDataBound="gvFindGrid_RowDataBound"
CssClass="ContrastTable UFWideTable" GridLines="None">
<AlternatingRowStyle CssClass="AlternateRow" />
<RowStyle CssClass="Row" />
<HeaderStyle CssClass="HeaderRow" />
<EmptyDataTemplate>
No Records Found
</EmptyDataTemplate>
<Columns>
...
...
</ContentTemplate>
</asp:UpdatePanel>
Now when I hit enter, on the page, I want the "btnSubmit" to be executed, which is not happening.
How do I get this working.
Thanks,
under what circumstances are you expecting the btnSubmit Click event to fire?
I ask because the event will only be triggered if the Enter key is pressed when a control which implements the IButtonControl interface has focus.
This would include (among others) TextBox and DropDownList.
If you select your DropDownList and then hit enter it should fire the event. But just clicking any element (such as a DIV) and hitting enter would not.
Edit (Based on comment)
Its a bit of a hack but you can achieve what you ask using the following javascript (uses JQuery):
<script type="text/javascript">
$(document).keypress(function (e) {
var code = (e.keyCode ? e.keyCode : e.which);
if (code == 13) {
$('#<%= btnSubmit.ClientID %>').click();
}
});
</script>
Try adding the following to the button:
<asp:Button ID="Button1" runat="sever" UseSubmitBehavior="true" ... >
If that doesn't help, try adding this to your markup (for IE):
<!-- Fix for IE bug submit on pressing "Enter" -->
<div style="display:none">
<input type="text" name="hiddenText"/>
</div>
this.Page.Form.DefaultButton = this.btnLogin.UniqueID;

very simple c# submit button

I have several ImageButtons on the top of my page, then I have a textbox and button in the middle. If you type in the textbox then hit enter on the keyboard the browser follows the link of the first ImageButton instead of the submit button next to the textbox. I have ran into this in the pase and had to put the imagebuttons on the bottom of the page for it to work correctly, that is not an ok fix in this case. I tried setting UseSubmitBehavior="true" , but that does nothing. I tried putting the textbox and button in a separate DIV and a separate panel, didn't work either
TOP of the page
<div style="position:absolute; left: 70% ; top: 5%;">
<asp:ImageButton ID="imgFB" runat="server" ImageUrl="Images/facebook_icon.jpg" PostBackUrl="http://www.facebook.com/832586561" />
<asp:ImageButton ID="imgLI" runat="server" ImageUrl="Images/linkedin_logo.jpg" PostBackUrl="http://www.linkedin.com/pub/scott-selby/33/304/44a" />
<asp:ImageButton ID="imgCB" runat="server" ImageUrl="Images/careerbuilder_logo.jpg" PostBackUrl="http://www.careerbuilder.com" />
<asp:ImageButton ID="imgCP" runat="server" ImageUrl="Images/codeplex_logo.jpg" PostBackUrl="http://www.codeplex.com" />
</div>
Middle of Page
<div ID="formPanel" runat="server" style="position:absolute; top:235px;">
<asp:TextBox ID="txtNewCity" runat="server"></asp:TextBox>
<asp:Button ID="btnChangeCity" runat="server" Text="Change City" UseSubmitBehavior="true" />
</div>
You may set the default button via <form/> attribute.
<form defaultbutton="btnChangeCity" id="form1" runat="server">
...
</form>
Or use Panel control to set default button.
<asp:Panel ID="Panel1" runat="server" DefaultButton="btnChangeCity">
<asp:TextBox ID="txtNewCity" runat="server"></asp:TextBox>
<asp:Button ID="btnChangeCity" runat="server" Text="Change City" />
</asp:Panel>
You surround your controls with <div> which is a good idea, but to have it run at all you also need <form>. In case you just didn't include it, the submit button also has to have OnClick="sub" attribute.
Read more here.

Update Progress on processing after fileupload

I have a webform with two upload controls and a number of textfields.
When the button is pressed the files get uploaded and then get processed.
The upload takes no time, but the processing does.
I know I can't have an upload control in an update panel, so I can't work out how to use the update progress control to show my progress.
My page with an updateprogress control that does work is as follows:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:UpdateProgress ID="UpdateProgress1" runat="server" AssociatedUpdatePanelID="UpdatePanel1" DynamicLayout="true">
<ProgressTemplate>
<div class="LOADING">
Your data is being processed<br />
<br />
<img src="/images/loading.gif" /><br />
<br />
Please wait...
</div>
</ProgressTemplate>
</asp:UpdateProgress>
<div class="addFixture">
<asp:ValidationSummary ID="ValidationSummary1" ValidationGroup="fixture" runat="server" />
<label>
Type
<asp:DropDownList ID="ddlType" runat="server" AppendDataBoundItems="true">
<asp:ListItem Text=""></asp:ListItem>
</asp:DropDownList>
<label>
Date
</label>
<asp:TextBox ID="txtDate" runat="server"></asp:TextBox>
<label>
Name 1</label>
<asp:TextBox ID="txtName1" runat="server"></asp:TextBox>
<label>
Name 2
<asp:TextBox ID="txtName2" runat="server"></asp:TextBox>
<label>
First XML File</label>
<asp:FileUpload ID="firstFileUp" runat="server" />
<br />
<label>
Second Xml File</label>
<asp:FileUpload ID="secondFileUp" runat="server" />
<br />
<br />
<asp:Button ID="SubmitButton" runat="server" CausesValidation="true" Text="Submit" OnClick="SubmitButton_Click" />
<asp:Label ID="ErrorMessageLabel" runat="server" EnableTheming="false"></asp:Label>
</div>
</ContentTemplate>
</asp:UpdatePanel>
I have searched for this, but people seem to be trying to get the progress of the file upload and not the processing.
Can anyone help?
Remove the UpdatePanel as all the way you can't use FileUpload controls inside it.
Add to the SubmitButton OnClientClick property with following value: OnClientClick="showProgress()"
Also add onto the page javascript function below:
function showProgress() {
var updateProgress = $get("<%= UpdateProgress1.ClientID %>");
updateProgress.style.display = "block";
}
By the way, consider to use some async file upload control like one from the Ajax Control Toolkit library
You can use an UpdatePanel and the FileUpload if you use the ASyncFileUploadControl. It works pretty well. Make sure you download the latest version because there were a couple of issues with prior releases.

Categories