Kentico中UniGrid的使用
https://docs.xperience.io/k12sp/custom-development/ui-controls/unigrid
Getting started
The following is a step-by-step tutorial that shows how to display a table containing all users from the Kentico database using the UniGrid control, and implement a custom action button:
- Create a Web form named User_UniGrid.aspx in your web project (the code in the example assumes that you add the file into a subfolder named UniGridExample).
-
Add the following directives to the beginning of the web form's markup to register the UniGrid control:
<%@ Register src="~/CMSAdminControls/UI/UniGrid/UniGrid.ascx" tagname="UniGrid" tagprefix="cms" %> <%@ Register Namespace="CMS.UIControls.UniGridConfig" TagPrefix="ug" Assembly="CMS.UIControls" %>
-
Add the following code into the content area of the page (inside the <form> element):
<div class="cms-bootstrap"> <asp:ScriptManager ID="manScript" runat="server" ScriptMode="Release" EnableViewState="false" /> <asp:Label runat="server" ID="lblInfo" EnableViewState="false" Visible="false" /> <cms:UniGrid ID="UserGrid" runat="server" /> </div>
This adds the following controls:
- The ScriptManager control is required by the UniGrid control. The sample code manually adds the script manager to be functional as a standalone example. The ScriptManager is typically included on your website's master page, so you do not need to add it in real-world scenarios.
- A standard Label that will be used to display information messages. The label is not necessary for the functioning of the UniGrid, but can be very convenient, for example to display error messages.
- The UniGrid control itself (without any configuration for now).
The cms-bootstrap class is required if you wish to use the default UniGrid styles (including font icons).
-
Extend the definition of the UniGrid control according to the markup below:
<cms:UniGrid ID="UserGrid" runat="server" ObjectType="cms.user" Columns="UserID, UserName" OrderBy="UserName"> <GridActions> <ug:Action Name="view" Caption="$General.View$" FontIconClass="icon-eye" FontIconStyle="allow" /> </GridActions> <GridColumns> <ug:Column Source="UserName" Caption="$general.username$" Width="100%" /> </GridColumns> </cms:UniGrid>
The basic configuration example above defines a single action (view) and one column containing user names. The control retrieves the data from user objects, which is achieved by setting the ObjectType property to cms.user.
For more details and a full account of the configuration options you can specify for the UniGrid, see:
-
Switch to the web form's code behind (User_UniGrid.aspx.cs) and add the following code:
Note: Adjust the name of the class according to the location of your web form.
using System; using CMS.Helpers; using CMS.Membership; using CMS.Base.Web.UI; public partial class UniGridExample_User_UniGrid : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { // Registers the default CSS and JavaScript files onto the page (used to style the UniGrid) CssRegistration.RegisterBootstrap(Page); ScriptHelper.RegisterBootstrapScripts(Page); // Assigns a handler for the OnAction event UserGrid.OnAction += userGrid_OnAction; } /// <summary> // Handles the UniGrid's OnAction event. /// </summary> protected void userGrid_OnAction(string actionName, object actionArgument) { // Implements the logic of the view action if (actionName == "view") { // Stores the values of the actionArgument argument (UserID) int userId = ValidationHelper.GetInteger(actionArgument, 0); // Gets a UserInfo object representing the user with the given ID UserInfo ui = UserInfoProvider.GetUserInfo(userId); // If the user exists if (ui != null) { // Sets the information label to display the full name of the viewed user lblInfo.Visible = true; lblInfo.Text = "Viewed user: " + HTMLHelper.HTMLEncode(ui.FullName); } } } }
This code demonstrates how to implement the logic of a UniGrid action. OnAction event handlers have the following parameters:
- string actionName – identifies which action raised the event. This example only has one action, but the UniGrid control often contains more in real scenarios. The name passed into the parameter is set through the UniGrid's definition in the Name attribute of individual Action elements.
- object actionArgument – passes the value of a data source column from the UniGrid row for which the action was used. The column can be specified through the configuration in the commandargument attribute of individual Action elements, otherwise the first column in the data source is used by default.
This example only displays the full name of the viewed user in the label above the UniGrid when the view button is clicked. You can implement any required action in a similar fashion.
- Save the web form and its code behind file.
- Right-click the web form in the Solution explorer and select View in Browser.
The resulting page displays a table containing user names and view action buttons. If you click the view action, the full name of the user on the given row appears above the grid.
需要配合以下代码,以及<div class="cms-bootstrap">
CssRegistration.RegisterBootstrap(Page);
ScriptHelper.RegisterBootstrapScripts(Page);
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2019-08-05 HearthBuddy BotManager
2019-08-05 HearthBuddy版本收集
2019-08-05 Could not attach to a Hearthstone process.
2019-08-05 C# 7 .NET / CLR / Visual Studio version requirements
2019-08-05 Difference between C# compiler version and language version
2019-08-05 HearthBuddy CSharpCodeProvider 如何编译cs文件 升级编译器
2019-08-05 HearthBuddy DONOTDELETE.bin