0

In my codebehind, I set the display property of an asp.net control to none as follows;

fuDoc.Attributes("style") = "display:none;" 'fuDoc is a FileUpload control. 

On my page, if the user wants to upload a document, they click on a link, which by using jQuery, I set the display property to 'block' by using .show() as follows;

$('#fuDoc').show();

I also tried;

$('#fuDoc').attr('style', 'display:block;');

now, on form submit, I need to check if the fuDoc is visible and if so, do the standard file upload process e.g. check .HasFile etc..

However, although the fuDoc control is set to display:block in HTML with no problem, the following asp.net code always produces True

If fuDoc.Attributes("style") = "display:none;" Then 'always results to true

What is the reason for this? Any help would be appreciated.

Note: the FileUpload control is NOT created dynamically.

2 Answers 2

2

The problem is that style attributes changes on the client are not persisted during a postback.

One possible fix would be use a hiddenfield to store the state of the fuDoc control.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you codrin, voted up for being first, but had to accept the other since it includes example and might be helpful for others having similar problems.
1

Rather than relying on detecting whether or not the FileUpload is visible on the page, you could include a TextBox or HiddenField such as:

<asp:TextBox id="txtIsFileUploadShown" runat="server" cssclass="txtisfileuploadshown" Text="0" style="display:none;" />

This control will be hidden on your page, storing the value of 0 by default.

Whenever you hide/show the file upload, change your code to also change this value:

$('#fuDoc').show();
$(".txtisfileuploadshown").val('1');

When hiding:

$('#fuDoc').hide();
$(".txtisfileuploadshown").val('0');

Then in your code-behind on post back, simply check:

If IsNumeric(txtisfileuploadshown.text) AndAlso Cint(txtisfileuploadshown.text)=1 Then
   'Do HasFile validation here.
End

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.