HtmlDecode 、HtmlEncode的用法

<asp:Repeater ID="Repeater1" runat="server">
        <ItemTemplate>       
            <%#Eval("class").ToString().Replace(((char)10).ToString(), "<br/>") %>
        </ItemTemplate>
</asp:Repeater>

<%#Eval("class").ToString().Replace(((char)10).ToString(), "<br/>") %>
将数据库中的数据原样输出

<%#Server.HtmlDecode(Eval("class").ToString().Replace(((char)10).ToString(), "<br/>")) %>
此时数据库中的HTML标识符数据会被解释为相应的效果

后台代码:
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[1].ConnectionString);      
        string str = Server.HtmlEncode(TextBox1.Text);//编码后写入数据库,读出时可以包含html标识符
        SqlCommand com = new SqlCommand("update stuInfo set class='"+str+"' where num='2001'", con);
        con.Open();
        com.ExecuteNonQuery();
        con.Close();


SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[1].ConnectionString);
        SqlCommand com = new SqlCommand("select class from stuInfo where num='2001'", con);
        con.Open();
        //Label1.Text = Server.HtmlDecode(com.ExecuteScalar().ToString());//取出数据库中的数据然后解码,读出数据将不包含html标识符,被转换为相应的效果
        Label1.Text = com.ExecuteScalar().ToString(); //取出数据库中的数据不解码,数据库的数据原样输出
        con.Close();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值