我有一個關於Telerik的問題。我有一個RadGrid裏面的MasterTableView我有一個DetailTables標籤(所以我有一個層次結構),我也有我的MasterTableView標籤的CommandItemTemplate標籤。當它從標記運行時,一切都很好,但我試圖從後面的代碼編程構建。不幸的是我沒有成功。一切正常,除了我的CommandItemTemplate無處。如何以編程方式在Telerik中構建CommandItemTemplate?
我的方法: 我有這個CommandItemTemplate我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>
我已經擺脫了那個標記,我已經創建了一個輔助類:
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無處可用,我的LinkButtons不會出現在UI上,它們不是生成的標記的一部分。
任何幫助表示讚賞。
此致敬意,
Lajos Arpad。