Apr 19, 2010, 5:18 PM
Composite Server Control (grid+paging toolbar+store)
hi,
here is the code from my composite server control,
first I'm creating a javascript function in order to format the numeric columns and I'm including that function on my control.
Also If you can give me some advice about code below I'll apreciate
Complete
thanks for your help.
here is the code from my composite server control,
first I'm creating a javascript function in order to format the numeric columns and I'm including that function on my control.
string resourceName = "formatNumber.js";
this.ScriptManager.RegisterClientScriptInclude(typeof(SuperGrid), resourceName);
Next on the prerender method I'm checking the format columns(date or float)and according to that. I'm showing on the grid the numeric columns based to server culture. for example: a numeric colum renderers to 0,000.00 or 0 000.00 and I'm avoiding to renderer each column on the grid using <Renderer Fn="......." />
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
string resourceName = "formatNumber.js";
this.ScriptManager.RegisterClientScriptInclude(typeof(SuperGrid), resourceName);
if (this.DateFormat != null)
{
CheckDateFields(this.DateFormat);
}
if (this.NumberFormat!=null)
{
CheckNumberFields(NumberFormat);
}
the CheckNumberFields method allow us to render each numeric column (type float) according to server culture
private void CheckNumberFields(string numberFormat)
{
Store store = ControlUtils.FindControl(this, this.StoreID) as Store;
RecordField field = new RecordField();
Column column = new Column();
for (int i = 0; i < store.Reader.Count; i++)
{
for (int j = 0; j < store.Reader[i].Fields.Count; j++)
{
field = store.Reader[i].Fields[j];
if (field.Type == RecordFieldType.Float)
{
for (int k = 0; k < this.ColumnModel.Columns.Count; k++)
{
column = this.ColumnModel.Columns[k] as Column;
if (field.Name == column.DataIndex)
{
column.Renderer.Fn = string.Format("Ext.util.Format.numberRenderer(\'{0}\')", this.NumberFormat);
}
}
}
}
}
}
I would like to know if I can include the store object inside that server control, and How can I expose the DataBind method, DataSource proporties, recordfields, etc...Also If you can give me some advice about code below I'll apreciate
Complete
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Coolite.Ext.Web;
using System.Threading;
using Coolite.Utilities;
namespace Controls
{
public class SuperGrid : GridPanel
{
PagingToolbar pg = new PagingToolbar();
Store store = new Store();
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
string resourceName = "formatNumber.js";
this.ScriptManager.RegisterClientScriptInclude(typeof(SuperGrid), resourceName);
if (this.DateFormat != null)
{
CheckDateFields(this.DateFormat);
}
if (this.NumberFormat!=null)
{
CheckNumberFields(NumberFormat);
}
}
private string NumberFormatMethod(string culture)
{
string lang = culture;
StringBuilder sb = new StringBuilder();
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture(lang);
string SymbolMoney = Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencySymbol;
string GroupSeparator = Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyGroupSeparator;
string DecimalSeparator = Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalSeparator;
object formatMoney = "0" + GroupSeparator + "000" + DecimalSeparator + "00";
return (string)formatMoney;
}
private void CheckNumberFields(string numberFormat)
{
Store store = ControlUtils.FindControl(this, this.StoreID) as Store;
RecordField field = new RecordField();
Column column = new Column();
for (int i = 0; i < store.Reader.Count; i++)
{
for (int j = 0; j < store.Reader[i].Fields.Count; j++)
{
field = store.Reader[i].Fields[j];
if (field.Type == RecordFieldType.Float)
{
for (int k = 0; k < this.ColumnModel.Columns.Count; k++)
{
column = this.ColumnModel.Columns[k] as Column;
if (field.Name == column.DataIndex)
{
column.Renderer.Fn = string.Format("Ext.util.Format.numberRenderer(\'{0}\')", this.NumberFormat);
}
}
}
}
}
}
private void CheckDateColumns(string dateFormat)
{
for (int i = 0; i < this.ColumnModel.Columns.Count; i++)
{
CommandColumn cmdCol = this.ColumnModel.Columns[i] as CommandColumn;
Column column = this.ColumnModel.Columns[i] as Column;
if (column.DataIndex == "Date")
{
column.Renderer.Fn = string.Format("Ext.util.Format.dateRenderer(\'{0}\')", getCLFormat(this.DateFormat));
}
}
}
private void CheckDateFields(string dateFormat)
{
Store store = ControlUtils.FindControl(this, this.StoreID) as Store;
RecordField field = new RecordField();
Column column = new Column();
for (int i = 0; i < store.Reader.Count; i++)
{
for (int j = 0; j < store.Reader[i].Fields.Count; j++)
{
field = store.Reader[i].Fields[j];
if (field.Type==RecordFieldType.Date)
{
for (int k = 0; k < this.ColumnModel.Columns.Count; k++)
{
column = this.ColumnModel.Columns[k] as Column;
if (field.Name==column.DataIndex)
{
column.Renderer.Fn = string.Format("Ext.util.Format.dateRenderer(\'{0}\')", getCLFormat(this.DateFormat));
}
}
}
}
}
}
/// <summary>
/// Date format string. The format must be valid according to Date.parseDate.
/// </summary>
[Description("Date format string. The format must be valid according to Date.parseDate.")]
public string DateFormat
{
get
{
object obj = this.ViewState["DateFormat"];
return (obj == null) ? System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern.ToString() : (string)obj;
}
set
{
this.ViewState["DateFormat"] = value;
}
}
private string getCLFormat(string netformat)
{
// Formats supported by .Net traslate to coolite formats.
switch (netformat)
{
case "yyyy-MM-dd":
return "Y-m-d";
case "yyyy-dd-MM":
return "Y-d-m";
case "MM-dd-yyyy":
return "m-d-Y";
case "dd-MM-yyyy":
return "d-m-Y";
case "yyyy/MM/dd":
return "Y/m/d";
case "yyyy/dd/MM":
return "Y/d/m";
case "MM/dd/yyyy":
return "m/d/Y";
case "dd/MM/yyyy":
return "d/m/Y";
default:
throw new Exception("Format not supported");
}
}
#region Grid Properties
[Category("Config Options")]
[DefaultValue(20)]
[Description("The number of records to display per page (defaults to 20).")]
[NotifyParentProperty(true)]
public int PageSize
{
get
{
EnsureChildControls();
return pg.PageSize;
}
set
{
EnsureChildControls();
pg.PageSize = value;
}
}
[Category("Config Options")]
[DefaultValue(false)]
[Description("True to display the displayMsg (defaults to false).")]
[NotifyParentProperty(true)]
public virtual bool DisplayInfo
{
get
{
EnsureChildControls();
return pg.DisplayInfo;
}
set
{
EnsureChildControls();
pg.DisplayInfo = value;
}
}
[Category("Config Options")]
[DefaultValue("Displaying {0} - {1} of {2}")]
[NotifyParentProperty(true)]
[Localizable(true)]
[Description("The paging status message to display (defaults to 'Displaying {0} - {1} of {2}'). Note that this string is formatted using the braced numbers 0-2 as tokens that are replaced by the values for start, end and total respectively. These tokens should be preserved when overriding this string if showing those values is desired.")]
public virtual string DisplayMsg
{
get
{
EnsureChildControls();
return pg.DisplayMsg;
}
set
{
EnsureChildControls();
pg.DisplayMsg = value;
}
}
[Category("Config Options")]
[DefaultValue("No data to display")]
[NotifyParentProperty(true)]
[Localizable(true)]
[Description("The message to display when no records are found (defaults to 'No data to display').")]
public string EmptyMsg
{
get
{
EnsureChildControls();
return pg.EmptyMsg;
}
set
{
EnsureChildControls();
pg.EmptyMsg = value;
}
}
[Bindable(false),
Category("Appearance"),
DefaultValue(false),
Description("True to display Paging Toolbar.")]
public bool PagingBar
{
get
{
object obj = this.ViewState["PagingBar"];
return (obj == null) ? false : (bool)obj;
}
set
{
this.ViewState["PagingBar"] = value;
}
}
[Category("Config Options")]
[Description("The number of records to display per page (defaults to 20).")]
[NotifyParentProperty(true)]
public string NumberFormat
{
get
{
object obj = this.ViewState["NumberFormat"];
string lang = System.Globalization.CultureInfo.CurrentCulture.ToString();
return (obj == null) ? NumberFormatMethod(lang) : (string)obj;
}
set
{
this.ViewState["NumberFormat"] = value;
}
}
#endregion
protected override void CreateChildControls()
{
base.CreateChildControls();
if (this.PagingBar)
{
this.BottomBar.Add(pg);
pg.Listeners.Render.Handler = "el.loading.hide();";
}
}
}
}
thanks for your help.