如何在Telerik中以编程方式构建CommandItemTemplate?

时间:2011-11-07 09:06:38

标签: asp.net vb.net telerik code-behind

我对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>&#160;&#160;&#160;
                <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>&#160;&#160;&#160;
                <asp:LinkButton ID="btnManageShiftColumns" runat="server" CommandName="ManageShiftColumns" OnClientClick="return fireCommand('ManageShiftColumns', '');">
                    <img alt="" src="../../Images/Icons/Columns.png" />Manage Shift Columns</asp:LinkButton>&#160;&#160;&#160;
                <asp:LinkButton ID="btnManageJobColumns" runat="server" CommandName="ManageJobColumns" OnClientClick="return fireCommand('ManageJobColumns', '');">
                    <img alt="" src="../../Images/Icons/Columns.png" />Manage Job Columns</asp:LinkButton>&#160;&#160;&#160;
                <%--<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>&#160;&#160;&#160;
                <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>&#160;&#160;&#160;--%>
            </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无处可去,我的LinkBut​​tons没有出现在UI上,它们不是生成的标记的一部分。

感谢任何帮助。

致以最诚挚的问候,

Lajos Arpad。

1 个答案:

答案 0 :(得分:1)

在我的源代码中,缺少以下部分:

RadGrid1.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.Top

由于RadGrid1.MasterTableView.CommandItemDisplay已初始化,我可以看到我的项目命令模板,我可以使用项目命令模板提供的功能。