I use RadGrid a lot, so this one is mystifying me.
I have a RadGrid with a GridTemplateColumn which contains a button. The first time I click on the button I get a postback, but the ItemCommand handler is not triggered. However, if I click the button a second time the ItemCommand handler does get executed (which redirects to a new page so I don't get a third chance to try clicking the button).
The only thing unusual about this RadGrid is that the columns are dynamically created.
Here is the RadGrid Markup
<telerik:RadGrid ID="dgDevices" runat="server" ShowGroupPanel="False" AllowPaging="true" ItemStyle-BackColor="#ffe394" BackColor="White" Visible="true" Height="200" PageSize="25" Skin="Office2010Black" AllowSorting="false" AutoGenerateColumns="false" BorderWidth="0px" Width="1000" AllowCustomPaging="False" OnNeedDataSource="dgDevices_NeedDataSource" OnItemCommand="dgDevices_ItemCommand"> <AlternatingItemStyle BackColor="White" /> <ExportSettings ExportOnlyData="True" IgnorePaging="true" OpenInNewWindow="true" HideStructureColumns="true"> </ExportSettings> <ClientSettings AllowColumnHide="True" AllowColumnsReorder="True" AllowGroupExpandCollapse="True" ReorderColumnsOnClient="True" AllowDragToGroup="False"> <Scrolling AllowScroll="False" UseStaticHeaders="True" /> <Resizing AllowColumnResize="true" AllowRowResize="true" /> </ClientSettings> <GroupingSettings ShowUnGroupButton="False" /> <PagerStyle AlwaysVisible="true" Position="TopAndBottom" /> <MasterTableView TableLayout="Fixed" CommandItemDisplay="Top" UseAllDataFields="true" > <NoRecordsTemplate><br />No assets matched this search.<br /><br /></NoRecordsTemplate> <CommandItemSettings ShowExportToWordButton="true" ShowExportToExcelButton="true" ShowExportToCsvButton="false" ShowExportToPdfButton="false" ShowAddNewRecordButton="false" /> <Columns> <telerik:GridTemplateColumn HeaderText="View" ItemStyle-HorizontalAlign="Center" > <ItemTemplate > <asp:ImageButton ID="ibtnView" runat="server" CommandName="View" ImageUrl="images/1258747021_old-edit-find.png" ToolTip="Drill down to view more detail" /> </ItemTemplate> </telerik:GridTemplateColumn> <telerik:GridBoundColumn DataField="pct" HeaderText="% of Total" UniqueName="pct" ItemStyle-HorizontalAlign="Right" DataFormatString="{0:p}" /> <telerik:GridBoundColumn DataField="cnt" HeaderText="Device Count" UniqueName="cnt" ItemStyle-HorizontalAlign="Right" DataFormatString="{0:###,###,###}" /> </Columns> </MasterTableView> </telerik:RadGrid>I don't think the ItemCommand code is useful here since when it is called, it works.
Here is the code that creates the columns:
Sub BindColumns() dgDevices.Columns.Clear() Dim btncol As GridButtonColumn = New GridButtonColumn btncol.ButtonType = ButtonColumnType.LinkButton btncol.HeaderText = "View" btncol.CommandName = "View" btncol.Text = "<IMG src=""images/1258747021_old-edit-find.png"" border=0 /> " btncol.ItemStyle.HorizontalAlign = HorizontalAlign.Center dgDevices.Columns.Add(btncol) Dim bc As GridBoundColumn bc = New GridBoundColumn() bc.HeaderText = "Count" bc.SortExpression = "cnt" bc.DataField = "cnt" dgDevices.Columns.Add(bc) bc = New GridBoundColumn() bc.HeaderText = "Percent" bc.DataField = "pct" bc.DataFormatString = "{0:p}" dgDevices.Columns.Add(bc) Dim I As Integer For I = 0 To cblColumns.Items.Count - 1 If cblColumns.Items(I).Selected Then Dim Value As String = cblColumns.Items(I).Value bc = New GridBoundColumn() bc.HeaderText = cblColumns.Items(I).Text bc.SortExpression = cblColumns.Items(I).Value bc.DataField = Value dgDevices.Columns.Add(bc) End If NextEnd SubHere is the code that binds the data to the RadGrid - and it calls the above subroutine to add the columns
Sub BindDevices() Dim myConnection As SqlConnection = New SqlConnection(AppSettings.Get("connAccess2")) Dim dt As DataTable Dim myCommand As SqlDataAdapter Dim mySelectString As String mySelectString = " select * FROM ... " myCommand = New SqlDataAdapter(mySelectString, myConnection) dt = New DataTable() myCommand.Fill(dt) BindColumns() 'dynamically add the columns selected in the check box list dgDevices.DataSource = dt.DefaultView End SubThanks for any help.