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

Dynamic Template column for Command not firing event

8 Answers 245 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Akhil Raj
Top achievements
Rank 1
Akhil Raj asked on 18 Feb 2010, 09:30 AM
hi all,
  I need to bind xml data t rad grid view. Accoridng to the xml i want to set column dynamically So i need to create gridview column dynamically when click button for load xml. All columns working except command columns. For command column also i created template column with link buttons. But when i click the button command not initiating..

My staic declaration of RadGrid at initial:

<telerik:RadGrid ID="rad_grd_XmlGrid" runat="server"   
AutoGenerateColumns="false" oncancelcommand="rad_grd_XmlGrid_CancelCommand"   
oneditcommand="rad_grd_XmlGrid_EditCommand"   
 onitemcommand="rad_grd_XmlGrid_ItemCommand">   
 </telerik:RadGrid>  
 
 


My Code for Grid Generation

 

 

GridTemplateColumn tcol1 = new GridTemplateColumn();   
tcol1.HeaderText = "Name";   
tcol1.ItemTemplate = new DynamicTemplate(GridItemType.Item, "name", tcol1.DataType.ToString());   
tcol1.EditItemTemplate = new DynamicTemplate(GridItemType.EditItem, "name", tcol1.DataType.ToString());   
GridTemplateColumn tcol2 = new GridTemplateColumn();   
tcol2.HeaderText = "Modify";   
tcol2.ItemTemplate = new DynamicTemplate(GridItemType.Item, "..""Command");   
tcol2.EditItemTemplate = new DynamicTemplate(GridItemType.EditItem, "..""Command");   
grid.MasterTableView.Columns.Add(tcol1);  
grid.MasterTableView.Columns.Add(tcol2);  
DataSet ds = new DataSet();   
ds.ReadXml("c:\\Services.xml");   
grid.DataSource = ds;  
grid.DataBind();  
 
 
 


My Code for TemplateGeneration

 

public class DynamicTemplate : ITemplate   
{  
GridItemType iType;   
String fName;   
String sinfo;   
   
public DynamicTemplate(GridItemType itemType, String fieldName, String info)   
{  
iType = itemType;  
fName = fieldName;  
sinfo = info;  
}  
 
public void InstantiateIn(System.Web.UI.Control Container)   
{  
switch (iType)   
{  
case GridItemType.Header:   
   Literal header_ltrl = new Literal();   
   header_ltrl.Text = "<b>" + fName + "</b>";   
   Container.Controls.Add(header_ltrl);  
   break;   
case GridItemType.Item:   
switch (sinfo)   
{  
case "Command":   
   LinkButton edit_button = new LinkButton();   
   edit_button.ID = "editButton";   
   edit_button.CommandName = "Edit";   
   //edit_button.Click +=new EventHandler(edit_button_Click);   
   edit_button.Text = "Edit";   
   edit_button.ToolTip = "Edit";   
   Container.Controls.Add(edit_button);  
   break;   
default:   
   Label field_lbl = new Label();   
   field_lbl.ID = fName;  
   field_lbl.DataBinding += new EventHandler(OnDataBinding);   
   Container.Controls.Add(field_lbl);  
   break;   
}  
break;   
case GridItemType.EditItem:   
    if (sinfo == "Command")   
    {  
      LinkButton update_button = new LinkButton();   
      update_button.ID = "updateButton";   
      update_button.CommandName = "Update";   
      update_button.Text = "Update";   
      Container.Controls.Add(update_button);  
      LinkButton cancel_button = new LinkButton();   
      cancel_button.ID = "cancelButton";   
      cancel_button.CommandName = "Cancel";   
      cancel_button.Text = "Cancel";   
      cancel_button.ToolTip = "Cancel";   
      Container.Controls.Add(cancel_button);  
     }  
     else   
     {  
       TextBox field_txtbox = new TextBox();   
       field_txtbox.ID = fName;  
       field_txtbox.DataBinding += new EventHandler(OnDataBinding);   
       Container.Controls.Add(field_txtbox);  
     }  
     break;   
}  
}  
//protected void edit_button_Click(Object sender, EventArgs e)   
//{   
// new Page().Session["InsertFlag"] = 0;   
}   
   
private void OnDataBinding(object sender, EventArgs e)   
{  
 
switch (iType)   
{  
case GridItemType.Item:   
     Label field_ltrl = (Label)sender;   
     GridDataItem container = (GridDataItem)field_ltrl.NamingContainer;   
     field_ltrl.Text = DataBinder.Eval(container.DataItem, fName).ToString();   
     break;   
case GridItemType.EditItem:   
     TextBox field_txtbox = (TextBox)sender;   
     GridEditFormItem container1 = (GridEditFormItem)field_txtbox.NamingContainer;   
     field_txtbox.Text = DataBinder.Eval(container1.DataItem, fName).ToString();   
     break;   
}  
}  
}  
 

 


How can i trigger edit command?


 

 

8 Answers, 1 is accepted

Sort by
0
Yavor
Telerik team
answered on 23 Feb 2010, 09:27 AM
Hi Akhil,

Can you please make sure that the template column is created in the PageInit event handler, which is a requirement when adding template columns to the grid control. More details on this are available in the following example:

http://demos.telerik.com/aspnet-ajax/grid/examples/hierarchy/definingstructure/defaultcs.aspx

Additionally, I would suggest that you use NeedDataSource event handler, or a datasource control, to populate the grid with data, as opposed to using simple databinding.
I hope this gets you started properly.

Best wishes,
Yavor
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Akhil Raj
Top achievements
Rank 1
answered on 24 Feb 2010, 04:11 AM
thanks admin... yes i got your point and my problem is i create columns in button event. Now i got that template column must be created in page_InIt.. Am i correct?

Accoridng to my scenario i want to create when the user press one button. So one xml is filled in datatable and create columns accoriding to that table column count. Anyway now i acheived this using bound column. Bound column is created in page load event and it is working corerctly. For button press i created one hidden filed and bind one value and submit the form from client side function when that button is pressed. I think this is ok accoridng to this scenario. But all my gridbind is happening from page load only.

Thanks for your help
0
Yavor
Telerik team
answered on 01 Mar 2010, 06:37 AM
Hi Akhil,

Indeed, the control must be created in the PageInit event handler, to ensure that the template columns are functioning properly.
You can also use the PageLoad, if no template columns are present. I hope the setup is functioning as per your requirements now.

Sincerely yours,
Yavor
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Akhil Raj
Top achievements
Rank 1
answered on 02 Mar 2010, 04:08 AM
yes admin i created bound clolumns in page load event and working as hierarchial model. But my new issue is expandcol,lapse column not working. I already posted about this in another thread and got one solution as create custom expand collapse image button in template column... again i came the same situation. If i create custom expandcollapse in button in template column again i need to work in page-init correct? also grid is created dynamically so my doubt is expand collpase button work or not? because viewstate not aviable here know. anyway working as a good challenge in this. please give any ideas ir i am in wrong thinking or direction
0
Yavor
Telerik team
answered on 04 Mar 2010, 12:03 PM
Hello Akhil,

The hierarchy can be created in pageInit as well, and the columns will work as expected. This is demonstrated in the following example:

http://demos.telerik.com/aspnet-ajax/grid/examples/programming/hierarchy/defaultcs.aspx

Let me know if this is the case at your end.

Best wishes,
Yavor
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Akhil Raj
Top achievements
Rank 1
answered on 05 Mar 2010, 04:15 AM
ok admin i will let u know when i finish  will try this method
0
varsha Motwani
Top achievements
Rank 1
answered on 29 Apr 2010, 01:48 PM
 

I am also facing the similar problem even after creating template columns inside
pageinit event.It is giving following error in javascript:
Sys.WebForms.PageRequestManagerServerErrorException: Multiple controls with the same ID 'FilterTextBox_TemplateColumn' were found. FindControl requires that controls have unique IDs.

aspx code:

 

 

 

  <telerik:RadGrid ID="tEng" runat="server" AllowPaging="true" PageSize="10" ClientSettings-Selecting-AllowRowSelect="true" 
                                                    AllowSorting="true" AllowMultiRowSelection="true" AllowMultiRowEdit="true" AutoGenerateColumns="false" 
                                                    GridLines="None" BorderColor="#247424" BorderWidth="3px" BorderStyle="Solid" 
                                                    AllowAutomaticUpdates="false" AllowFilteringByColumn="true" Skin="Telerik" EnableEmbeddedSkins="true" 
                                                    AllowAutomaticInserts="false" AllowAutomaticDeletes="true" 
                                                       
                                                    > 
                                                                                                        <PagerStyle Mode="NumericPages" /> 
                                                    <MasterTableView EditMode="InPlace">  
                                                         
                                                       
                                                    </MasterTableView> 
                                                    <HeaderStyle /> 
                                                </telerik:RadGrid> 

aspx.cs  code
protected void Page_Init(object sender, System.EventArgs e)  
        {  
              DataSet ds = new DataSet();  
                ds.ReadXml(Server.MapPath("..//App_Data//GridTemplate.xml"));  
                CreateGrid(ds);  
                DataSet objDS = new DataSet();  
                objDS.ReadXml(Server.MapPath("..//App_Data//EngagementData.xml"));  
                tEng.DataSource = objDS;  
                tEng.DataBind();  
              
        }  
 
 
        public void CreateGrid(DataSet ds)  
        {  
            //GridClientSelectColumn grdSelcol = new GridClientSelectColumn();  
            //grdSelcol.HeaderStyle.Width = Unit.Pixel(5);  
            //tEng.MasterTableView.Columns.Add(grdSelcol);  
 
            GridEditCommandColumn grdEdCol = new GridEditCommandColumn();  
            grdEdCol.ButtonType = GridButtonColumnType.ImageButton;  
            grdEdCol.EditImageUrl = "../CSS/img/Edit.jpg";  
            grdEdCol.UniqueName = "EditCmdCol";  
            tEng.MasterTableView.Columns.Add(grdEdCol);  
 
            GridButtonColumn grdDelCol = new GridButtonColumn();  
            grdDelCol.ButtonType = GridButtonColumnType.ImageButton;  
            grdDelCol.ImageUrl = "../CSS/img/del.jpg";  
            grdDelCol.UniqueName = "DelCmdCol";  
            grdDelCol.HeaderStyle.Width = Unit.Pixel(5);  
            tEng.MasterTableView.Columns.Add(grdDelCol);  
 
            GridButtonColumn grdViewCol = new GridButtonColumn();  
            grdViewCol.ButtonType = GridButtonColumnType.ImageButton;  
            grdViewCol.ImageUrl = "../CSS/img/view.jpg";  
            grdViewCol.UniqueName = "ViewCmdCol";  
            grdViewCol.HeaderStyle.Width = Unit.Pixel(5);  
            tEng.MasterTableView.Columns.Add(grdViewCol);  
 
              
 
 
            for (int count = 0;count< ds.Tables[0].Rows.Count; count++)  
            {  
                GridTemplateColumn tempcol = new GridTemplateColumn();  
                if (ds.Tables[0].Rows[count][0].ToString() == "QueueID")  
                {  
                    tempcol.ItemTemplate = new DynamicTemplate(GridItemType.Item, ds.Tables[0].Rows[count][0].ToString(), "lbl");  
                    tempcol.ItemTemplate = new DynamicTemplate(GridItemType.EditItem, ds.Tables[0].Rows[count][0].ToString(), "lbl");  
                }  
                else  
                {  
                    tempcol.ItemTemplate = new DynamicTemplate(GridItemType.Item, ds.Tables[0].Rows[count][0].ToString(), "cmb");  
                    tempcol.ItemTemplate = new DynamicTemplate(GridItemType.EditItem, ds.Tables[0].Rows[count][0].ToString(), "cmb");  
                }  
 
                tEng.MasterTableView.Columns.Add(tempcol);  
                 
 
            }  
              
                 
        }  
  public class DynamicTemplate : ITemplate  
    {  
        GridItemType iType;  
        String fName;  
        String sinfo;  
 
        public DynamicTemplate(GridItemType itemType, String fieldName, String info)  
        {  
            iType = itemType;  
            fName = fieldName;  
            sinfo = info;  
        }  
        public void InstantiateIn(System.Web.UI.Control Container)  
        {  
            switch (iType)  
            {  
                case GridItemType.Header:  
                    Literal header_ltrl = new Literal();  
                    header_ltrl.Text = "<b>" + fName + "</b>";  
                    Container.Controls.Add(header_ltrl);  
                    break;  
                case GridItemType.Item:  
                    switch (sinfo)  
                    {  
                        case "Command":  
                            //LinkButton edit_button = new LinkButton();  
                            //edit_button.ID = "editButton";  
                            //edit_button.CommandName = "Edit";  
                            ////edit_button.Click +=new EventHandler(edit_button_Click);      
                            //edit_button.Text = "Edit";  
                            //edit_button.ToolTip = "Edit";  
                            //Container.Controls.Add(edit_button);  
                            break;  
                        default:  
                            Label field_lbl = new Label();  
                            field_lbl.ID = fName;  
                            field_lbl.DataBinding += new EventHandler(OnDataBinding);  
                            Container.Controls.Add(field_lbl);  
                            break;  
                    }  
                    break;  
                case GridItemType.EditItem:  
                    if (sinfo == "Command")  
                    {  
                        //LinkButton update_button = new LinkButton();  
                        //update_button.ID = "updateButton";  
                        //update_button.CommandName = "Update";  
                        //update_button.Text = "Update";  
                        //Container.Controls.Add(update_button);  
                        //LinkButton cancel_button = new LinkButton();  
                        //cancel_button.ID = "cancelButton";  
                        //cancel_button.CommandName = "Cancel";  
                        //cancel_button.Text = "Cancel";  
                        //cancel_button.ToolTip = "Cancel";  
                        //Container.Controls.Add(cancel_button);  
                    }  
                    else if (sinfo == "cmb")  
                    {  
                        RadComboBox field_cmb = new RadComboBox();  
                        field_cmb.ID = fName;  
                        field_cmb.DataSource = GetDatasource(fName);  
                        field_cmb.DataTextField = "Value";  
                        field_cmb.DataValueField = "ID";  
                        field_cmb.DataBinding += new EventHandler(OnDataBinding);  
                        Container.Controls.Add(field_cmb);  
                    }  
                    else if (sinfo == "lbl")  
                    {  
                        Label field_lbl = new Label();  
                        field_lbl.ID = fName;     
 
                        field_lbl.DataBinding += new EventHandler(OnDataBinding);  
                        Container.Controls.Add(field_lbl);  
                    }  
                    break;  
            }  
        }  
        private DataTable GetDatasource(string fname)  
        {  
              
            DataSet ds=new DataSet();  
            if(fname=="InvoiceOwner")  
                ds.ReadXml(HttpContext.Current.Server.MapPath("..//App_Data//InvoiceOwner.xml"));  
            if(fname=="Client")  
                ds.ReadXml(HttpContext.Current.Server.MapPath("..//App_Data//Client.xml"));  
            if(fname=="ProductGroup")  
                ds.ReadXml(HttpContext.Current.Server.MapPath("..//App_Data//ProductGroup.xml"));  
            return ds.Tables[0];  
        }  
        private void OnDataBinding(object sender, EventArgs e)  
        {  
 
            switch (iType)  
            {  
                case GridItemType.Item:  
                    Label field_ltrl = (Label)sender;  
                    GridDataItem container = (GridDataItem)field_ltrl.NamingContainer;  
                    field_ltrl.Text = DataBinder.Eval(container.DataItem, fName).ToString();  
                    break;  
                case GridItemType.EditItem:  
                    if (sinfo == "lbl")  
                    {  
                        Label field_lbl = (Label)sender;  
                        GridDataItem container1 = (GridDataItem)field_lbl.NamingContainer;  
                        field_lbl.Text = DataBinder.Eval(container1.DataItem, fName).ToString();  
                    }  
                    else if(sinfo == "cmb")  
                    {  
                        RadComboBox field_cmb = (RadComboBox)sender;  
                        GridDataItem container1 = (GridDataItem)field_cmb.NamingContainer;  
                        field_cmb.SelectedValue = DataBinder.Eval(container1.DataItem, fName).ToString();  
                    }  
                    break;  
            }  
        }    
 
 
 
    } 


0
Yavor
Telerik team
answered on 04 May 2010, 10:16 AM
Hello varsha,

When creating the control's structure in the PageInit event handler, the whole structure needs to be created in this event, and no declaration should be included in the aspx page.
Additionally, you should not set the datasource directly and call databind - you can utilize the NeedDataSource event handler, and provide data for the grid there.
I hope this information helps.

All the best,
Yavor
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
Grid
Asked by
Akhil Raj
Top achievements
Rank 1
Answers by
Yavor
Telerik team
Akhil Raj
Top achievements
Rank 1
varsha Motwani
Top achievements
Rank 1
Share this question
or