asp.net 关闭父窗体_一个ASP.NET速览:优雅的Web窗体和地滚雪球

asp.net 关闭父窗体

asp.net 关闭父窗体

For the most part, I'm an ASP.NET developer. I don't need to specify MVC or Web Forms, because it's all One ASP.NET its core. My apps are often hybrids and include not just Web Forms or MVC but also SignalR and Web API.

在大多数情况下,我是ASP.NET开发人员。 我不需要指定MVC或Web窗体,因为它全部是ASP.NET的核心。 我的应用程序经常是混合的,不仅包括Web FormsMVC ,还包括SignalR和Web API。

Web Forms often gets picked on because of large View State, weird markup or maybe folks don't like the controls model. However, Web Forms has its place and it's getting even better with .NET 4.5. Here's a little sneak peek of some cool ideas Damian Edwards and the team have been working on for the next version of ASP.NET.

Web窗体经常由于较大的视图状态,怪异的标记或人们可能不喜欢控件模型而被选中。 但是,Web Forms占有一席之地,并且在.NET 4.5中变得越来越好。 这是一些不错的主意,这是一些秘密的想法, Damian Edwards及其团队一直在努力开发下一个版本的ASP.NET

As a place to start, remember that ASP.NET routing started in MVC and moved into core ASP.NET. Routing is useful in all ASP.NET applications - MVC, Web Pages and Web Forms. Model Binding is coming to Web Forms as well, as well as Strongly Typed Data Controls and some other features that make both the code and the result pretty compelling. Dare I say, elegant. Elegant Web Forms? Madness! Who is this fool?

首先,请记住ASP.NET路由是从MVC开始的,并转移到了核心ASP.NET中。 路由在所有ASP.NET应用程序(MVC,网页和Web窗体)中都很有用。 Web窗体以及强类型数据控件以及其他一些使代码和结果都非常引人注目的功能也将应用于模型绑定我敢说,优雅。 优雅的Web表单? 疯狂! 这个傻瓜是谁?

Here's a sortable grid with Create, Edit, Delete in Web Forms 4.5. An experiment for you, Dear Reader, would be to do the same thing I'm doing here in ASP.NET MVC or Web Pages.

这是一个可排序的网格,具有在Web Forms 4.5中创建,编辑,删除的功能。 亲爱的读者,为您做的一项实验就是在ASP.NET MVC或Web页面中执行与此处相同的操作。

Do note that this is fresh off Damian's laptop, and it's a experiment.

请注意,这是Damian笔记本电脑上的新产品,这是一个实验。

First, note the clean URLs. Use Routing, Web Forms people. You have no reason not to.

首先,记下干净的URL。 使用路由,Web Forms的人。 您没有理由不这样做。

Clean URLs in WebForms. Scandalous.

Here's what it'll look like:

外观如下:

Databinding in ASP.NET Web Forms 4.5 Sneak Peek

Right now in this experiment, there is this routing table. Personally I'd like a convention for CRUD to make this one line.

现在在此实验中,有此路由表。 我个人希望CRUD能够做到这一点。

Routes.MapPageRoute("CategoriesNew", "Categories/New", "~/Categories_New.aspx");
Routes.MapPageRoute("CategoriesEdit", "Categories/{id}", "~/Categories_Edit.aspx");
Routes.MapPageRoute("CategoriesDelete", "Categories/{id}/Delete", "~/Categories_Delete.aspx");
Routes.MapPageRoute("Categories", "Categories", "~/Categories.aspx");

Here's the  Grid. Now, before you declare that's too freaky, take a look and note there's a lot of functionality going on here in not too many lines. ItemType (was ModelType in the Developer Preview) is strongly typing this grid to the Category model. Notice SelectMethod. You just need to provide a method here that returns an iQueryable, in this case, GetCategories.

这是网格。 现在,在您宣布它太怪异之前,请看一下并注意这里有很多功能在不多的行中进行。 ItemType(在“开发人员预览”中为ModelType)正在强烈地将此网格键入到Category模型。 注意SelectMethod。 您只需要在此处提供一个返回iQueryable的方法,在本例中为GetCategories。

UPDATE with NOTE: See the comments below. Damian Edwards says: "That said, you don't need to return IQueryable. You can happily return an IEnumerable and just take in the extra parameters that the GridView will give you to ensure you can retrieve only the data for the currently requested page and sorted by the chosen column."

注意:请参见以下注释。 达米安·爱德华兹(Damian Edwards)说:“也就是说,您不需要返回IQueryable。您可以很高兴地返回IEnumerable,只需接受GridView将为您提供的其他参数,以确保您只能检索当前请求页面的数据,并且按所选列排序。

<asp:GridView runat="server" ID="categoriesGrid" CellSpacing="-1" GridLines="None"
ItemType="VS11Experiment.Model.Category" DataKeyNames="CategoryId"
AutoGenerateColumns="false"
AllowPaging="true" AllowSorting="true" PageSize="5"
SelectMethod="GetCategories">
<Columns>
<asp:DynamicField DataField="Name" />
<asp:DynamicField DataField="Description" />
<asp:TemplateField>
<ItemTemplate>
<a runat="server" href='<%# GetRouteUrl("CategoriesEdit", new { id = Item.CategoryId }) %>'>edit</a>
<a runat="server" href='<%# GetRouteUrl("CategoriesDelete", new { id = Item.CategoryId }) %>'>delete</a>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
No categories found.
</EmptyDataTemplate>
<SortedAscendingHeaderStyle CssClass="asc" />
<SortedDescendingHeaderStyle CssClass="desc" />
<PagerStyle CssClass="pager" />
</asp:GridView>

"OK, Hanselman, what hellish code-behind are you going to show us now? What Satan's spawn have you been shining us on with all this time only to spring the nasty stuff at the last minute? I know you crafty Microsoft types, always creeping around with your Legos and your Windows Phones."

“好吧,汉塞尔曼,您现在要向我们展示的是什么地狱代码?这段时间,您一直在撒旦的光芒上一直在照耀着我们,直到最后一刻才出现讨厌的东西?我知道您总是在狡猾地使用Microsoft类型与乐高积木和Windows Phone一起爬行。”

Fine, you caught me.

很好,你抓住了我。

public partial class Categories : System.Web.UI.Page
{
private readonly DemoWhateverDataContext _db = new DemoWhateverDataContext();

public void Page_Load()
{
if (!IsPostBack)
{
// Set default sort expression
categoriesGrid.Sort("Name", SortDirection.Ascending);
}
}

public IQueryable<Category> GetCategories()
{
return _db.Categories;
}
}

Aargh! My eyes! Wait, that doesn't suck at all. Even better if I could hypothetically put the default sort on the GridView and lose the whole Page_Load.

啊! 我的眼睛! 等等,这根本不吸引人。 如果我可以假设将默认排序放到GridView上,然后丢失整个Page_Load,那就更好了。

Whatever database or repository or Web (HTTP) Service you like, as long as your data access layer returns some IQueryables and you're in a good place. Sorting happens via LINQ so your data access layer can do the work, not ASP.NET.

无论您喜欢哪种数据库或存储库或Web(HTTP)服务,只要您的数据访问层返回一些IQueryable即可,并且您处在一个合适的位置。 排序是通过LINQ进行的,因此您的数据访问层可以完成工作,而不是ASP.NET。

So listing categories in a grid is decent, what's Edit look like?

因此,在网格中列出类别是很不错的,Edit的外观如何?

If you add Model Binding to ASP.NET WebForms you spend less time digging around in the Request object. Notice that we're not doing that all here.

如果将模型绑定添加到ASP.NET WebForms,则可以花费更少的时间来研究Request对象。 请注意,我们并没有在这里做所有的事情。

See how the RouteData attribute on GetCategory pulls the id out of the URL Categories/1?

看看GetCategory上的RouteData属性如何将ID从URL Categories / 1中拉出来?

public partial class Categories_Edit : System.Web.UI.Page
{
private readonly DemoWhateverDataContext _db = new DemoWhateverDataContext();

public Category GetCategory([RouteData]int? id)
{
return _db.Categories.Find(id);
}

public int UpdateCategory(int categoryId /* Comes from the data control itself via DataKeyNames property */)
{
var category = _db.Categories.Find(categoryId);
TryUpdateModel(category);
if (ModelState.IsValid)
{
return _db.SaveChanges();
}
return 0;
}

protected void categoriesForm_ItemUpdated(object sender, FormViewUpdatedEventArgs e)
{
Response.RedirectToRoute("Categories");
}

protected void categoriesForm_ItemCommand(object sender, FormViewCommandEventArgs e)
{
if (e.IsCancel())
{
Response.RedirectToRoute("Categories");
}
}
}

Often folks point to ASP.NET MVC's ability to use PRG (Post Redirect Get) as a strength. Often PostBacks in WebForms are looked down upon. In this model above, we're also totally able to use the PRG interaction model in Web Forms. See how the item is updated and we redirect to a route.

经常有人指出ASP.NET MVC使用PRG(后重定向获取)的能力是一种优势。 WebForms中的PostBacks通常被看不起。 在上面的模型中,我们还完全能够在Web窗体中使用PRG交互模型。 查看商品的更新方式,然后我们重定向到路线。

And the categoryId on UpdateCategory() comes from the Form View that is HTTP posting the data back. Here's a snippet:

并且UpdateCategory()上的categoryId来自于HTTP将数据回传的Form View。 这是一个片段:

<asp:FormView runat="server" ID="categoriesForm" RenderOuterTable="false"
ItemType="VS11Experiment.Model.Category" DataKeyNames="CategoryId"
DefaultMode="Edit"
SelectMethod="GetCategory" UpdateMethod="UpdateCategory"
OnItemUpdated="categoriesForm_ItemUpdated"
OnItemCommand="categoriesForm_ItemCommand">

Also, you know how in ASP.NET MVC you've got unobtrusive JavaScript validation that is driven by the model class itself?

另外,您知道在ASP.NET MVC中如何由模型类本身驱动JavaScript验证不引人注意吗?

In ASP.NET MVC one often uses EditorFor, and in Web Forms we've got Dynamic Control. The idea being that Dates get Calendars and you can replace the UI completely using a field template. That feature actually started in Web Forms Dynamic Data and then moved to ASP.NET MVC. Features move both ways when it's all ASP.NET underneath. See what I did there?

在ASP.NET MVC中,人们经常使用EditorFor,而在Web Forms中,我们具有Dynamic Control。 日期是日历的想法,您可以使用字段模板完全替换UI。 该功能实际上始于Web窗体动态数据,然后移至ASP.NET MVC。 当功能全部位于ASP.NET下方时,功能会双向移动。 看看我在那里做什么?

<EditItemTemplate>
<ol>
<li><label>Name:</label>
<asp:DynamicControl runat="server" ID="name" DataField="Name" Mode="Edit" />
</li>
<li><label>Description:</label>
<asp:DynamicControl runat="server" ID="description" DataField="Description" Mode="Edit" />
</li>
</ol>
...

And when these forms are POSTed, you'll need validation. Rather than Validation Controls, in this case since we already know about the model we can use unobtrusive validation, similar to ASP.NET MVC. The idea is push the best ideas into the core of ASP.NET and make common stuff easy while letting people work the way they want to work.

这些表格过帐后,您需要进行验证。 在这种情况下,由于我们已经知道该模型,因此可以使用无障碍验证,而不是验证控件,类似于ASP.NET MVC。 这个想法是将最好的想法推入ASP.NET的核心,并使常见的事情变得容易,同时让人们按照自己想要的方式工作。

public class Category
{
[ScaffoldColumn(false), Display(Name="Id")]
public long CategoryId { get; set; }

[Required, StringLength(100)]
public string Name { get; set; }

[StringLength(10000), DataType(DataType.MultilineText)]
public string Description { get; set; }
}

Sure, ASP.NET Web Forms may not be your cup of tea just like ASP.NET MVC might not be either. But remember that it's all One ASP.NET, and you've got a number of tools in your toolkit. Pick the ones that make you happy.

当然,ASP.NET Web窗体可能也不是您的最佳选择,就像ASP.NET MVC也不一样。 但是请记住,所有这些都是一个ASP.NET,并且您的工具箱中有许多工具。 选择那些让你开心的东西。

*And no, Hell isn't a dirty word in this context. ;)

*而且不,在这种情况下地狱不是一个脏话。 ;)

相关链接 (Related Links)

翻译自: https://www.hanselman.com/blog/one-aspnet-sneak-peek-elegant-web-forms-and-snowballs-in-hell

asp.net 关闭父窗体

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值