I got some problems with using template columns. Rad Version - 2008.3.1016.35
The question is: why DataBinding template event doesn't fires after postback and can it be fixed?
To illustrate it I done the test described below.
And ASPX:
Please, push the button "POST" and look at the column "test".
No DataBinding event, no values at column "test"
The question is: why DataBinding template event doesn't fires after postback and can it be fixed?
To illustrate it I done the test described below.
using System; |
using System.Data; |
using System.Data.SqlClient; |
using System.Web.UI; |
using System.Web.UI.WebControls; |
using Telerik.Web.UI; |
namespace Test.WebApplication |
{ |
public partial class postbacktest : System.Web.UI.Page |
{ |
protected RadGrid dataGrid; |
private string keyColumnName; |
protected void Page_Init(object sender, EventArgs e) |
{ |
dataGrid = RadGrid1; |
dataGrid.Skin = "Office2007"; |
dataGrid.Width = 800; |
dataGrid.AllowAutomaticDeletes = false; |
dataGrid.AllowAutomaticInserts = false; |
dataGrid.AllowAutomaticUpdates = false; |
dataGrid.AutoGenerateColumns = false; |
dataGrid.AutoGenerateEditColumn = false; |
dataGrid.AutoGenerateDeleteColumn = false; |
dataGrid.MasterTableView.EditMode = GridEditMode.InPlace; |
dataGrid.MasterTableView.ShowFooter = true; |
dataGrid.EnableAjaxSkinRendering = true; |
BindGridToDataTable(); |
//AddEditColumn(); |
AddTextColumn("first", "ProductName"); |
AddTextColumn("id", "ProductID"); |
AddNumericColumn("second", "Number", NumericType.Currency); |
AddColumn("test", "ComboID"); |
AddDeleteColumn(); |
} |
public void AddColumn(string headerText, string dataField) |
{ |
GridTemplateColumn templateColumn = new GridTemplateColumn(); |
BPTemplate temp = new BPTemplate(dataField, keyColumnName); |
templateColumn.ItemTemplate = temp; |
templateColumn.EditItemTemplate = temp; |
templateColumn.HeaderText = headerText; |
templateColumn.UniqueName = dataField; |
dataGrid.MasterTableView.Columns.Add(templateColumn); |
} |
public void AddTextColumn(string headerText, string dataField) |
{ |
GridBoundColumn boundColumn = new GridBoundColumn(); |
boundColumn.HeaderText = headerText; |
boundColumn.DataField = dataField; |
boundColumn.UniqueName = dataField; |
dataGrid.MasterTableView.Columns.Add(boundColumn); |
} |
public void AddNumericColumn(string headerText, string dataField, NumericType type) |
{ |
GridNumericColumn numericColumn = new GridNumericColumn(); |
numericColumn.HeaderText = headerText; |
numericColumn.DataField = dataField; |
numericColumn.UniqueName = dataField; |
numericColumn.NumericType = type; |
dataGrid.MasterTableView.Columns.Add(numericColumn); |
} |
public void AddDeleteColumn() |
{ |
GridButtonColumn buttonColumn = new GridButtonColumn(); |
buttonColumn.ButtonType = GridButtonColumnType.ImageButton; |
buttonColumn.UniqueName = "DeleteCol"; |
buttonColumn.CommandName = "Delete"; |
buttonColumn.Text = "Delete"; |
buttonColumn.ItemStyle.HorizontalAlign = HorizontalAlign.Center; |
buttonColumn.ItemStyle.CssClass = "MyImageButton"; |
dataGrid.MasterTableView.Columns.Add(buttonColumn); |
} |
public void AddEditColumn() |
{ |
GridEditCommandColumn editCommandColumn = new GridEditCommandColumn(); |
editCommandColumn.UniqueName = "EditCol"; |
editCommandColumn.ButtonType = GridButtonColumnType.ImageButton; |
editCommandColumn.ItemStyle.CssClass = "MyImageButton"; |
dataGrid.MasterTableView.Columns.Add(editCommandColumn); |
} |
private void BindGridToDataTable() |
{ |
SqlConnection con = new SqlConnection("Data Source=;Initial Catalog=Northwind;Integrated Security=True"); |
SqlDataAdapter adapter = new SqlDataAdapter("SELECT TOP 5 [ProductID], cast([ProductID] as numeric) as Number,[ProductName],[ProductID] as ComboID FROM [Products]", con); |
DataTable links = new DataTable(); |
adapter.Fill(links); |
DataBindGrid("ProductID", links); |
} |
public void DataBindGrid(string keyColName, DataTable data) |
{ |
dataGrid.DataSource = data.DefaultView; |
keyColumnName = keyColName; |
dataGrid.MasterTableView.DataKeyNames = new string[] { keyColumnName }; |
} |
} |
public class BPTemplate : ITemplate |
{ |
protected LiteralControl lControl; |
private string colname; |
private DataTable controlData; |
private string keyColName; |
public BPTemplate(string columnName, string keyColumnName) |
{ |
colname = columnName; |
keyColName = keyColumnName; |
} |
public void InstantiateIn(Control container) |
{ |
lControl = new LiteralControl(); |
lControl.ID = "lControl"; |
lControl.DataBinding += lControl_DataBinding; |
container.Controls.Add(lControl); |
} |
public void lControl_DataBinding(object sender, EventArgs e) |
{ |
LiteralControl l = (LiteralControl)sender; |
GridDataItem container = (GridDataItem)l.NamingContainer; |
var value = ((DataRowView)container.DataItem)[colname].ToString(); |
l.Text = value; |
} |
} |
} |
And ASPX:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="postbacktest.aspx.cs" Inherits="Test.WebApplication.postbacktest" %> |
<%@ Register assembly="Telerik.Web.UI" namespace="Telerik.Web.UI" tagprefix="telerik" %> |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
<html xmlns="http://www.w3.org/1999/xhtml" > |
<head runat="server"> |
<title></title> |
</head> |
<body> |
<script type="text/javascript"> |
function Post() { |
__doPostBack("", ""); |
} |
</script> |
<form id="form1" runat="server"> |
<div> |
<telerik:RadScriptManager ID="RadScriptManager1" Runat="server"> |
</telerik:RadScriptManager> |
</div> |
<telerik:RadGrid ID="RadGrid1" runat="server"> |
</telerik:RadGrid> |
<button id="button" onclick="Post()">POST</button> |
</form> |
</body> |
</html> |
Please, push the button "POST" and look at the column "test".
No DataBinding event, no values at column "test"