Welcome Guest Search | Active Topics | Sign In | Register

Populating of Editor template from c# code Options
Michael
Posted: Tuesday, December 8, 2009 10:34:17 AM
Rank: Member
Groups: Member

Joined: 10/28/2009
Posts: 15
Hi,

In a markup for Custom column I have

<eo:CustomColumn>
<EditorTemplate>
<select id="ApplySort_ddl">
<asp:Repeater runat="server" ID="ApplySort">
<ItemTemplate>
<option value="<%#DataBinder.Eval(Container.DataItem, "Code")%>">
<%#DataBinder.Eval(Container.DataItem, "Description")%>
</option>
</ItemTemplate>
</asp:Repeater>
</select>
</EditorTemplate>
</eo:CustomColumn>

How to populate EditorTemplate from c# code?

EO.Web.CustomColumn cc = new EO.Web.CustomColumn();
cc.Width = 160;
cc.DataField = "ApplySort";
cc.HeaderText = "Sort by";
cc.Name = "ApplySort";
cc.DataType = EO.Web.GridColumnDataType.Integer;
cc.ClientSideBeginEdit = "on_begin_edit_ApplySort";
cc.ClientSideEndEdit = "on_end_edit_ApplySort";
cc.ClientSideGetText = "on_column_gettext_ApplySort";
cc.EditorTemplate = ????
Grid1.Columns.Add(cc);

Thanks in advance
Michael
eo_support
Posted: Tuesday, December 8, 2009 12:01:07 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,194
Hi,

You will need to create an object that implements ITemplate. This is what ASP.NET compiler does when you have a static template in your .aspx.

Thanks
Michael
Posted: Wednesday, December 9, 2009 5:26:29 PM
Rank: Member
Groups: Member

Joined: 10/28/2009
Posts: 15
Hi,

Thanks, I implemented ITemplate interface like

public class DropDownListTemplate : ITemplate
{
private string aName = "";
DropDownList ddl = null;

public DropDownListTemplate(string name)
{ aName = name; }

public void InstantiateIn(System.Web.UI.Control container)
{
ddl = new DropDownList();
ddl.ID = aName;
ddl.Items.Add(“Test 1”);
ddl.Items.Add(“Test 2”);
container.Controls.Add(ddl);
}
}

On Page_init dynamically created columns:

protected void Page_Init(object sender, EventArgs e)
{
if (!IsPostBack)
{
Grid1.Columns.Clear();

EO.Web.RowNumberColumn rnc = new EO.Web.RowNumberColumn();
Grid1.Columns.Add(rnc);

EO.Web.CustomColumn cc = new EO.Web.CustomColumn();
cc.DataField = "ApplySort";
cc.HeaderText = "Sort by";
cc.Name = "ApplySort";
cc.DataType = EO.Web.GridColumnDataType.Integer;
cc.ClientSideBeginEdit = "on_begin_edit_ApplySort";
cc.ClientSideEndEdit = "on_end_edit_ApplySort";
cc.ClientSideGetText = "on_column_gettext_ApplySort";
cc.EditorTemplate = new DropDownListTemplate(“ApplySort_ddl”);
Grid1.Columns.Add(cc);
}
}

So DDL is dynamically created on Page_preload:

protected void Page_PreRender(object sender, EventArgs e)
{
if (!IsPostBack) {
Control cc = ((EO.Web.CustomColumn)Grid1.FindColumn("ApplySort")).EditorInstance;
String sScript = String.Concat("ddl=document.getElementById('", cc.Controls[0].ClientID, "');")
ScriptManager.RegisterStartupScript(… sScript …);
}
}
Everything works fine when page displays first time. After partial postback DDL disappears. What’s wrong in my action? Why EditorInstance returns null during PostBack?

Some code from markup:

<script>

var ddl = null;

function on_column_gettext_ApplySort(column, item, cellValue)
{
if (ddl == null) return cellValue;
else
{ … }
}
function on_begin_edit_ApplySort(cell)
{ … }

function on_end_edit_ApplySort(cell)
{ … }
</script>


Tnanks in advance
Michael
eo_support
Posted: Wednesday, December 9, 2009 7:35:46 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,194
Hi,

You need to initialize the columns' EditorTemplate regardless whether IsPostBack is true or false (you only need to create the column when IsPostBack is false). EditorTemplate is not saved in view state so you must always create it.

Thanks!
Michael
Posted: Monday, December 14, 2009 11:29:46 AM
Rank: Member
Groups: Member

Joined: 10/28/2009
Posts: 15
Hi,

Thanks. It works. I have two more questions:

1. I have Custom column with a Text Box and a Regular Expression Validator. How to prevent moving from this column after editing if Validator.isValid is false?

2. I need to return default value from on_begin_edit function for custom column in a case if I edit new row. Is there a way to know that editable cell/row is new one?

Thanks in advance
Michael
eo_support
Posted: Monday, December 14, 2009 3:05:33 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,194
Hi,

For the first one you will need to let cell leave edit mode and then call Grid.editCell inside setTimeout to bring the cell back to edit mode. See here for sample code:

http://www.essentialobjects.com/forum/postst2712_Datetime-validator.aspx

For the second one you need to check the cell argument passed to your begin_edit handler. You can get row/column number from that object. You will then use some global variable to keep information for the previous editing cell and then compare the new one with the information you kept to determine whether you got a new cell.

Thanks!


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.