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

LinkButton in ITemplate not triggering the ItemCommand

1 Answer 119 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Todd A
Top achievements
Rank 1
Todd A asked on 18 Dec 2010, 02:10 AM
I have used the ITemplate structure to dynamically create columns for RadGrid's -- and it works great for me for display purposes.

I am now trying to add a LinkButton and to process the event when it's clicked through the Grid's onItemCommand property.  Although, when the linkbutton is clicked, the page postback happends, but the ItemCommand method is not triggered.

What's the missing link?

Thanks,
Todd.

grid html

<telerik:RadGrid ID="TimeSlotScheduleGrid" runat="server" EnableViewState="True"
      AutoGenerateColumns="False" Width="584" ShowFooter="false"
      onItemDataBound="TimeSlotScheduleGrid_RowDataBound" onItemCommand="TimeSlotScheduleGrid_RowCommand"
      OnColumnCreated="TimeSlotScheduleGrid_ColumnCreated">                     
      <MasterTableView DataKeyNames="Id"  GroupLoadMode="Client" Width="99%">
          <GroupByExpressions>
              <telerik:GridGroupByExpression>
                  <SelectFields>
                      <telerik:GridGroupByField FieldName="GameNo" HeaderText="Game Number" HeaderValueSeparator=": " />
                  </SelectFields>
                  <GroupByFields>                                    
                      <telerik:GridGroupByField FieldName="GameNo" SortOrder="Ascending" />
                  </GroupByFields>
              </telerik:GridGroupByExpression>
          </GroupByExpressions>                      
      <Columns>                                
          <telerik:GridTemplateColumn HeaderText='Time Slot'>
              <HeaderStyle Width="130" />
              <ItemStyle  HorizontalAlign="Left" VerticalAlign="Middle" />                    
              <ItemTemplate>                                
                      <asp:Label ID="timeSlotLabel" runat="server" />                                
              </ItemTemplate>
          </telerik:GridTemplateColumn>                                                            
      </Columns>
      </MasterTableView>
      <ClientSettings EnableRowHoverStyle="false">
          <Selecting AllowRowSelect="false" />
          <Scrolling AllowScroll="True" UseStaticHeaders="True" SaveScrollPosition="True" FrozenColumnsCount="1"/>                        
      </ClientSettings>                     
  </telerik:RadGrid>

The columns are created during the first Page_Load

for (int wk = 1; wk < (maxWeeks + 1); wk++)
{
    GridTemplateColumn templateColumn = new GridTemplateColumn();
    string columnName = "W" + wk.ToString();
    templateColumn.HeaderText = columnName;
    templateColumn.HeaderStyle.Width = new Unit(40);
    templateColumn.UniqueName = columnName;
    templateColumn.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
    templateColumn.ItemStyle.VerticalAlign = VerticalAlign.Top;
      
    if(divisionObj.IsManualTimeSlotEntry)
        templateColumn.ItemTemplate = new TimeSlotCheckBoxTemplate(columnName);
    else
        templateColumn.ItemTemplate = new TimeSlotTemplate(columnName);
    TimeSlotScheduleGrid.MasterTableView.Columns.Add(templateColumn);
}

Here is the template class

private class TimeSlotTemplate : ITemplate
{
    private string templateName;
    //controls
    private Label label;
    private LinkButton linkButton;
    private Image image;
      
    public TimeSlotTemplate(string name)
    {
        templateName = name;
    }
    public void InstantiateIn(System.Web.UI.Control container)
    {
        label = new Label();
        label.ID = templateName +  "Label";
        linkButton = new LinkButton();
        linkButton.ID = templateName + "Button";
        image = new Image();
        image.ID = templateName + "Image";
        linkButton.Controls.Add(image);
        container.Controls.Add(label);
        container.Controls.Add(linkButton);
    }
}


1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 20 Dec 2010, 11:06 AM
Hello Todd,

You need to create GridTemplateColumns in PageInit and check if it works. In order to persist their ViewState you need to generate your grid completely in the code-behind (on PageInit). Thus the template controls will be retained as they will be instantiated before LoadViewState event of the page.
For more on this please go through the following documentation.
Programmatic creation

Hope this helps,
Princy.
Tags
Grid
Asked by
Todd A
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or