title: ASP.net Speeding up the dropdownlist load by preloading its data from table
body: <p>I have a grid view that uses dropdownlist to select employees ID but displaying names.  </p>
  
  <pre><code> &lt;EditItemTemplate&gt;
   &lt;asp:DropDownList ID="DropDownList5" runat="server" AppendDataBoundItems="True" DataSourceID="SqlDataSourceEmployees" DataTextField="name" DataValueField="empID" SelectedValue='&lt;%# Bind("employee") %&gt;'&gt;
                                  &lt;asp:ListItem&gt;&lt;/asp:ListItem&gt;
                              &lt;/asp:DropDownList&gt;
    &lt;/EditItemTemplate&gt;
  </code></pre>
  
  <p>This works fine, but the SqlDataSourceEmployees is called the moment user clicks on the dropdownlist, which causes quite annoying delay, since as I understand first it fires a SQL command (simple <code>SELECT empID,NAME FROM EMPLOYEES WHERE department=@department</code>) and then the list is populated. It would be much better to bind the dropDowList to something that is already in memory, especially that I don't have to worry that the data in the list would change after page has been loaded.</p>
  
  <p>I've thought about loading it to the DataTable on PageLoad and then binding such table to dropDownList but the list cannot find mentioned above table. I've even put the DataTable as public method of the webpage:</p>
  
  <pre><code>public partial class PlannersCurrentRoster : System.Web.UI.Page
  {
  
   private DataSet.employeesDataTable employeesTable;
  
  public DataSet.employeesDataTable EmployeesTable
  {
      get { return employeesTable; }
      set { employeesTable = value; }
  }
  
  protected void Page_Load(object sender, EventArgs e)
  {
  
      DataSetTableAdapters.employeesTableAdapter TA = new DataSetTableAdapters.employeesTableAdapter();
      DataSet.employeesDataTable empTable = TA.GetDataByDepartment(lblDepartment.Text);
      EmployeesTable = empTable;
  </code></pre>
  
  <p>but then changing the bind of the list</p>
  
  <pre><code>&lt;EditItemTemplate&gt;
                              &lt;asp:DropDownList ID="DropDownList5" runat="server" AppendDataBoundItems="True" DataSourceID="EmployeesTable" DataTextField="name" DataValueField="empID" SelectedValue='&lt;%# Bind("employee") %&gt;'&gt;
                                  &lt;asp:ListItem&gt;&lt;/asp:ListItem&gt;
                              &lt;/asp:DropDownList&gt;
  </code></pre>
  
  <p>fails to find the "EmployesTable". </p>
  
  <p>EDIT:</p>
  
  <p>Following the solution below I've tried:</p>
  
  <pre><code>protected void GridView5_RowUpdating(object sender, GridViewUpdateEventArgs e)
  { ((DropDownList)GridView5.Rows[e.RowIndex].FindControl("DropDownList5")).DataSource = EmployeesTable;
      ((DropDownList)GridView5.Rows[e.RowIndex].FindControl("DropDownList5")).DataBind();
  </code></pre>
  
  <p>}</p>
  
  <p>Which doesn't speed up things a bit, I'm sure that the DDL still takes data from SQL source (when I was trying to remove it, I had an error stating that SelevtedValue is invalid)</p>
  
  <p>So I've tried to assign it one step earlier, during editing event</p>
  
  <pre><code> protected void GridView5_RowEditing(object sender, GridViewEditEventArgs e)
  {
      ((DropDownList)GridView5.FindControl("DropDownList5")).DataSource = EmployeesTable;
      ((DropDownList)GridView5.FindControl("DropDownList5")).DataBind();
  
  
  }
  </code></pre>
  
  <p>but then it fails to find dropdownlist5</p>
  
  <p>EDIT: I give up. After reading <a href="http://msdn.microsoft.com/en-us/library/vstudio/cc668225(v=vs.100).aspx" rel="nofollow">this article</a> I've simply changed SQLDataSource type to DataReader which indeed improved performance. Or maybe its the placebo effect for my tired mind.</p>