1

So I have a search textbox at the top of my website in the Master Page that I wish to disappear when the user is transferred to my search page. My textbox is written like so:

  <div class = "SearchBox">
    <form action="javascript:searchSite()">
      <input type="text" id="searchIn" />
    </form>
  </div>

The best way I could think to do this was to have some JavaScript run on the PageLoad event of my search page, like so:

protected void Page_Load(object sender, EventArgs e)
    {
       if (!IsPostBack)
        {
            this.ClientScript.RegisterStartupScript(this.GetType(), "show", "<script>document.getElementById('searchIn').style.display = 'none'</script>");
        }
   }

I am fairly certain that the javascript works, because sometimes the textbox will disappear for a second or two. Regardless, it immediately comes back and won't remain hidden. I have a asp:Textbox that I can easily hide using:

  Site1 m = Master as Site1;
        m.OtherTextBox.Visible = false;

I don't understand why hiding the HTML textbox is so difficult. Any suggestions or thoughts on how to remedy this would be much appreciated!

4

2 回答 2

1

display: hidden不是有效的 CSS 值。

你想要display: none

于 2013-10-07T15:59:31.220 回答
1

Page_Load是服务器端事件,但您还必须等待元素在客户端加载。您可以将 js 代码包装在window.onload处理程序中:

this.ClientScript.RegisterStartupScript(this.GetType(), "show", "<script>window.onload = function() { document.getElementById('searchIn').style.display = 'none'; }</script>");

此外,display: none按照 SLaks 的说明使用。

于 2013-10-07T16:10:26.000 回答