title: ASPxGridView allow sorting and DataItemTemplate with ASPxCheckBox
body: <p>I have an <code>ASPxGridView</code> that contains a column with a <code>DataItemTemplate</code> that contains an <code>ASPxCheckBox</code>. I then have a button outside of the grid that performs some actions based on whether the checkbox is checked.</p>
  
  <p>The grid is defined as:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;dx:ASPxGridView ID="grid" runat="server"&gt;
      &lt;Columns&gt;
          &lt;dx:GridViewDataColumn&gt;
              &lt;DataItemTemplate&gt;
                  &lt;ds:ASPxCheckBox ID="checkbox" runat="server" /&gt;
              &lt;/DataItemTemplate&gt;
          &lt;/dx:GridViewDataColumn&gt;
      &lt;Columns&gt;
  &lt;/dx:ASPxGridView&gt;
  </code></pre>
  
  <p>The grid is populated in <code>Page_Load</code> as such:</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {
      //Some stuff
      grid.DataSource = sourceTable;
      grid.DataBind();
      //Other stuff
  }
  </code></pre>
  
  <p>Where <code>sourceTable</code> is defined as: </p>
  
  <pre><code>public DataTable sourceTable
  {
      get
      {
          if (ViewState["sourceTable"] == null)
              return new DataTable();
          return (DataTable)ViewState["sourceTable"];
      }
      set
      {
          ViewState["sourceTable"] = value;
      }
  }
  </code></pre>
  
  <p>And populated elsewhere. (The population is not important and works)</p>
  
  <p>The code is implemented as such to ensure that sorting and paging works correctly on the grid, as defined in various DevExpress support topics. This is understandable. However, when we try to get the <code>Checked</code> value from <code>checkbox</code> on the button click, it is always false:</p>
  
  <pre><code>protected void button_Click(object sender, EventArgs e)
  {
      //Some stuff
      for(int i = 0; i &lt; grid.VisibleRowCount; i++)
      {
          //Other stuff
          ASPxCheckBox checkbox = grid.FindRowCellTemplateControl(i, grid.Columns[0], "checkbox") as ASPxCheckBox;
          if(checkbox.Checked) //Always false
          {
              //Do conditional stuff
          }
          //More stuff
      }
      //Even more stuff
  }
  </code></pre>
  
  <p>I understand why this is always false as the grid rebinds the data and recreates the checkbox to its default state, any documentation I have found for this issue regarding DevExpress states to wrap the data binding in <code>Page_Load</code> in <code>if(!Page.IsPostBack)</code> which does indeed work, but breaks sorting and paging.</p>
  
  <p>I'm sure I have the solution for this in other projects I have done, I just can't find it. What should be done in this case?</p>
  
  <p>Note: I have shortened the code to as little as I can possibly get away with and have not tested it. There may be some small errors with the actual code, but use it as a guide to what I am trying to do and will clarify any issues people may come across.</p>
  
  <p>EDIT:  Using <code>if(!Page.IsPostBack)</code> or <code>if(!Page.IsCallback)</code> in <code>Page_Load</code> stops sorting and paging on the grid, removing this condition means <code>Checked</code> is always false. I need both sorting/paging to occur AND to be able to read the <code>Checked</code> value of the <code>ASPxCheckBox</code> in the <code>DataItemTemplate</code>.</p>