如何在 Telerik 中以编程方式构建 CommandItemTemplate?
我有一个关于 Telerik 的问题。我有一个 RadGrid,在 MasterTableView 内部有一个 DetailTables 标签(因此我有一个层次结构),在 MasterTableView 标签内也有一个 CommandItemTemplate 标签。当它从标记运行时一切都很好,但我尝试从后面的代码以编程方式构建它。不幸的是我没有成功。一切正常,除了我的 CommandItemTemplate 不存在。
我的做法: 我的 MasterTableView 中有这个 CommandItemTemplate:
<CommandItemTemplate>
<div class="cmdItem">
<asp:LinkButton ID="btnAddShift" runat="server" CommandName="AddShift" OnClientClick="return fireCommand('AddShift', '');">
<img alt="" src="../../Images/Icons/AddRecord.png" />Add Shift</asp:LinkButton>   
<asp:LinkButton ID="btnExportCSV" runat="server" CommandName="Export CSV" OnClientClick="return exportGrid('CSV');"><img alt="" src="../../Images/Icons/ExportCSV.png" />Export to CSV</asp:LinkButton>   
<asp:LinkButton ID="btnManageShiftColumns" runat="server" CommandName="ManageShiftColumns" OnClientClick="return fireCommand('ManageShiftColumns', '');">
<img alt="" src="../../Images/Icons/Columns.png" />Manage Shift Columns</asp:LinkButton>   
<asp:LinkButton ID="btnManageJobColumns" runat="server" CommandName="ManageJobColumns" OnClientClick="return fireCommand('ManageJobColumns', '');">
<img alt="" src="../../Images/Icons/Columns.png" />Manage Job Columns</asp:LinkButton>   
<%--<asp:LinkButton ID="btnDetailedExportCSV" runat="server" CommandName="Export CSV Detailed"
OnClientClick="return exportGrid('CSVDetailed');"><img alt="" src="../../Images/Icons/ExportCSV.png" />Detailed Export to CSV</asp:LinkButton>   
<asp:LinkButton ID="btnExportPDF" runat="server" CommandName="Export PDF" OnClientClick="return exportGrid('PDF');"><img alt="" src="../../Images/Icons/ExportPDF.png" />Export to PDF</asp:LinkButton>   --%>
</div>
</CommandItemTemplate>
我已经摆脱了该标记,并且创建了一个辅助类:
Private Class RadGridHeaderTemplate
Implements ITemplate
Public Sub New()
End Sub
Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn
Dim div As HtmlGenericControl = New HtmlGenericControl("div")
div.Attributes.Add("class", "cmdItem")
Dim addShiftButton As LinkButton = New LinkButton With {.ID = "btnAddShift", .CommandName = "AddShift", .OnClientClick = "return fireCommand('AddShift', '');"}
Dim addShiftButtonImage As HtmlGenericControl = New HtmlGenericControl("img")
addShiftButtonImage.Attributes.Add("alt", "")
addShiftButtonImage.Attributes.Add("src", "../../Images/Icons/AddRecord.png")
addShiftButton.Controls.Add(addShiftButtonImage)
addShiftButton.Text = "Add Shift"
Dim exportCSVButton As LinkButton = New LinkButton With {.ID = "btnExportCSV", .CommandName = "Export CSV", .OnClientClick = "return exportGrid('CSV');"}
Dim exportCSVButtonImage As HtmlGenericControl = New HtmlGenericControl("img")
exportCSVButtonImage.Attributes.Add("alt", "")
exportCSVButtonImage.Attributes.Add("src", "../../Images/Icons/ExportCSV.png")
exportCSVButton.Controls.Add(exportCSVButtonImage)
exportCSVButton.Text = "Export to CSV"
Dim manageShiftColumnButton As LinkButton = New LinkButton With {.ID = "btnManageShiftColumns", .CommandName = "ManageShiftColumns", .OnClientClick = "return fireCommand('ManageShiftColumns', '');"}
Dim manageShiftColumnButtonImage As HtmlGenericControl = New HtmlGenericControl("img")
manageShiftColumnButtonImage.Attributes.Add("alt", "")
manageShiftColumnButtonImage.Attributes.Add("src", "../../Images/Icons/Columns.png")
manageShiftColumnButton.Controls.Add(manageShiftColumnButtonImage)
manageShiftColumnButton.Text = "Manage Shift Columns"
Dim manageJobColumnButton As LinkButton = New LinkButton With {.ID = "btnManageJobColumns", .CommandName = "ManageJobColumns", .OnClientClick = "return fireCommand('ManageJobColumns', '');"}
Dim manageJobColumnButtonImage As HtmlGenericControl = New HtmlGenericControl("img")
manageJobColumnButtonImage.Attributes.Add("alt", "")
manageJobColumnButtonImage.Attributes.Add("src", "../../Images/Icons/Columns.png")
manageJobColumnButton.Controls.Add(manageJobColumnButtonImage)
manageJobColumnButton.Text = "Manage Job Columns"
div.Controls.Add(addShiftButton)
div.Controls.Add(exportCSVButton)
div.Controls.Add(manageShiftColumnButton)
div.Controls.Add(manageJobColumnButton)
container.Controls.Add(div)
End Sub
End Class
我在 CreateGrid 方法中使用这个类,在该方法中以编程方式创建 RadGrid 并将其添加到页面的控件中。我以这种方式使用它:
Protected Sub CreateGrid()
RadGrid1 = New RadGrid With {.ID = "RadGrid1", .AutoGenerateColumns = True, .AllowCustomPaging = True, .AllowMultiRowSelection = True}
'...
Dim template As RadGridHeaderTemplate = New RadGridHeaderTemplate()
template.InstantiateIn(RadGrid1.MasterTableView)
'...
End Sub
不幸的是,这种方法不起作用。我没有任何错误,但我的 CommandItemTemplate 不存在,我的 LinkButton 没有出现在 UI 上,它们不是生成的标记的一部分。
任何帮助表示赞赏。
最好的问候,
拉约斯·阿帕德。
I have a question about Telerik. I have a RadGrid where inside the MasterTableView I have a DetailTables tag (so I have a hierarchy) and I also have a CommandItemTemplate tag inside my MasterTableView tag. Everything is fine when it runs from markup, but I've tried to build this programmatically from code behind. Unfortunately I was not successful. Everything works, except that my CommandItemTemplate is nowhere.
My approach:
I had this CommandItemTemplate inside my MasterTableView:
<CommandItemTemplate>
<div class="cmdItem">
<asp:LinkButton ID="btnAddShift" runat="server" CommandName="AddShift" OnClientClick="return fireCommand('AddShift', '');">
<img alt="" src="../../Images/Icons/AddRecord.png" />Add Shift</asp:LinkButton>
<asp:LinkButton ID="btnExportCSV" runat="server" CommandName="Export CSV" OnClientClick="return exportGrid('CSV');"><img alt="" src="../../Images/Icons/ExportCSV.png" />Export to CSV</asp:LinkButton>
<asp:LinkButton ID="btnManageShiftColumns" runat="server" CommandName="ManageShiftColumns" OnClientClick="return fireCommand('ManageShiftColumns', '');">
<img alt="" src="../../Images/Icons/Columns.png" />Manage Shift Columns</asp:LinkButton>
<asp:LinkButton ID="btnManageJobColumns" runat="server" CommandName="ManageJobColumns" OnClientClick="return fireCommand('ManageJobColumns', '');">
<img alt="" src="../../Images/Icons/Columns.png" />Manage Job Columns</asp:LinkButton>
<%--<asp:LinkButton ID="btnDetailedExportCSV" runat="server" CommandName="Export CSV Detailed"
OnClientClick="return exportGrid('CSVDetailed');"><img alt="" src="../../Images/Icons/ExportCSV.png" />Detailed Export to CSV</asp:LinkButton>
<asp:LinkButton ID="btnExportPDF" runat="server" CommandName="Export PDF" OnClientClick="return exportGrid('PDF');"><img alt="" src="../../Images/Icons/ExportPDF.png" />Export to PDF</asp:LinkButton> --%>
</div>
</CommandItemTemplate>
I've got rid of that markup and I've created a helper class:
Private Class RadGridHeaderTemplate
Implements ITemplate
Public Sub New()
End Sub
Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn
Dim div As HtmlGenericControl = New HtmlGenericControl("div")
div.Attributes.Add("class", "cmdItem")
Dim addShiftButton As LinkButton = New LinkButton With {.ID = "btnAddShift", .CommandName = "AddShift", .OnClientClick = "return fireCommand('AddShift', '');"}
Dim addShiftButtonImage As HtmlGenericControl = New HtmlGenericControl("img")
addShiftButtonImage.Attributes.Add("alt", "")
addShiftButtonImage.Attributes.Add("src", "../../Images/Icons/AddRecord.png")
addShiftButton.Controls.Add(addShiftButtonImage)
addShiftButton.Text = "Add Shift"
Dim exportCSVButton As LinkButton = New LinkButton With {.ID = "btnExportCSV", .CommandName = "Export CSV", .OnClientClick = "return exportGrid('CSV');"}
Dim exportCSVButtonImage As HtmlGenericControl = New HtmlGenericControl("img")
exportCSVButtonImage.Attributes.Add("alt", "")
exportCSVButtonImage.Attributes.Add("src", "../../Images/Icons/ExportCSV.png")
exportCSVButton.Controls.Add(exportCSVButtonImage)
exportCSVButton.Text = "Export to CSV"
Dim manageShiftColumnButton As LinkButton = New LinkButton With {.ID = "btnManageShiftColumns", .CommandName = "ManageShiftColumns", .OnClientClick = "return fireCommand('ManageShiftColumns', '');"}
Dim manageShiftColumnButtonImage As HtmlGenericControl = New HtmlGenericControl("img")
manageShiftColumnButtonImage.Attributes.Add("alt", "")
manageShiftColumnButtonImage.Attributes.Add("src", "../../Images/Icons/Columns.png")
manageShiftColumnButton.Controls.Add(manageShiftColumnButtonImage)
manageShiftColumnButton.Text = "Manage Shift Columns"
Dim manageJobColumnButton As LinkButton = New LinkButton With {.ID = "btnManageJobColumns", .CommandName = "ManageJobColumns", .OnClientClick = "return fireCommand('ManageJobColumns', '');"}
Dim manageJobColumnButtonImage As HtmlGenericControl = New HtmlGenericControl("img")
manageJobColumnButtonImage.Attributes.Add("alt", "")
manageJobColumnButtonImage.Attributes.Add("src", "../../Images/Icons/Columns.png")
manageJobColumnButton.Controls.Add(manageJobColumnButtonImage)
manageJobColumnButton.Text = "Manage Job Columns"
div.Controls.Add(addShiftButton)
div.Controls.Add(exportCSVButton)
div.Controls.Add(manageShiftColumnButton)
div.Controls.Add(manageJobColumnButton)
container.Controls.Add(div)
End Sub
End Class
I'm using this class inside my CreateGrid method where I create my RadGrid programmatically and adding it to the controls of the page. I'm using it this way:
Protected Sub CreateGrid()
RadGrid1 = New RadGrid With {.ID = "RadGrid1", .AutoGenerateColumns = True, .AllowCustomPaging = True, .AllowMultiRowSelection = True}
'...
Dim template As RadGridHeaderTemplate = New RadGridHeaderTemplate()
template.InstantiateIn(RadGrid1.MasterTableView)
'...
End Sub
Unfortunately this approach doesn't work. I don't have any errors, but my CommandItemTemplate is nowhere, my LinkButtons don't appear on the UI, they are not part of the generated markup.
Any help is appreciated.
Best regards,
Lajos Arpad.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在我的源代码中,缺少以下部分:
由于 RadGrid1.MasterTableView.CommandItemDisplay 已初始化,我可以看到我的项目命令模板,并且可以使用我的项目命令模板提供的功能。
In my source code the following part was missing:
Since RadGrid1.MasterTableView.CommandItemDisplay is initialized I can see my item command template and I can use the features provided by my item command template.