让ASP.Net HTML页面代码 清爽起来
自从用了 ASP.Net MVC后就喜欢上了它 ,因为MVC对服务器控件的依赖大大减少,它生成的HTML页面就比WebForm清爽多了,加载速度有了明显的改善。
但对于页面中内嵌script,还是不能彻底的避免,如:
<script type="text/javascript" language="javascript">
//<!--
function DepositPage() {
// 这是一个第3方的WebControl,由于是服务器控件,受MasterPage影响,它的客户端ID并不是确定的。
this.ctrlRadGrid = "#<%= ctrlRadGrid.ClientID %>";
// 这是一段又臭又长的JSON,从Modal中传递过来
this.accounts = <%= Modal.JSON %>;
// .............................
}
new DepositPage();
// -->
</script>
是否有一种方法可以将这段JS不内嵌在HTML中,使用<script src="">标记从外部文件加载?
答案是肯定的,通过自定义WebControl完全可以实现,有如下WebControl
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
[ParseChildren(false)]
public class ExternalJavascriptControl : WebControl
{
private string SafeFilenamePrefix
{
get
{
return Regex.Replace( this.ClientID
, "(\\\\|\\/|\\:|\\*|\\?|\\\"|\\<|\\>|\\|)"
, "_"
, RegexOptions.ECMAScript | RegexOptions.Compiled
);
}
}
protected override void Render(HtmlTextWriter writer)
{
if (!Visible)
return;
try
{
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
base.Render(htw);
// get the rendered content
string rendered = sw.ToString();
// remove the script tag if exist
rendered = Regex.Replace(rendered
, @"(<[\s\/]*script\b[^>]*>)"
, string.Empty
, RegexOptions.ECMAScript | RegexOptions.Compiled | RegexOptions.IgnoreCase
);
// get the file pa
相关文档:
<!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>
<meta NAME="Copyright" CONTENT="">
<meta http-equiv="Content-Type" content="text/html; charset=u ......
Ajax现在已经是相当流行的技术了,Ajax不仅是想服务器端发送消息,更重要的是无刷新的重载页面。
如果页面单纯的使用js来创建,要写大量的代码,而且不直观。
在asp.net中,其实我们可以创建用户自定义控件,通过Ajax请求返回用户自定义控件HTML代码。
public static string RangerUsControl(string controlName) ......
最近公司需要开发一个简历导入功能,类似博客搬家或者邮箱搬家,之前抓取信息是利用火车采集器,但是简历导入功能需要用户登陆以后才能获取简历数据,无奈只好自己开发了。
首先是遇到的问题是:如何实现模拟登陆?
我们知道一般的网站都是通过Cookies来维护状态的,我抓的网站也是支持利用Cookies来验证用户的,构造一个 ......
快捷键 功能
CTRL + SHIFT + B生成解决方案
CTRL + F7 生成编译
CTRL + O 打开文件
CTRL + SHIFT + O打开项目
CTRL + SHIFT + C显示类视图窗口
F4 显示属性窗口
SHIFT + F4显示项目属性窗口
CTRL + SHIFT + E显示资源视图
F12 转到定义
CTRL + F12转到声明
CTRL + ALT + J对象浏览
CTRL + ALT + F1 ......