Asp.net Dynamic Data之三改变编辑和操作数据的现实方式

Asp.net Dynamic Data之三改变编辑和操作数据的现实方式

本专题介绍如何运用RouteCollection 添加或是修改Routing URL规则实现对页面的控制.

默认情况下

从Global.asax代码中我们不难看出它的规则{Table}/{action}.aspx,action=List,Detail,Edit,Insert,那么一定存在List.aspx,Detail.aspx,Edit.aspx,Insert.aspx的web page,表示不同的表的CRUD操作对应不同的页面;

// The following statement supports separate-page mode, where the List, Detail, Insert, and

// Update tasks are performed by using separate pages. To enable this mode, uncomment the following

// route definition, and comment out the route definitions in the combined-page mode section that follows.

routes.Add(new DynamicDataRoute("{table}/{action}.aspx")

{

Constraints = new RouteValueDictionary(new { action = "List|Details|Edit|Insert" }),

Model = model

});

// The following statements support combined-page mode, where the List, Detail, Insert, and

// Update tasks are performed by using the same page. To enable this mode, uncomment the

// following routes and comment out the route definition in the separate-page mode section above.

//routes.Add(new DynamicDataRoute("{table}/ListDetails.aspx")

//{

// Action = PageAction.List,

// ViewName = "ListDetails",

// Model = model

//});

//routes.Add(new DynamicDataRoute("{table}/ListDetails.aspx")

//{

// Action = PageAction.Details,

// ViewName = "ListDetails",

// Model = model

//});

页面模板如下

提供了上述定义的{action}.aspx

显示的效果

页面部分的代码

注意看NavigateUrl的属性,通过GetActionPath动态获取Url,具体的用法我们后面再说明

<asp:GridView ID="GridView1" runat="server" DataSourceID="GridDataSource" 
AllowPaging="True" AllowSorting="True" CssClass="gridview"> 
<Columns> 
<asp:TemplateField> 
<ItemTemplate> 
<asp:HyperLink ID="EditHyperLink" runat="server" 
NavigateUrl='<%# table.GetActionPath(PageAction.Edit, GetDataItem()) %>' 
Text="Edit" />&nbsp;<asp:LinkButton ID="DeleteLinkButton" runat="server" CommandName="Delete" 
CausesValidation="false" Text="Delete" 
OnClientClick='return confirm("Are you sure you want to delete this item?");' 
/>&nbsp;<asp:HyperLink ID="DetailsHyperLink" runat="server" 
NavigateUrl='<%# table.GetActionPath(PageAction.Details, GetDataItem()) %>' 
Text="Details" /> 
</ItemTemplate> 
</asp:TemplateField> 
</Columns> 
<PagerStyle CssClass="footer"/> 
<PagerTemplate> 
<asp:GridViewPager runat="server" /> 
</PagerTemplate> 
<EmptyDataTemplate> 
There are currently no items in this table. 
</EmptyDataTemplate> 
</asp:GridView> 
改变一下编辑方式 
希望修改/新增/显示(Edit,Insert,List,Detail)在一个页面里面完成,那我们需要怎么做呢; 
修改一下Global.asax下的代码,注解掉上的语句,恢复下面的,URL的规则变成{table}/ListDetails.aspx,所有的操作将在同一个页面中完成; 
// The following statement supports separate-page mode, where the List, Detail, Insert, and 
// Update tasks are performed by using separate pages. To enable this mode, uncomment the following 
// route definition, and comment out the route definitions in the combined-page mode section that follows. 
//routes.Add(new DynamicDataRoute("{table}/{action}.aspx") 
//{ 
// Constraints = new RouteValueDictionary(new { action = "List|Details|Edit|Insert" }), 
// Model = model 
//}); 
// The following statements support combined-page mode, where the List, Detail, Insert, and 
// Update tasks are performed by using the same page. To enable this mode, uncomment the 
// following routes and comment out the route definition in the separate-page mode section above. 
routes.Add(new DynamicDataRoute("{table}/ListDetails.aspx") 
{ 
Action = PageAction.List, 
ViewName = "ListDetails", 
Model = model 
}); 
routes.Add(new DynamicDataRoute("{table}/ListDetails.aspx") 
{ 
Action = PageAction.Details, 
ViewName = "ListDetails", 
Model = model 
}); 

显示的效果

对一个表的操作都在同一个页面中完成;

页面部分的代码

这里我们看不到GetActionPath的方法了,完全按照通常的处理方式作了

<asp:GridView ID="GridView1" runat="server" DataSourceID="GridDataSource" 
AutoGenerateSelectButton="True" AutoGenerateEditButton="True" AutoGenerateDeleteButton="true" 
AllowPaging="True" AllowSorting="True" OnDataBound="OnGridViewDataBound" 
OnRowEditing="OnGridViewRowEditing" OnSelectedIndexChanging="OnGridViewSelectedIndexChanging" 
OnRowDeleted="OnGridViewRowDeleted" OnRowUpdated="OnGridViewRowUpdated" 
OnRowCreated="OnGridViewRowCreated" CssClass="gridview"> 
<PagerStyle CssClass="footer" /> 
<SelectedRowStyle CssClass="selected" /> 
<PagerTemplate> 
<asp:GridViewPager runat="server" /> 
</PagerTemplate> 
<EmptyDataTemplate> 
There are currently no items in this table. 
</EmptyDataTemplate> 
</asp:GridView> 

更加灵活的方式

我要实现对Products表的操作分在不同的页面,而像Categories表字段少的就在一个页面里做;

修改一下Global.asax下的代码,Url规则Products/{action}.aspx,Table="Products"。

// The following statement supports separate-page mode, where the List, Detail, Insert, and 
// Update tasks are performed by using separate pages. To enable this mode, uncomment the following 
// route definition, and comment out the route definitions in the combined-page mode section that follows. 
routes.Add(new DynamicDataRoute("Products/{action}.aspx") 
{ 
Constraints = new RouteValueDictionary(new { action = "List|Details|Edit|Insert" }), 
Table="Products", 
Model = model 
}); 
// The following statements support combined-page mode, where the List, Detail, Insert, and 
// Update tasks are performed by using the same page. To enable this mode, uncomment the 
// following routes and comment out the route definition in the separate-page mode section above. 
routes.Add(new DynamicDataRoute("{table}/ListDetails.aspx") 
{ 
Action = PageAction.List, 
ViewName = "ListDetails", 
Model = model 
}); 
routes.Add(new DynamicDataRoute("{table}/ListDetails.aspx") 
{ 
Action = PageAction.Details, 
ViewName = "ListDetails", 
Model = model 
}); 

下期将如何改变字段的现实/编辑方式,可以是Rich_Text,Date_Edit,Number

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏从零开始学自动化测试

appium+python自动化34-获取元素属性

前言 在做断言的时候,我们往往需要先获取目标元素的某些属性值,拿来判断,比如登录后,如何判断登录成功了呢?可以通过登录后获取账号的名称(元素的text属性)来判...

44540
来自专栏C/C++基础

Linux命令(46)——read命令

read命令是Shell内建命令,用于从标准输入或-u选项指定的文件描述符中读取单行,并将读取的单行根据IFS变量分割成多个字段,并将分割后的字段分别赋值给指定...

14820
来自专栏ShaoYL

预处理指令(文件的包含)

33590
来自专栏Java后端技术

JVM 运行时数据区详解

  Java虚拟机在执行Java程序的过程中会把它所管理的内存划分为若干个不同数据区域。

8930
来自专栏用户2442861的专栏

Nginx源码剖析之内存池,与内存管理

    Nginx(发音同 engine x)是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like...

19840
来自专栏程序员的知识天地

Python3 错误和异常

作为Python初学者,在刚学习Python编程时,经常会看到一些报错信息,在前面我们没有提及,这章节我们会专门介绍。

18820
来自专栏牛客网

知识总结:C++工程师106道面试题总结(含答案详解)

可以说个人秋招就要结束了,就等两个offer通知,然后签完搞定,这里提供一下自己复习的东西吧,我也就把这个东西给搞了一遍,然后面试基本没啥问题了,如果问的很深的...

44790
来自专栏猿人谷

用C来实现内存池

介绍:        设计内存池的目标是为了保证服务器长时间高效的运行,通过对申请空间小而申请频繁的对象进行有效管理,减少内存碎片的产生,合理分配管理用户内存,...

58870
来自专栏软件工程师成长笔记

Checkstyle提示

解决方法:在方法前得注释中添加这样一行:* @throws Exception if has error(异常说明)

22220
来自专栏游戏杂谈

php的Snoopy类

获取请求网页里面的所有链接,直接使用fetchlinks就可以,获取所有文本信息使用fetchtext(其内部还是使用正则表达式在进行处理),还有其它较多的功能...

14130

扫码关注云+社区

领取腾讯云代金券