This is a migrated thread and some comments may be shown as answers.
Button in Grid Group Header fires on second click
5 Answers 65 Views
This is a migrated thread and some comments may be shown as answers.
uwe
Top achievements
Rank 1
uwe asked on 22 Jul 2011, 09:12 AM
Hello,

in my test scenario i insert a radbutton into the group header. This stuff is done in the following methods:

protected void RadGrid1_OnItemDataBound(object sender, GridItemEventArgs e)
       {
          _AppendButtonToHeader(e.Item);           
       }
 
       protected void RadGrid1_OnItemCreated(object sender, GridItemEventArgs e)
       {
          _AppendButtonToHeader(e.Item);
       }
 
       private void _AppendButtonToHeader(object item)
       {
           if (item is GridGroupHeaderItem)
           {
               var gridGroupHeaderItem = item as GridGroupHeaderItem;
               var dataCell = gridGroupHeaderItem.DataCell;
 
               var radButton = new RadButton();
               radButton.ButtonType = RadButtonType.StandardButton;
               radButton.Text = "do";
               radButton.Click += new EventHandler(radButton_Click);
 
               dataCell.Controls.Add(radButton);
               dataCell.Controls.Add(new LiteralControl(dataCell.Text));
           }
       }

When i load the page for the first time the 'click' on the 'do'-button does not fire a event. Neither in 'radButton_Click' nor in RadGrid1_ItemCommand'. After the second 'click' to the 'do'-button the event fires.
This behavior is very strange or i missed a  configuration.

Here ist the markup for the grid:

<telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True"
                                 AutoGenerateColumns="false" CellSpacing="0" GridLines="None"
                                 ShowGroupPanel="True" OnNeedDataSource="RadGrid1_OnNeedDataSource"
                                 OnItemDataBound="RadGrid1_OnItemDataBound"
                                 OnItemCreated="RadGrid1_OnItemCreated"
                                 OnPreRender="RadGrid1_OnPreRender">
                    <ClientSettings AllowDragToGroup="True">
                    </ClientSettings>
                    <MasterTableView AutoGenerateColumns="False" GroupLoadMode="Server" GroupsDefaultExpanded="False">
                        <GroupByExpressions>
                            <telerik:GridGroupByExpression>
                                <SelectFields>
                                        <telerik:GridGroupByField FieldName="gl1" HeaderText="gl1" />
                                        <telerik:GridGroupByField FieldName="gl2" HeaderText="gl2" />
                                        <telerik:GridGroupByField FieldName="gl3" HeaderText="gl3" />
                                        <telerik:GridGroupByField FieldName="gl4" HeaderText="gl4" />
                                    </SelectFields>
                                <GroupByFields>
                                    <telerik:GridGroupByField FieldName="gl1" SortOrder="Ascending" />
                                    <telerik:GridGroupByField FieldName="gl2" SortOrder="Ascending" />
                                    <telerik:GridGroupByField FieldName="gl3" SortOrder="Ascending" />
                                    <telerik:GridGroupByField FieldName="gl4" SortOrder="Ascending" />
                                </GroupByFields>
                            </telerik:GridGroupByExpression>
                            
                        </GroupByExpressions>
 
                        <Columns>
                        <telerik:GridTemplateColumn HeaderStyle-Width="100px">                               
                                <ItemTemplate>
                                    <telerik:RadButton ID="RadButton1" runat="server" OnClick="OnClick" ButtonType="LinkButton" Text="plah" CommandName=""></telerik:RadButton>
                                    <telerik:RadButton ID="RadButton2" runat="server" OnClick="OnClick" ButtonType="LinkButton" Text="plah" ></telerik:RadButton>
                                </ItemTemplate>
                            </telerik:GridTemplateColumn >
                                <telerik:GridButtonColumn HeaderText="Status" UniqueName="ImageColumn" ButtonType="ImageButton" HeaderStyle-Width="100px" />
                                <telerik:GridBoundColumn DataField="Forecast_Id" DataType="System.String" FilterControlAltText="Filter type1 column" HeaderText="ForecastId"  UniqueName="Forecast" />
                                <telerik:GridBoundColumn Display="False" DataField="Forecast_StdInt03" DataType="System.Int32" UniqueName="Status"  />
                        </Columns>
                     
 
                        <CommandItemSettings ExportToPdfText="Export to PDF"></CommandItemSettings>
 
                        <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column">
                            <HeaderStyle Width="20px"></HeaderStyle>
                        </RowIndicatorColumn>
 
                        <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column">
                            <HeaderStyle Width="20px"></HeaderStyle>
                        </ExpandCollapseColumn>
 
                        <EditFormSettings>
                            <EditColumn FilterControlAltText="Filter EditCommandColumn column"></EditColumn>
                        </EditFormSettings>
                    </MasterTableView>
 
                    <FilterMenu EnableImageSprites="False"></FilterMenu>
 
                    <HeaderContextMenu CssClass="GridContextMenu GridContextMenu_Default"></HeaderContextMenu>
                </telerik:RadGrid>

Thanks in advance for the help.

Regards Uwe


5 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 1
answered on 22 Jul 2011, 10:31 AM
Hello Uwe,

I cannot replicate the issue at my end. Here is the sample code that I tried which worked as expected.

C#:
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
 if (e.Item is GridGroupHeaderItem)
 {
    GridGroupHeaderItem groupheader = (GridGroupHeaderItem)e.Item;
    RadButton radButton = new RadButton();
    radButton.ButtonType = RadButtonType.StandardButton;
    radButton.Text = "do";
    radButton.Click+=new EventHandler(radButton_Click);
    groupheader.Cells[0].Controls.Add(radButton);
 }
}
 void radButton_Click(object sender, EventArgs e)
{
 
}

Thanks,
Princy
0
prat
Top achievements
Rank 1
answered on 08 Dec 2011, 12:28 AM
I use the same methodology to insert a button into the radgrid, except that i need to bind the text of that button to the radgrid group header. i.e I need the Group header by itself to be a link button

I achieved it thru
using this snippet in Item created and databound event
  if (e.Item is GridGroupHeaderItem)
                {
                    GridGroupHeaderItem item = e.Item as GridGroupHeaderItem;
                    LinkButton lnk = new LinkButton();
                    lnk.ID = "lnkid";
                    lnk.Text = (((Telerik.Web.UI.GridGroupHeaderItem)(e.Item)).DataCell).Text;
                    lnk.CommandArgument = (((Telerik.Web.UI.GridGroupHeaderItem)(e.Item)).DataCell).Text;
                    lnk.Command += new CommandEventHandler(btn_Click);
                    item.DataCell.Controls.Add(lnk);
                  
                }

But when the event is triggered the button does not return any Command Source or Command Argument text. It returns only &nbsp;
I want to retrieve the button text, i.e radgrid group header text.
Plz help!!




0
Shinu
Top achievements
Rank 1
answered on 08 Dec 2011, 05:04 AM
Hello Prat,

Here is the sample code which worked as expected.
C#:
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
  if (e.Item is GridGroupHeaderItem)
  {
    GridGroupHeaderItem groupheader = (GridGroupHeaderItem)e.Item;
    LinkButton link = new LinkButton();
    link.ID="Click";
    link.Text = "Click";
    groupheader.Cells[0].Controls.Add(link);
   }
}

-Shinu.
0
Mathew
Top achievements
Rank 1
answered on 06 Jan 2014, 11:38 AM
Hi Shinu,

The problem with this approach is that the button will be added to the expand/collapse column of the group header, while I need it added to the text cell along with the label.  I can achiever this by adding the control on both ItemDataBound and ItemCreated events, however in that case I get the behaviour described in the first post - i.e. the ItemCommand will only fire on the second click of the button.
0
Mathew
Top achievements
Rank 1
answered on 06 Jan 2014, 11:46 AM
Actually, it would appear that after setting the ID of the control to be the same on both ItemDataBound and ItemCreated, this now works as expected, thanks!
Tags
Grid
Asked by
uwe
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
prat
Top achievements
Rank 1
Shinu
Top achievements
Rank 1
Mathew
Top achievements
Rank 1
Share this question
or