RadGrid ItemCommand not firing the first time

5 posts, 0 answers
  1. Hunter
    Hunter avatar
    26 posts
    Member since:
    Aug 2012

    Posted 31 May 2013 Link to this post

    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
        Next
    End Sub

    Here 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 Sub

    Thanks for any help.











  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 03 Jun 2013 Link to this post

    Hi,

    RadGrid does not support mixing declarative grid columns with grid columns added dynamically at runtime. You should either create all the columns in the grid programmatically, or else define them all in the ASPX file. You can find out the same mentioned in the following help document:
    Programmatic Creation

    Thanks,
    Princy.
  3. Hunter
    Hunter avatar
    26 posts
    Member since:
    Aug 2012

    Posted 03 Jun 2013 Link to this post

    Hi Princy,

    Thanks for the quick reply.  I did remove the static column declarations (which I should have done earlier, I admit, just lazy sometimes).  However,  now when I click on the button it still does not fire the ItemCommand.  Also, the button in the grid column disappears (which I'm not that worried about since it should have fired the IteomCommand handler, which does a redirect).

    Any ideas?

    Thanks,
    Hunter
  4. Hunter
    Hunter avatar
    26 posts
    Member since:
    Aug 2012

    Posted 03 Jun 2013 Link to this post

    I found a fix for this.  In the grid markup I just set EnableViewState "false" and MasterTableView-EnableViewState = "false"

  5. Robert
    Robert avatar
    1 posts
    Member since:
    Feb 2015

    Posted 02 Oct 2017 Link to this post

    Thanks for the tip to set EnableViewState="false"--it solved a similar issue we were having. Does anyone know why this works?
Back to Top