当前位置导航:炫浪网>>网络学院>>网页制作>>ASP.NET教程

html自定义标签

  这是后台代码

  代码

  using System;

  using System.Collections;

  using System.Configuration;

  using System.Data;

  using System.Linq;

  using System.Web;

  using System.Web.Security;

  using System.Web.UI;

  using System.Web.UI.HtmlControls;

  using System.Web.UI.WebControls;

  using System.Web.UI.WebControls.WebParts;

  using System.Xml.Linq;

  using System.Text;

  using System.Text.RegularExpressions;

  namespace WebApplication8 {

  public partial class _Default : System.Web.UI.Page {

  protected void Page_Load(object sender, EventArgs e) {

  String str = "123<a href=\"{&a'>http://www.qq.com\">{&a  }<a>2{&bbbbccccc    }3";

  String b = cleanStr(str);

  string c = str1(str);

  this.Label1.Text = c;

  }

  public string cleanStr(String str) {

  StringBuilder bs = new StringBuilder(str);

  string str2 = str.Substring(2, str.Length - 3);

  str2 = str2.Trim();

  return str2;

  }

  public string str1(string str) {

  int i1 = 0;

  int i2 = 0;

  string str_ = "";

  string str3 = "";

  string str4 = "";

  Boolean bl1 = false;

  Boolean bl2 = true;

  for (int i = 0; i < str.Length; i++) {

  if (str[i] == '{' && str[i + 1] == '&') {

  i1 = i;

  bl1 = true;

  bl2 = false;

  }

  if (bl2) {

  str_ += str[i];

  }

  if (str[i] == '}' && bl1) {

  i2 = i;

  str3 = str.Substring(i1, (i2 - i1) + 1);// +",";

  str4 = str3.Substring(2, str3.Length - 3);

  str4 = str4.Trim();

  str4 = html_str(str4);

  str_ += str4;

  bl1 = false;

  bl2 = true;

  }

  }

  return str_;

  }

  public string html_str(string str) {

  if (str == "a") {

  return html_str2("{&b}");

  }

  if (str == "b") {

  return html_str2("{&c}");

  }

  if (str == "c") {

  return html_str2("进入qq");

  }

  return null;

  }

  public string html_str2(string str) {

  Regex r = new Regex(@"{+&+[A-Za-z0-9]+}");

  if (r.IsMatch(str)) {

  return str1(str);

  }

  return str;

  }

  }

  }

  前台代码

  代码

  <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication8._Default" %>

  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

  <html xmlns="http://www.w3.org/1999/xhtml" >

  <head id="Head1" runat="server">

  <title>Untitled Page</title>

  </head>

  <body>

  <form id="form1" runat="server">

  <div>

  <asp:Label ID="Label1" runat="server"></asp:Label>

  </div>

  </form>

  </body>

  </html>

  运行结果

  

相关内容
赞助商链接