随笔- 18
文章- 0
评论- 4
通常web应用程序在发布后,为了给用户一个友好界面和使用体验,都会在错误发生时跳转至一个自定义的错误页面,而不是asp.net向用户暴露出来的详细的异常列表。
简单的错误处理页面可以通过web.config来设置
<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
void Application_Error(object sender, EventArgs e)
{
Exception objErr = Server.GetLastError().GetBaseException();
string error = "发生异常页: " + Request.Url.ToString() + "<br>";
error += "异常信息: " + objErr.Message + "<br>";
Server.ClearError();
Application["error"] = error;
Response.Redirect("~/ErrorPage/ErrorPage.aspx");
}
protected void Page_Load(object sender, EventArgs e)
{
ErrorMessageLabel.Text = Application["error"].ToString();
}当最终用户使用应用程序的时候,他们可能不想知道错误的原因,这个时候,我们可以通过复选框来实现,是否呈现错误的原因。可将Label放在一个div中,然后用复选框来决定是否呈现div
<script language="javascript" type="text/javascript">
<!--

function CheckError_onclick() {
var chk = document.getElementById("CheckError");
var divError = document.getElementById("errorMsg");
if(chk.checked)
{
divError.style.display = "inline";
}
else
{
divError.style.display = "none";
}
}

// -->
</script>
简单的错误处理页面可以通过web.config来设置
<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>如果想通过编程的方式来呈现错误原因,可以通过Page_Error事件来做这件事.
另一种方式则可以通过Global.asax来实现,我觉得这种方式较为方便,另外如果能结合一个单独的更加友好的页面,则看来起更舒服一些
void Application_Error(object sender, EventArgs e)
{
Exception objErr = Server.GetLastError().GetBaseException();
string error = "发生异常页: " + Request.Url.ToString() + "<br>";
error += "异常信息: " + objErr.Message + "<br>";
Server.ClearError();
Application["error"] = error;
Response.Redirect("~/ErrorPage/ErrorPage.aspx");
}ErrorPage.aspx
protected void Page_Load(object sender, EventArgs e)
{
ErrorMessageLabel.Text = Application["error"].ToString();
}
<script language="javascript" type="text/javascript">
<!--
function CheckError_onclick() {
var chk = document.getElementById("CheckError");
var divError = document.getElementById("errorMsg");
if(chk.checked)
{
divError.style.display = "inline";
}
else
{
divError.style.display = "none";
}
}
// -->
</script>
我们可以对errorpage这页做一些更亲切的设计,让人看起来更舒服些。
转自:http://www.cnblogs.com/EasyLive2006/archive/2007/01/07/613922.html
posted @ 2011-12-20 17:23 袜子不回头 阅读(29) 评论(0) 编辑
1 OpenModalDialog("Dialog.aspx", 500, 200); JS Open一个模式对话框,如果对话框里有Button提交 或任何和服务器有交互,点击按钮就会打开另外一个页面来显示弹出的窗口
解决办法: 在Dialog.aspx 页面的的head 里面加入<base target="_self" /> 指向自己,这样就不会在点击Dialog.aspx里面的按钮之后打开另外一个页面。
2 清除Jquery缓存的方法:在Json请求的时候路径变一下,加个随机数参数
var x = 99; |
|
var y = 95; |
|
var rand = parseInt(Math.random() * (x - y + 1) + y); |
后台代码向控件加属性
controlId.Attributes["属性名字"]=值
后台向控件注册前台JS 方法
controlId.OnClientClick = "functionName('" + 参数+ "')";
嵌套Repeater 内层Repeater的onItemCommand事件需要在外层的 Repeater的OnItemCreate事件里注册
posted @ 2011-11-30 11:11 袜子不回头 阅读(23) 评论(0) 编辑
<script type="text/javascript">
debugger;
$(document).ready(function () {
var url = '@Url.Action("getJson", "Home")';
$("button").click(function () {
$.getJSON(url,"x", function (data) { //这里的x是Action getJson的参数,是可以从请求字符串(QueringString)中取到的
alert(data.name);
// $.each(result, function (i, field) {
// $("p").append(field + " ");
// });
});
});
});
</script>
debugger;
$(document).ready(function () {
var url = '@Url.Action("getJson", "Home")';
$("button").click(function () {
$.getJSON(url,"x", function (data) { //这里的x是Action getJson的参数,是可以从请求字符串(QueringString)中取到的
alert(data.name);
// $.each(result, function (i, field) {
// $("p").append(field + " ");
// });
});
});
});
</script>
public JsonResult getJson(string x)
{
var data = new { name = "TestName" };
return Json(data, JsonRequestBehavior.AllowGet); //在这里必须要设置JSON的请求行为为GET,不然是取不到数据的
}
{
var data = new { name = "TestName" };
return Json(data, JsonRequestBehavior.AllowGet); //在这里必须要设置JSON的请求行为为GET,不然是取不到数据的
}
posted @ 2011-11-02 11:24 袜子不回头 阅读(218) 评论(0) 编辑
先上实体类
1 public class ParentItem
2 {
3 public int ID { get; set; }
4 public string Name { get; set; }
5 }
6 public class SubItem
7 {
8 public int ParentID { get; set; }
9 public int ID { get; set; }
10 public string Name { get; set; }
11 }
12 public class TestViewModel
13 {
14 public List<ParentItem> Parent { get; set; }
15 public List<SubItem> Sub { get; set; }
16 }
2 {
3 public int ID { get; set; }
4 public string Name { get; set; }
5 }
6 public class SubItem
7 {
8 public int ParentID { get; set; }
9 public int ID { get; set; }
10 public string Name { get; set; }
11 }
12 public class TestViewModel
13 {
14 public List<ParentItem> Parent { get; set; }
15 public List<SubItem> Sub { get; set; }
16 }
Controller中的关键代码
1 public class HomeController : Controller
2 {
3 List<ParentItem> _parentList = new List<ParentItem>();
4 List<SubItem> _subList = new List<SubItem>();
5
6 //
7 // GET: /Home/
8
9 public HomeController()
10 {
11 _parentList.Add(new ParentItem() { ID = 1, Name = "P1" });
12 _parentList.Add(new ParentItem() { ID = 2, Name = "P2" });
13 _parentList.Add(new ParentItem() { ID = 3, Name = "P3" });
14 _subList.Add(new SubItem() { ID = 1, ParentID = 1, Name = "P1-S1" });
15 _subList.Add(new SubItem() { ID = 2, ParentID = 1, Name = "P1-S2" });
16 _subList.Add(new SubItem() { ID = 3, ParentID = 2, Name = "P2-S1" });
17 _subList.Add(new SubItem() { ID = 4, ParentID = 2, Name = "P2-S2" });
18 _subList.Add(new SubItem() { ID = 5, ParentID = 3, Name = "P3-S1" });
19 _subList.Add(new SubItem() { ID = 6, ParentID = 3, Name = "P3-S2" });
20 }
21 public ActionResult Index()
22 {
23
24 TestViewModel model = new TestViewModel() { Parent = this._parentList, Sub = this._subList };
25
26 ViewData["Parent"] = new SelectList(_parentList, "ID", "Name", 3);
27 ViewData["Sub"] = new SelectList(_subList, "ID", "Name", 3);
28 return View(model);
29 }
30 //JsonResult继承了ActionResult
31
32 public JsonResult GetBZ(int parentId) //GetBZ对应View的GetBZ,parentId也是通过View可以获取参数值
33 {
34 var d = this._subList.FindAll(s => s.ParentID == parentId);
35
36 return Json(d, JsonRequestBehavior.AllowGet);
37
38 //这里的代码是封装过的,可以在这里写任何想要的代码
39 //注意,由于是列表框 所以返回的值应该是List<SelectListItem>(也许不只一种传递类型)
40 }
2 {
3 List<ParentItem> _parentList = new List<ParentItem>();
4 List<SubItem> _subList = new List<SubItem>();
5
6 //
7 // GET: /Home/
8
9 public HomeController()
10 {
11 _parentList.Add(new ParentItem() { ID = 1, Name = "P1" });
12 _parentList.Add(new ParentItem() { ID = 2, Name = "P2" });
13 _parentList.Add(new ParentItem() { ID = 3, Name = "P3" });
14 _subList.Add(new SubItem() { ID = 1, ParentID = 1, Name = "P1-S1" });
15 _subList.Add(new SubItem() { ID = 2, ParentID = 1, Name = "P1-S2" });
16 _subList.Add(new SubItem() { ID = 3, ParentID = 2, Name = "P2-S1" });
17 _subList.Add(new SubItem() { ID = 4, ParentID = 2, Name = "P2-S2" });
18 _subList.Add(new SubItem() { ID = 5, ParentID = 3, Name = "P3-S1" });
19 _subList.Add(new SubItem() { ID = 6, ParentID = 3, Name = "P3-S2" });
20 }
21 public ActionResult Index()
22 {
23
24 TestViewModel model = new TestViewModel() { Parent = this._parentList, Sub = this._subList };
25
26 ViewData["Parent"] = new SelectList(_parentList, "ID", "Name", 3);
27 ViewData["Sub"] = new SelectList(_subList, "ID", "Name", 3);
28 return View(model);
29 }
30 //JsonResult继承了ActionResult
31
32 public JsonResult GetBZ(int parentId) //GetBZ对应View的GetBZ,parentId也是通过View可以获取参数值
33 {
34 var d = this._subList.FindAll(s => s.ParentID == parentId);
35
36 return Json(d, JsonRequestBehavior.AllowGet);
37
38 //这里的代码是封装过的,可以在这里写任何想要的代码
39 //注意,由于是列表框 所以返回的值应该是List<SelectListItem>(也许不只一种传递类型)
40 }
前台
<script type="text/javascript">
$(function(){
$("#Parent").change(function(){ //Parent选项改变时激活
var selec = $(this).val(); //获取改变的选项值
var url = "@Url.Action("GetBZ")"; //参数依次类型(action,Controller,area)
$("#Sub").find("option").remove(); //清空
$.getJSON(url, { 'parentId': selec }, function (data) { //parentId是参数名和Controllers中的action参数名相同
$.each(data, function (i, item) {
$("<option></option>").val(item["ID"]).text(item["Name"]).appendTo($("#Sub"));
}); //如果url访问成功 则执行function(data)这个函数(看仔细了`,这里该函数也是.getJSON的第三个参数)
}); //function(data)获取了通过url返回来的值,并且循环读取出来
});
});
</script>
@Html.DropDownList("Parent","Select")
@Html.DropDownList("Sub", "Select")
$(function(){
$("#Parent").change(function(){ //Parent选项改变时激活
var selec = $(this).val(); //获取改变的选项值
var url = "@Url.Action("GetBZ")"; //参数依次类型(action,Controller,area)
$("#Sub").find("option").remove(); //清空
$.getJSON(url, { 'parentId': selec }, function (data) { //parentId是参数名和Controllers中的action参数名相同
$.each(data, function (i, item) {
$("<option></option>").val(item["ID"]).text(item["Name"]).appendTo($("#Sub"));
}); //如果url访问成功 则执行function(data)这个函数(看仔细了`,这里该函数也是.getJSON的第三个参数)
}); //function(data)获取了通过url返回来的值,并且循环读取出来
});
});
</script>
@Html.DropDownList("Parent","Select")
@Html.DropDownList("Sub", "Select")
posted @ 2011-10-25 10:38 袜子不回头 阅读(230) 评论(1) 编辑
1 //Product 实体
2 public class Product
3 {
4 [HiddenInput(DisplayValue = false)]
5 public int ProductID { get; set; }
6 [Required(ErrorMessage = "Please enter a product name")]
7 public string Name { get; set; }
8 [Required(ErrorMessage = "Please enter a description")]
9 [DataType(DataType.MultilineText)]
10 public string Description { get; set; }
11 [Required]
12 [Range(0.01, double.MaxValue, ErrorMessage = "Please enter a positive price")]
13 public decimal Price { get; set; }
14 [Required(ErrorMessage = "Please specify a category")]
15 public string Category { get; set; }
16 public byte[] ImageData { get; set; }
17 [HiddenInput(DisplayValue = false)]
18 public string ImageMimeType { get; set; }
19
20 }
2 public class Product
3 {
4 [HiddenInput(DisplayValue = false)]
5 public int ProductID { get; set; }
6 [Required(ErrorMessage = "Please enter a product name")]
7 public string Name { get; set; }
8 [Required(ErrorMessage = "Please enter a description")]
9 [DataType(DataType.MultilineText)]
10 public string Description { get; set; }
11 [Required]
12 [Range(0.01, double.MaxValue, ErrorMessage = "Please enter a positive price")]
13 public decimal Price { get; set; }
14 [Required(ErrorMessage = "Please specify a category")]
15 public string Category { get; set; }
16 public byte[] ImageData { get; set; }
17 [HiddenInput(DisplayValue = false)]
18 public string ImageMimeType { get; set; }
19
20 }
//View
@model List<List<Product>>
//----------------不用理会-----------------
@{
ViewBag.Title = "Admin: All Products";
Layout = "~/Views/Shared/_AdminLayout.cshtml";
}
<h1>
All Products</h1>
<table class="Grid">
<tr>
<th>
ID
</th>
<th>
Name
</th>
<th class="NumericCol">
Price
</th>
<th>
Actions
</th>
</tr>
//----------------不用理会-----------------
//----------------重点---------------------
//必须使用for循环来做模型绑定,MVC内置使用用[]索引来绑定集合的
@using (Html.BeginForm("Test", "Admin"))//ActionName,ControllerName
{
for (int i = 0; i < Model.Count; i++)
{
for (int j = 0; j < Model[i].Count; j++)
{
<tr>
<td>
//m 是什么?你懂的
@Html.LabelFor(m => m[i][j].ProductID)
@Html.HiddenFor(m => m[i][j].ProductID)
@Html.HiddenFor(m => m[i][j].Name)
</tr>
}
}
<input type="submit" value="Test" />
}
@model List<List<Product>>
//----------------不用理会-----------------
@{
ViewBag.Title = "Admin: All Products";
Layout = "~/Views/Shared/_AdminLayout.cshtml";
}
<h1>
All Products</h1>
<table class="Grid">
<tr>
<th>
ID
</th>
<th>
Name
</th>
<th class="NumericCol">
Price
</th>
<th>
Actions
</th>
</tr>
//----------------不用理会-----------------
//----------------重点---------------------
//必须使用for循环来做模型绑定,MVC内置使用用[]索引来绑定集合的
@using (Html.BeginForm("Test", "Admin"))//ActionName,ControllerName
{
for (int i = 0; i < Model.Count; i++)
{
for (int j = 0; j < Model[i].Count; j++)
{
<tr>
<td>
//m 是什么?你懂的
@Html.LabelFor(m => m[i][j].ProductID)
@Html.HiddenFor(m => m[i][j].ProductID)
@Html.HiddenFor(m => m[i][j].Name)
</tr>
}
}
<input type="submit" value="Test" />
}
//Controller:
//加断点即可查看传进来的参数,并行自定义对象集合都可以传,更别说单集合对象了(List<Product>) 自己改造下即可,你懂的
public ViewResult Test(List<List<Product>> list)
{
return View();
}
//加断点即可查看传进来的参数,并行自定义对象集合都可以传,更别说单集合对象了(List<Product>) 自己改造下即可,你懂的
public ViewResult Test(List<List<Product>> list)
{
return View();
}
posted @ 2011-10-17 16:33 袜子不回头 阅读(134) 评论(0) 编辑
摘要: 有时候会遇到这种情况:在一个表单上需要多个按钮来完成不同的功能,比如一个简单的审批功能。如果是用webform那不需要讨论,但asp.net mvc中一个表单只能提交到一个Action处理,相对比较麻烦点。方法一:使用客户端脚本比如我们在View中这样写:view sourceprint?1<input type="submit" value="审核通过" onclick='this.form.action="<%=Url.Action("Action1") %>";' />阅读全文
posted @ 2011-10-14 16:19 袜子不回头 阅读(41) 评论(0) 编辑
摘要: public class RoleFilter : FilterAttribute, IAuthorizationFilter 02{ 0304#region IAuthorizationFilter 成员 0506/// <summary> 07/// 产生随机数判断是否具有权限访问 08/// </summary> 09/// <param name="filterContext"></param> 10public void OnAuthorization(AuthorizationContext filterConte阅读全文
posted @ 2011-10-13 14:44 袜子不回头 阅读(65) 评论(0) 编辑
摘要: 当一个Activity绑定到一个Service上时,它负责维护Service实例的引用,允许你对正在运行的Service进行一些方法调用。Activity能进行绑定得益于Service的接口。为了支持Service的绑定,实现onBind方法如下所示:private final IBinder binder = new MyBinder();@Overridepublic IBinder onBind(Intent intent) {return binder;}public class MyBinder extends Binder {MyService getService(){retur阅读全文
posted @ 2011-06-30 18:42 袜子不回头 阅读(190) 评论(0) 编辑
摘要: 多Activity开发中,有可能是自己应用间的activity 跳转,或者夹带其他应用的可复用activity。可能会希望跳转到原来某个activity实例,而非产生多个重复的activity。我们可借助 activity 四种启动模式来实现不同的需求:standard默认模式 --------- 来了intent,每次都创建新的实例。singleTop-------- 来了intent, 每次都创建新的实例,仅一个例外:当栈顶的activity 恰恰就是该activity的实例(即需要创建的实例)时,不再创建新实例。这解决了栈顶复用问题,想一想,你按两次back键,退出的都是同一个activ阅读全文
posted @ 2011-06-30 18:13 袜子不回头 阅读(264) 评论(0) 编辑
摘要: 首先在Mainifest.xml的Activity元素中加入android:configChanges="orientation|keyboardHidden"属性 <activity android:name=".FileBrowser" android:label="@string/app_name" android:configChanges="orientation|keyboardHidden"> <intent-filter> <action android:name=&q阅读全文
posted @ 2011-06-30 16:35 袜子不回头 阅读(1376) 评论(0) 编辑


