(1)通过语句控制控件显示:
1.1条件控制某个控件是否显示:
<% if(A>0) {%>
<input id='input1'></input>
<%}%>
1.2通过js控制:(下列代码直接放在body中可用。)
<script>
function factorial(n){
if (n<=1) return n;
else return n*factorial(n-1);
}
document.write("<table>")
document.write("<tr><th>n</th><th>n!</th></tr>");
for(var i=0;i<10;i++){
document.write("<tr><td>"+i+"</td><td>"+factorial(i)+"</td><tr>");
}
document.write("</table>");
document.write("产生时间 "+new Date());
</script>
(2)literal 控件使用:
2.1定义:同一般input控件。
<asp:Literal id='litMenu' runat='server'></asp Literal>
2.2使用:这样便能在服务器端对其赋值,赋值内容如下:
控件的巧妙用法,这里我把它做成了一个类似Menu控件,实现菜单的功能:
litMenu.Text =
"<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
<tr>
<td class=\"Tab1\" style=\"width:114px;\" οnclick=\"window.location.href='/onlineUserView.aspx?type=1';\">在线人员</td>
<td class=\"Tab2\" style=\"width:114px;\" οnclick=\"window.location.href='/SMS/SMSList.aspx?type=2';\">收件箱</td>
<td class=\"Tab1\" style=\"width:114px;\" οnclick=\"window.location.href='/SMS/SMSList.aspx?type=3';\">草稿箱</td>
<td class=\"Tab1\" style=\"width:114px;\" οnclick=\"window.location.href='/SMS/SMSList.aspx?type=4';\">已发送</td>
<td class=\"Tab1\" style=\"width:115px;\" οnclick=\"window.location.href='/SMS/SMSList.aspx?type=5';\">已删除</td>
</tr> </table>"
2.3服务端动态设置:
//2.3.1定义菜单数组的名称
public static string[] sName4 = new string[5] { "在线人员","收件箱", "草稿箱", "已发送", "已删除" };
public static string[] sUrl4 = new string[5] { "/onlineUserView.aspx?type=1", "/SMS/SMSList.aspx?type=2", "/SMS/SMSList.aspx?type=3", "/SMS/SMSList.aspx?type=4", "/SMS/SMSList.aspx?type=5" };
//2.3.2定义每个菜单对应的链接的数组
///<summary> 通用Tab </summary>
/// <param name="litMenu">Literal的ID</param>
/// <param name="sName">菜单数组名称</param>
/// <param name="sUrl">链接的数组的名称</param>
/// <param name="iSelect">选中的第几个菜单项</param>
/// <param name="sParameter"></param>
public static void LoadMenuCommon(System.Web.UI.WebControls.Literal litMenu, string[] sName, string[] sUrl, int iSelect,string sParameter)
{
System.Text.StringBuilder sb = new StringBuilder();
sb.Append("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">").Append("<tr>");
for (int i = 0; i < sName.Length; i++)
{
<span style="white-space:pre"> </span>string sClassName = (i + 1) == iSelect ? "Tab2" : "Tab1"; //选中的菜单用CSS中的类"Tab2",否则用"Tab1"
<span style="white-space:pre"> </span>int iWidth = i == (sName.Length - 1) ? 115 : 114;
<span style="white-space:pre"> </span>sb.Append(string.Format("<td class=\"{0}\" style=\"width:{4}px;\" οnclick=\"window.location.href='{2}{3}';\">{1}</td>", sClassName, sName[i], sUrl [i], sParameter, iWidth));
}
sb.Append("</tr></table>");
litMenu.Text = sb.ToString();
}
//2.3.3这里的“Tab2”与“Tab1”只是一个CSS类样式,用于标识选中菜单与未选中菜单样式的区别
</pre><pre name="code" class="css">.Tab1 { background-image: url(../Images/Tab1.gif); font-weight: bold; color: #064570; width: 120px; height: 27px; text-align: center; background-repeat: no-repeat; cursor: hand; }
.Tab2 { background-image: url(../Images/Tab2.gif); font-weight: bold; color: #064570;
width: 120px; height: 27px; text-align: center; background-repeat: no-repeat; cursor: hand; }
//2.3.4最后一步,就是调用了,在其他的web页,调用如下
Menu.LoadMenuCommon(LitMenu, Menu.sName4, Menu.sUrl4, 1, string.Empty);