ASP.NET中控件分为2种,一种是服务器控件,一种的普通的HTML控件,对这2种控件的隐藏与显示的方法是不一样的。
HTML控件:
document.getElementById('控件ID').style.display="none"; //隐藏
document.getElementById('控件ID').style.display=""; //显示
服务器控件:
document.getElementById('').style.display="none"; //隐藏
document.getElementById('').style.display=""; //显示
上面的方法控件隐藏后不再占用页面空间。
下面的方法可以将控件隐藏后在页面上保留控件占用的空间。
document.all['控件ID'].style.visibility="hidden";
document.all['控件ID'].style.visibility="";
如果做成控件的号,控件ID前一定要加上"控件名" + "1_"
否则会提示缺少对象的.
例:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ListSearch.ascx.cs" Inherits="ListSearch" %>
<script language="javascript">
function check(){
var r1 = document.getElementById("Radio1");
if(r1.checked)
{
//alert("Yes");
document.getElementById("ListSearch1_DropDownList2").style.display="";
}
else
{
//alert("No");
document.getElementById("ListSearch1_DropDownList2").style.display="none";
}
}
</script>
<asp:TextBox ID="TextBox1" runat="server" Width="80px"></asp:TextBox>
<input id="Radio1" type="radio" name="RadioGroup" CHECKED οnclick="return check()"/>商品
<input id="Radio2" type="radio" name="RadioGroup" οnclick="return check()"/>用户
<input id="Radio3" type="radio" name="RadioGroup" οnclick="return check()"/>店铺
<asp:DropDownList ID="DropDownList2" runat="server" Width="80px">
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
Text="搜索" />