This is a migrated thread and some comments may be shown as answers.

[Solved] JScript Error Binding Template

1 Answer 116 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Michelle Edwards
Top achievements
Rank 1
Michelle Edwards asked on 19 Feb 2010, 11:42 PM
Hi,

I am getting the following error when I try to edit a Grid Row. One of the columns in the grid is a templated column (created programatically):

Microsoft Jscript runtime error:
Sys.WebForms.PageRequestManagerServerErrorException: Unable to cast object of Type RadLookupTextBox to type System.Web.UI.IBindableTemplate.

Below is the source code of the function that creates the Templated Column as well as the Templated Class.
Please let me know where the problem is.

private void CreateTemplatedColumn(string caption, string datafield, string hyperlinkfield, int width)  
    {  
        RadLookupTextBox txt = new RadLookupTextBox();  
        txt.BindingName = datafield;  
        txt.ControlName = "gridtextboxcoleditor_" + datafield;  
        txt.FieldName = datafield;  
        txt.HyperlinkFieldName = hyperlinkfield;  
        txt.Skin = "Office2007";  
        txt.Width = width;  
        txt.ButtonCssClass = "RadInputButton";  
        txt.ButtonClickFunctionName = "RadTextBoxButtonClick";  
        txt.EditMode = true;  
 
        RadLookupTextBox link = new RadLookupTextBox();  
        link.BindingName = datafield;  
        link.ControlName = "gridhyperlinkcoleditor_" + datafield;  
        link.FieldName = datafield;  
        link.HyperlinkFieldName = hyperlinkfield;  
        link.Width = width;  
        link.EditMode = false;  
          
        GridTemplateColumn col = new GridTemplateColumn();  
        col.EditItemTemplate = txt;  
        col.ItemTemplate = link;  
        col.UniqueName = datafield;  
        col.DataField = datafield;  
        col.HeaderText = caption;  
        col.ItemStyle.Width = Unit.Pixel(width);  
          
        rgEntity.MasterTableView.Columns.Add(col);  
                  
    }  
 

public class RadLookupTextBox : ITemplate   
{  
    // Make Properties  
    private string m_controlName = "", m_bindingName = "";  
    private string m_fieldName = "", m_hyperlinkFieldName;  
    private string m_skin = "", m_buttonCssClass = "";  
    private string m_functionButtonClick;  
    private int m_width = 0;  
    private bool m_editMode = false;  
 
    public string ControlName  
    {  
        set 
        {  
            m_controlName = value;  
        }  
    }  
    public string BindingName  
    {  
        set 
        {  
            m_bindingName = value;  
        }  
    }  
    public string FieldName  
    {  
        set 
        {  
            m_fieldName = value;  
        }  
    }  
    public string HyperlinkFieldName  
    {  
        set 
        {  
            m_hyperlinkFieldName = value;  
        }  
    }  
    public string Skin  
    {  
        set 
        {  
            m_skin = value;  
        }  
    }  
    public string ButtonCssClass  
    {  
        set 
        {  
            m_buttonCssClass = value;  
        }  
    }  
    public string ButtonClickFunctionName  
    {  
        set 
        {  
            m_functionButtonClick = value;  
        }  
    }  
    public int Width  
    {  
        set 
        {  
            m_width = value;  
        }  
    }  
    public bool EditMode  
    {  
        set 
        {  
            m_editMode = value;  
        }  
    }  
 
 
    public RadLookupTextBox()  
    {  
        //  
        // TODO: Add constructor logic here  
        //  
    }  
 
    public RadLookupTextBox(string controlName, string bindingName)  
    {  
        this.m_controlName = controlName;  
        if (bindingName != string.Empty)  
            this.m_bindingName = bindingName;  
    }  
 
    public void InstantiateIn(System.Web.UI.Control container)  
    {  
        if (m_editMode)  
        {  
            RadTextBox textbox = new RadTextBox();  
            textbox.DataBinding += new EventHandler(textbox_DataBinding);  
            textbox.ID = this.m_controlName;  
            textbox.Skin = m_skin;  
            textbox.Width = Unit.Pixel(m_width);  
            textbox.ShowButton = true;  
            textbox.ButtonCssClass = m_buttonCssClass;  
            textbox.ClientEvents.OnButtonClick = m_functionButtonClick;  
            container.Controls.Add(textbox);  
        }  
        else 
        {  
            HyperLink hl = new HyperLink();  
            hl.DataBinding += new EventHandler(hyperlink_DataBinding);  
            hl.ID = this.m_controlName;  
            hl.Width = Unit.Pixel(m_width);  
            container.Controls.Add(hl);  
        }  
    }  
 
    private void textbox_DataBinding(object sender, EventArgs e)  
    {  
        RadTextBox textbox = (RadTextBox)sender;  
        GridEditableItem container = (GridEditableItem)textbox.NamingContainer;  
        textbox.Text = ((DataRowView)container.DataItem)[m_fieldName].ToString();  
    }  
 
    private void hyperlink_DataBinding(object sender, EventArgs e)  
    {  
        HyperLink hl = (HyperLink)sender;  
        GridItem container = (GridItem)hl.NamingContainer;  
        hl.Text = ((DataRowView)container.DataItem)[m_fieldName].ToString();  
        hl.NavigateUrl = ((DataRowView)container.DataItem)[m_hyperlinkFieldName].ToString();  
    }  
 
 
}  

Thanks.

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 20 Feb 2010, 05:44 AM
Hi,

"Creating columns in the Page_Load event handler does not work for template columns. For the controls inside a template to persist theirViewState, the grid must be generated completely in the code-behind using the Page_Init event (see below). That way, template controls are instantiated before the LoadViewState event of the page."

How are you creating your grid?. For dynamic template columns ,you will need to generate the grid entirely in the Page_Init event.

Thanks,
Princy
Tags
Grid
Asked by
Michelle Edwards
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or