title: Converting code from my .aspx page to code behind in the C# .cs page
body: <p>Learning how to code in ASP.NET via Visual Studio comes with its pros and cons. The pros are that everything is so easy to do via drag and drop, but the con is that I don't know much about the code behind and all of this "data binding". I have this code in my .aspx page, but I'm not sure how to "convert" it to the code behind. For example, I am right now attempting to implement an application that is in essence a searchable (that has a drop down list to serve as a filter on which column to search on) Gridview with paging that has read and write capabilities. Here is the code I have thus far in my .aspx page:</p>
  
  <pre><code>&lt;head runat="server"&gt;
      &lt;title&gt;&lt;/title&gt;
      &lt;style&gt;
          .hiddencol 
          { 
              display: none; 
          }
       &lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;form id="form1" runat="server"&gt;
  &lt;div&gt;
      &lt;asp:TextBox ID="SearchBox" runat="server"&gt;&lt;/asp:TextBox&gt;
      &lt;asp:DropDownList ID="SearchParameterList" runat="server"&gt;
          &lt;asp:ListItem Selected="True" Value="Project_Name"&gt;Project Name&lt;/asp:ListItem&gt;
          &lt;asp:ListItem Value="Product"&gt;Product&lt;/asp:ListItem&gt;
          &lt;asp:ListItem Value="Description"&gt;Description&lt;/asp:ListItem&gt;
      &lt;/asp:DropDownList&gt;
      &lt;asp:Button ID="SearchButton" runat="server" Text="Search" OnClick="SearchButton_Click" /&gt;
      &lt;asp:Button ID="ClearButton" runat="server" OnClick="ClearButton_Click" Text="Clear" /&gt;
      &lt;br /&gt;
      &lt;br /&gt;
  
      &lt;asp:GridView ID="ProjectTable" runat="server" Font-Names="Verdana,Arial" Font-Size="12px" AutoGenerateColumns="False" AutoGenerateEditButton="True" DataSourceID="PopulateProjectTable" AllowPaging="True" OnPageIndexChanging="ProjectTable_PageIndexChanging&gt;
          &lt;AlternatingRowStyle BackColor="#BFE4FF" /&gt;
          &lt;PagerStyle BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" /&gt;
          &lt;HeaderStyle Height="30px" BackColor="#6DC2FF" Font-Size="12px" BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" /&gt;
          &lt;RowStyle Height="20px" Font-Size="12px" BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" /&gt;
          &lt;Columns&gt;
              &lt;asp:BoundField DataField="ProjID" HeaderText="ProjID" ItemStyle-CssClass="hiddencol" HeaderStyle-CssClass="hiddencol" SortExpression="ProjID" /&gt;
              &lt;asp:BoundField DataField="Project_Name" HeaderText="Project Name Value" ReadOnly="True" SortExpression="Project_Name" /&gt;
              &lt;asp:BoundField DataField="Product" HeaderText="Product" SortExpression="Product" ReadOnly="True" /&gt;
              &lt;asp:BoundField DataField="Product_Edit" HeaderText="Product (alternate)" SortExpression="Product_Edit" /&gt;
              &lt;asp:BoundField DataField="Description" HeaderText="Description" ReadOnly="True" SortExpression="Description" /&gt;
              &lt;asp:BoundField DataField="Description_Edit" HeaderText="Description (alternate)" SortExpression="Description_Edit" /&gt;
              &lt;asp:BoundField DataField="Comment" HeaderText="Comment Submission/Approval" SortExpression="Comment" /&gt;
          &lt;/Columns&gt;
      &lt;/asp:GridView&gt;
      &lt;asp:SqlDataSource ID="PopulateProjectTable" runat="server" 
          ConnectionString="&lt;%$ ConnectionStrings:ODSConnectionString %&gt;" 
          SelectCommand="SELECT [Project_Name], [Product], [Product_Edit], [Description], [Description_Edit], [Comment], [ProjID] FROM [Pipeline_Detail]" 
          UpdateCommand="UPDATE [Pipeline_Detail] SET Product_Edit = @Product_Edit, Description_Edit= @Description_Edit, Comment = @Comment WHERE ProjID = @ProjID"&gt;
          &lt;UpdateParameters&gt;
              &lt;asp:Parameter Name="ProjID"/&gt;
              &lt;asp:Parameter Name="Product_Edit" Type="String"/&gt;
              &lt;asp:Parameter Name="Description_Edit" Type="String"/&gt;        
              &lt;asp:Parameter Name="Comment" Type="String"/&gt;
          &lt;/UpdateParameters&gt;
      &lt;/asp:SqlDataSource&gt;
  
  &lt;/div&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p></p>
  
  <p>And here is what I have in the code behind:</p>
  
  <pre><code>public partial class Default : System.Web.UI.Page
  {
      protected void Page_Load(object sender, EventArgs e)
      {
  
      }
  
      protected void SearchButton_Click(object sender, EventArgs e)
      {
          PopulateProjectTable.FilterExpression = SearchParameterList.SelectedValue.ToString() + " LIKE " + "'" + "%{0}%" + "'";
      }
  
      protected void ClearButton_Click(object sender, EventArgs e)
      {
          PopulateProjectTable.FilterExpression = "Project_Name LIKE '%%'";
          SearchBox.Text = "";
      }
  }
  </code></pre>
  
  <p>Very barren. The issue I'm running into right now is that the paging "resets" itself after I search. For example, if I search up something that can't be displayed in one page, it'll obviously page it. If I click on any/all pages that isn't Page 1, the gridview resets back to its original select statement and shows what would be page 2 of THAT. I did some research and I see that it is an issue with the way the data is bound (feel free to correct me if that isn't the issue here). As such, how can I remedy the problem I'm having right now, and will it require me to convert my .aspx page to code behind in C#?</p>
  
  <p>Thanks for any help.</p>
  
  <p>EDIT: I made a big change with my code behind:</p>
  
  <pre><code>public partial class Default : System.Web.UI.Page
  {
      protected void Page_Load(object sender, EventArgs e)
      {
  
      }
  
      protected void SearchButton_Click(object sender, EventArgs e)
      {
          ProjectTable.PageIndex = 0;
          ProjectTable.DataSourceID = null;
          PopulateProjectTable.SelectCommand = "SELECT [Project_Name], [Product], [Product_Edit], [Description], [Description_Edit], [Comment], [ProjID] FROM [Pipeline_Detail] WHERE " + SearchParameterList.SelectedValue.ToString() + " LIKE '%" + SearchBox.Text + "%'";
          ProjectTable.DataSource = PopulateProjectTable;
          ProjectTable.DataBind();
      }
  
      protected void ClearButton_Click(object sender, EventArgs e)
      {
          ProjectTable.PageIndex = 0;
          ProjectTable.DataSourceID = null;
          PopulateProjectTable.SelectCommand = "SELECT [Project_Name], [Product], [Product_Edit], [Description], [Description_Edit], [Comment], [ProjID] FROM [Pipeline_Detail] WHERE Project_Name LIKE '%%'";
          ProjectTable.DataSource = PopulateProjectTable;
          ProjectTable.DataBind();
          SearchBox.Text = "";
      }
  
      protected void ProjectTable_PageIndexChanging(object sender, GridViewPageEventArgs e)
      {
          ProjectTable.PageIndex = e.NewPageIndex;
          ProjectTable.DataSourceID = null;
          PopulateProjectTable.SelectCommand = "SELECT [Project_Name], [Product], [Product_Edit], [Description], [Description_Edit], [Comment], [ProjID] FROM [Pipeline_Detail] WHERE " + SearchParameterList.SelectedValue.ToString() + " LIKE '%" + SearchBox.Text + "%'" ;
          ProjectTable.DataSource = PopulateProjectTable; 
          ProjectTable.DataBind();
  
      }
  }
  </code></pre>
  
  <p>I'm now getting these type of errors:</p>
  
  <pre><code>The GridView 'ProjectTable' fired event RowEditing which wasn't handled.
  The GridView 'ProjectTable' fired event RowCancelingEdit which wasn't handled.
  </code></pre>
  
  <p>Anyone know what's up?</p>