张银的博客


Eat to live, but do not live to eat.

导航

HiddenField、LinkButton、Literal

Posted on 2009-02-27 17:55  张银  阅读(608)  评论(0编辑  收藏  举报

HiddenField,
LinkButton,
Literal

HiddenField提供了一种方式实现在页面存储信息,并与其他控件信息用法差不多,但是它不显示出来、不占地方。仍然不能存放类似密码等敏感信息,因为它在页面html源代码中是可以看见的。

<asp:HiddenField ID="HiddenField1" runat="server" Value="隐藏字段的值" />

LinkButton,这个控件在用法上和Button差不多,是HyperLink和Button的整合。
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">LinkButton</asp:LinkButton>
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        LinkButton1.Text 
= HiddenField1.Value;
        
//日常使用(比如注册)中提取值,随同表单传送
    }

Literal,用的比较少。.net 2.0出来的
  Literal控件表示用于向页面添加内容的几个选项之一。对于静态内容,无需使用容器,可以将标记作为Html直接添加到页面中。但是,如果要动态添加内容,则必须将内容添加到容器中。典型的容器有Label控件、Literal控件、Panel控件和Placeholder控件。
  Literal控件的Mode属性:指定控件对所添加标记的处理方式。
  1.Transform:添加到控件中的任何标记都将进行转换,以适应请求浏览器的协议。如果向使用html外的其他协议的移动设备呈现内容,此设置非常有用。
  2.PassThrough:添加到控件中的任何标记都将按原样呈现在浏览器中。
  3.Encode:添加到控件中的任何标记都将使用HtmlEncode方法进行编码,该方法将把html编码转换为其文本表示形式。例如,<b>标记将呈现为&lt;b&gt;。当希望浏览器显示而不解释标记时,编码将很有用。编码对于安全也很有用,有助于防止在浏览器中执行恶意标记,显示来自不受信任的源的字符串时推荐使用此设置。

使用Transform模式:
<asp:Literal ID="Literal2" runat="server" Text='<hr><i>[aidd](2008);"heihei"<br><p></i>'></asp:Literal>
使用PassThrough模式:
<asp:Literal ID="Literal3" runat="server" Mode="PassThrough" Text='<hr><i>[aidd](2008);"heihei"<br><p></i>'></asp:Literal>
使用Encode模式:
<asp:Literal ID="Literal4" runat="server" Mode="Encode" Text='<hr><i>[aidd](2008);"heihei"<br><p></i>'></asp:Literal>
正常模式的Label:
<asp:Label ID="Label1" runat="server" Text='<hr><i>[aidd](2008);"heihei"<br><p></i>'></asp:Label>
使用了Server.HtmlEncode方法解码的方式显示Label:
<asp:Label ID="Label2" runat="server" Text='<hr><i>[aidd](2008);"heihei"<br><p></i>'></asp:Label>
    protected void Page_Load(object sender, EventArgs e)
    {
        Label2.Text 
= Server.HtmlEncode(Label2.Text);
    }

具体显示的效果如何自己去试下就知道了