美工作出来的页面都是html格式的~闲来无事~写了个程序用来转换html格式页面为aspx格式,并为之生成cs文件
defalut.aspx:
defalut.aspx.cs:
程序功能尚在完善中,已经完成可以将所有html控件转换为server控件,下一步是将指定的控件进行转换
传上来,自己别弄掉了,日后再用/Files/daiye/rar/html2aspx.rar
default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_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 runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" /> <asp:CheckBox ID="changeall"
runat="server" AutoPostBack="True" OnCheckedChanged="changeall_CheckedChanged" />change all the html control into server control?
<asp:Button ID="Button1" runat="server" Text="change" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_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 runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" /> <asp:CheckBox ID="changeall"
runat="server" AutoPostBack="True" OnCheckedChanged="changeall_CheckedChanged" />change all the html control into server control?
<asp:Button ID="Button1" runat="server" Text="change" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>
defalut.aspx.cs:
Code
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
private string[] filename = new string[2];
private string fullname;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string filepath;
if (FileUpload1.HasFile)
{
filepath=FileUpload1.PostedFile.FileName;
fullname = FileUpload1.FileName;
filename=fullname.Split('.');
readFile(filepath);
}
}
protected void readFile(string filepath)
{
StreamReader sr = new StreamReader(filepath,System.Text.Encoding.UTF8);
try
{
string str = sr.ReadToEnd();
replaceStr(str);
}
catch (IOException ioe)
{
Response.Write(ioe.Message);
}
finally
{
sr.Close();
}
}
protected void replaceStr(string str)
{
StringBuilder stb = new StringBuilder();
stb.Append("<%@ Page Language=\"C#\" AutoEventWireup=\"true\" CodeFile=\"" + filename[0] + ".aspx.cs\" Inherits=\""+filename[0]+"\" %>\n");
stb.Append(str);
stb.Replace("<head>","<head runat=\"server\">");
stb.Replace("<body>", "<body>\n<form id=\"form1\" runat=\"server\">\n<div>");
stb.Replace("</body>", "</div>\n</form>\n</body>");
if (changeall.Checked)
{ stb = controlChange(stb); }
// getAllCon(str,"input");
wirteFile(stb);
}
protected void wirteFile(StringBuilder str)
{
StreamWriter sw;
try
{
sw = new StreamWriter(Server.MapPath("new/") + filename[0] + ".aspx", false, System.Text.Encoding.UTF8);
sw.Write(str);
sw.Flush();
}
catch (IOException ioe)
{
Response.Write(ioe.Message);
}
StringBuilder cs = new StringBuilder();
cs.Append("using System;\n");
cs.Append("using System.Data;\n");
cs.Append("using System.Configuration;\n");
cs.Append("using System.Collections;\n");
cs.Append("using System.Web;\n");
cs.Append("using System.Web.Security;\n");
cs.Append("using System.Web.UI;\n");
cs.Append("using System.Web.UI.WebControls;\n");
cs.Append("using System.Web.UI.WebControls.WebParts;\n");
cs.Append("using System.Web.UI.HtmlControls;\n");
cs.Append("public partial class ");
cs.Append(filename[0]);
cs.Append(" : System.Web.UI.Page\n");
cs.Append("{\n");
cs.Append(" protected void Page_Load(object sender, EventArgs e)\n");
cs.Append(" {\n\n");
cs.Append(" }\n");
cs.Append("}\n");
try
{
sw = new StreamWriter(Server.MapPath("new/") + filename[0] + ".aspx.cs", false, System.Text.Encoding.UTF8);
sw.Write(cs);
sw.Flush();
}
catch (IOException ioe)
{
Response.Write(ioe.Message);
}
}
protected void changeall_CheckedChanged(object sender, EventArgs e)
{
if (changeall.Checked)
{
Response.Write("<script>alert('the control with the attribute \"ID\" will be changed')</script>");
}
}
protected StringBuilder controlChange(StringBuilder stb)
{
stb.Replace("<input", "<input runat=\"server\"");
stb.Replace("<textarea", "<textarea runat=\"server\"");
stb.Replace("<select","<select runat=\"server\"");
return stb;
}
protected void getAllCon(string str,string type)
{
string begin, end;
switch (type)
{
case "input": begin = "<input"; end = "/>"; break;
case "select": begin = "<selelct"; end = "</selelct>"; break;
default: begin = ""; end = ""; break;
}
int start = str.IndexOf(begin);
while (start > 0)
{
int length = str.IndexOf(end, start) - start + 2;
Response.Write(str.Substring(start, length));
start = str.IndexOf(begin, start + length);
}
}
}
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
private string[] filename = new string[2];
private string fullname;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string filepath;
if (FileUpload1.HasFile)
{
filepath=FileUpload1.PostedFile.FileName;
fullname = FileUpload1.FileName;
filename=fullname.Split('.');
readFile(filepath);
}
}
protected void readFile(string filepath)
{
StreamReader sr = new StreamReader(filepath,System.Text.Encoding.UTF8);
try
{
string str = sr.ReadToEnd();
replaceStr(str);
}
catch (IOException ioe)
{
Response.Write(ioe.Message);
}
finally
{
sr.Close();
}
}
protected void replaceStr(string str)
{
StringBuilder stb = new StringBuilder();
stb.Append("<%@ Page Language=\"C#\" AutoEventWireup=\"true\" CodeFile=\"" + filename[0] + ".aspx.cs\" Inherits=\""+filename[0]+"\" %>\n");
stb.Append(str);
stb.Replace("<head>","<head runat=\"server\">");
stb.Replace("<body>", "<body>\n<form id=\"form1\" runat=\"server\">\n<div>");
stb.Replace("</body>", "</div>\n</form>\n</body>");
if (changeall.Checked)
{ stb = controlChange(stb); }
// getAllCon(str,"input");
wirteFile(stb);
}
protected void wirteFile(StringBuilder str)
{
StreamWriter sw;
try
{
sw = new StreamWriter(Server.MapPath("new/") + filename[0] + ".aspx", false, System.Text.Encoding.UTF8);
sw.Write(str);
sw.Flush();
}
catch (IOException ioe)
{
Response.Write(ioe.Message);
}
StringBuilder cs = new StringBuilder();
cs.Append("using System;\n");
cs.Append("using System.Data;\n");
cs.Append("using System.Configuration;\n");
cs.Append("using System.Collections;\n");
cs.Append("using System.Web;\n");
cs.Append("using System.Web.Security;\n");
cs.Append("using System.Web.UI;\n");
cs.Append("using System.Web.UI.WebControls;\n");
cs.Append("using System.Web.UI.WebControls.WebParts;\n");
cs.Append("using System.Web.UI.HtmlControls;\n");
cs.Append("public partial class ");
cs.Append(filename[0]);
cs.Append(" : System.Web.UI.Page\n");
cs.Append("{\n");
cs.Append(" protected void Page_Load(object sender, EventArgs e)\n");
cs.Append(" {\n\n");
cs.Append(" }\n");
cs.Append("}\n");
try
{
sw = new StreamWriter(Server.MapPath("new/") + filename[0] + ".aspx.cs", false, System.Text.Encoding.UTF8);
sw.Write(cs);
sw.Flush();
}
catch (IOException ioe)
{
Response.Write(ioe.Message);
}
}
protected void changeall_CheckedChanged(object sender, EventArgs e)
{
if (changeall.Checked)
{
Response.Write("<script>alert('the control with the attribute \"ID\" will be changed')</script>");
}
}
protected StringBuilder controlChange(StringBuilder stb)
{
stb.Replace("<input", "<input runat=\"server\"");
stb.Replace("<textarea", "<textarea runat=\"server\"");
stb.Replace("<select","<select runat=\"server\"");
return stb;
}
protected void getAllCon(string str,string type)
{
string begin, end;
switch (type)
{
case "input": begin = "<input"; end = "/>"; break;
case "select": begin = "<selelct"; end = "</selelct>"; break;
default: begin = ""; end = ""; break;
}
int start = str.IndexOf(begin);
while (start > 0)
{
int length = str.IndexOf(end, start) - start + 2;
Response.Write(str.Substring(start, length));
start = str.IndexOf(begin, start + length);
}
}
}
程序功能尚在完善中,已经完成可以将所有html控件转换为server控件,下一步是将指定的控件进行转换
传上来,自己别弄掉了,日后再用/Files/daiye/rar/html2aspx.rar