I am making a simple grid control using the radgrid which derives its structure(columns) from an xml and databind using client side web serivce. It works fine, render intially, paging/sorting work fine, I implement the client OnRowDataBound event and render hyperlinks also. When a postback happens from any other buttons on the page, the columns in the grid are duplicated.
Following is my simplified code:
aspx:
<asp:Button ID="btnPostBack" runat="server" Text="PostBack" /> |
<telerik:RadGrid ID="grid" runat="server" GridLines="None" AllowFilteringByColumn="false" |
AllowSorting="true" AllowPaging="true" PageSize="10" AutoGenerateColumns="false"> |
<ClientSettings> |
<DataBinding SelectMethod="GetResults" /> |
<ClientEvents /> |
</ClientSettings> |
<MasterTableView Width="100%" AutoGenerateColumns="false"> |
</MasterTableView> |
<PagerStyle Mode="NextPrevAndNumeric" /> |
</telerik:RadGrid> |
Code behind:
protected override void OnInit(EventArgs e) |
{ |
base.OnInit(e); |
FillColumns(); |
} |
[Serializable] |
public class Field |
{ |
public String ColumnName { get; set; } |
public String DisplayName { get; set; } |
} |
private void FillColumns() |
{ |
List<Field> fields = new List<Field> |
{ |
new Field { DisplayName = "ID", ColumnName = "ID" }, |
new Field { DisplayName = "Name", ColumnName = "Name" }, |
new Field { DisplayName = "Description", ColumnName = "Description" }, |
new Field { DisplayName = "Location", ColumnName = "Location" } |
}; |
grid.MasterTableView.Columns.Clear(); |
foreach (var field in fields) |
{ |
GridBoundColumn col = new GridBoundColumn(); |
grid.MasterTableView.Columns.Add(col); |
col.DataField = field.ColumnName; |
col.HeaderText = field.DisplayName; |
if (field.ColumnName == "ID") |
col.DataType = Type.GetType("System.Int32"); |
} |
} |
protected void Page_Load(object sender, EventArgs e) |
{ |
grid.ClientSettings.DataBinding.Location = ResolveUrl("~/SearchService.asmx"); |
} |
ASMX:
[WebService(Namespace = "http://tempuri.org/")] |
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] |
[System.ComponentModel.ToolboxItem(false)] |
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. |
[System.Web.Script.Services.ScriptService] |
public class SearchService : System.Web.Services.WebService |
{ |
[WebMethod(EnableSession = true)] |
public Dictionary<String, Object> GetResults( |
int startRowIndex, int maximumRows, |
List<GridSortExpression> sortExpression, |
List<GridFilterExpression> filterExpression) |
{ |
DataTable dt = new DataTable(); |
dt.Columns.Add("ID", Type.GetType("System.Int32")); |
dt.Columns.Add("Name"); |
dt.Columns.Add("Description"); |
dt.Columns.Add("Location"); |
for (int i = 0; i < 35; i++) |
{ |
System.Data.DataRow dr = dt.NewRow(); |
dr["ID"] = i; |
dr["Name"] = "Name" + i; |
dr["Description"] = "Description" + i; |
dr["Location"] = "Location" + i; |
dt.Rows.Add(dr); |
} |
return new Dictionary<String, Object> |
{ |
{ "Data", RowsToDictionary(dt, dt.Select(string.Format(" ID > {0} and ID < {1}", startRowIndex.ToString(), (startRowIndex + maximumRows).ToString()))) }, |
{ "Count", dt.Rows.Count } |
}; |
} |
private static List<Dictionary<string, object>> RowsToDictionary(DataTable table, DataRow[] rows) |
{ |
List<Dictionary<string, object>> objs = |
new List<Dictionary<string, object>>(); |
foreach (DataRow dr in rows) |
{ |
Dictionary<string, object> drow = new Dictionary<string, object>(); |
for (int i = 0; i < table.Columns.Count; i++) |
{ |
drow.Add(table.Columns[i].ColumnName, dr[i]); |
} |
objs.Add(drow); |
} |
return objs; |
} |
Note, this is a simplified version and the control has more complicated things which are not relevant here.
Thank you,
Tilak