7

I have an ASP.NET text control "FromDate" whose visible property is set to false, but I wanted a client side javascript to be able to toggle the visibility property using CSS properties

element1.style.display = "none"; // hides the element
element1.style.display = ""; // shows the element

but when I attempt to get the textbox, I get null on

  var element1 = document.getElementById("FromDate");

When I try the same code with visble=true as the default on the "FromDate" ASP.NET control, it works (although that is not the behavior I need)

Any ideas?

3 Answers 3

22

When you set Visible = false to a control, it is not rendered. That means there is no HTML representation of that control sent to the page. Set the style only.

You can set the style as display: none from server side code like this:

FromDate.Style.Add(HtmlTextWriterStyle.Display, "none")
Sign up to request clarification or add additional context in comments.

6 Comments

Can events be called when visible is false? Asked for security concern.
@Ufuk: What kind of events? JavaScript events cannot be called because there is nothing on the client. Server side events can still be called (Although the control may have been written such that the events wont be raised if its Visible property is set to false).
Server side events like button click. If somebody messes with postback, can they trigger those events?
I've never tried it. Maybe EventValidation would stop it. Give it a try.
Important question: can a hacker change the control's value if visible=false?
|
14

If you want to hide this control, you can try CSS like this:

<asp:somecontrol id="FromDate" style="display:none" />

I think hiding the control with CSS is easier to understand.

Comments

3

Instead of setting Visible=false, set its style.display to none, that way the element is still there for JavaScript to manipulate.

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.