This is a migrated thread and some comments may be shown as answers.
RadGrid ItemCommand not firing the first time
4 Answers 50 Views
This is a migrated thread and some comments may be shown as answers.
Hunter
Top achievements
Rank 1
Hunter asked on 31 May 2013, 11:29 PM

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.











4 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 1
answered on 03 Jun 2013, 07:13 AM
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.
0
Hunter
Top achievements
Rank 1
answered on 03 Jun 2013, 02:09 PM
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
0
Hunter
Top achievements
Rank 1
answered on 03 Jun 2013, 11:01 PM
I found a fix for this.  In the grid markup I just set EnableViewState "false" and MasterTableView-EnableViewState = "false"

0
Robert
Top achievements
Rank 1
answered on 02 Oct 2017, 04:05 PM
Thanks for the tip to set EnableViewState="false"--it solved a similar issue we were having. Does anyone know why this works?
Tags
Grid
Asked by
Hunter
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
Hunter
Top achievements
Rank 1
Robert
Top achievements
Rank 1
Share this question
or