当前位置:首页 > asp.net网站教程(3):改变编辑和操作数据的现实方式

asp.net网站教程(3):改变编辑和操作数据的现实方式

点击次数:1327  更新日期:2010-12-31
\n


\n

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


\n

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

\n

默认情况下


\n

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


\n

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


\n

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


\n

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


\n

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


\n

{


\n

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


\n

Model = model


\n

});

\n

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


\n

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


\n

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


\n

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


\n

//{


\n

// Action = PageAction.List,


\n

// ViewName = “ListDetails”,


\n

// Model = model


\n

//});

\n

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


\n

//{


\n

// Action = PageAction.Details,


\n

// ViewName = “ListDetails”,


\n

// Model = model


\n

//});


\n

页面模板如下


\n

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


\n

\n

显示的效果


\n


\n

页面部分的代码


\n

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


\n

<asp:GridView ID=”GridView1″ runat=”server” DataSourceID=”GridDataSource”


\n

AllowPaging=”True” AllowSorting=”True” CssClass=”gridview”>


\n

<Columns>


\n

<asp:TemplateField>


\n

<ItemTemplate>


\n

<asp:HyperLink ID=”EditHyperLink” runat=”server”


\n

NavigateUrl=’<%# table.GetActionPath(PageAction.Edit, GetDataItem()) %>’


\n

Text=”Edit” />&nbsp;<asp:LinkButton ID=”DeleteLinkButton” runat=”server” CommandName=”Delete”


\n

CausesValidation=”false” Text=”Delete”


\n

OnClientClick=’return confirm(“Are you sure you want to delete this item?”);’


\n

/>&nbsp;<asp:HyperLink ID=”DetailsHyperLink” runat=”server”


\n

NavigateUrl=’<%# table.GetActionPath(PageAction.Details, GetDataItem()) %>’


\n

Text=”Details” />


\n

</ItemTemplate>


\n

</asp:TemplateField>


\n

</Columns>

\n

<PagerStyle CssClass=”footer”/>


\n

<PagerTemplate>


\n

<asp:GridViewPager runat=”server” />


\n

</PagerTemplate>


\n

<EmptyDataTemplate>


\n

There are currently no items in this table.


\n

</EmptyDataTemplate>


\n

</asp:GridView>


\n

改变一下编辑方式


\n

希望修改/新增/显示(Edit,Insert,List,Detail)在一个页面里面完成,那我们需要怎么做呢;


\n

修改一下Global.asax下的代码,注解掉上的语句,恢复下面的,URL的规则变成{table}/ListDetails.aspx,所有的操作将在同一个页面中完成;


\n

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


\n

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


\n

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


\n

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


\n

//{


\n

// Constraints = new RouteValueDictionary(new { action = “List|Details|Edit|Insert” }),


\n

// Model = model


\n

//});

\n

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


\n

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


\n

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


\n

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


\n

{


\n

Action = PageAction.List,


\n

ViewName = “ListDetails”,


\n

Model = model


\n

});

\n

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


\n

{


\n

Action = PageAction.Details,


\n

ViewName = “ListDetails”,


\n

Model = model


\n

});

\n

显示的效果


\n

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


\n


\n

页面部分的代码


\n

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


\n

<asp:GridView ID=”GridView1″ runat=”server” DataSourceID=”GridDataSource”


\n

AutoGenerateSelectButton=”True” AutoGenerateEditButton=”True” AutoGenerateDeleteButton=”true”


\n

AllowPaging=”True” AllowSorting=”True” OnDataBound=”OnGridViewDataBound”


\n

OnRowEditing=”OnGridViewRowEditing” OnSelectedIndexChanging=”OnGridViewSelectedIndexChanging”


\n

OnRowDeleted=”OnGridViewRowDeleted” OnRowUpdated=”OnGridViewRowUpdated”


\n

OnRowCreated=”OnGridViewRowCreated” CssClass=”gridview”>

\n

<PagerStyle CssClass=”footer” />


\n

<SelectedRowStyle CssClass=”selected” />


\n

<PagerTemplate>


\n

<asp:GridViewPager runat=”server” />


\n

</PagerTemplate>


\n

<EmptyDataTemplate>


\n

There are currently no items in this table.


\n

</EmptyDataTemplate>


\n

</asp:GridView>

\n

更加灵活的方式


\n

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


\n

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

\n

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


\n

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


\n

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


\n

routes.Add(new DynamicDataRoute(“Products/{action}.aspx”)


\n

{


\n

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


\n

Table=”Products”,


\n

Model = model


\n

});

\n

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


\n

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


\n

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


\n

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


\n

{


\n

Action = PageAction.List,


\n

ViewName = “ListDetails”,


\n

Model = model


\n

});

\n

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


\n

{


\n

Action = PageAction.Details,


\n

ViewName = “ListDetails”,


\n

Model = model


\n

});

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

\n