title: Dynamic Data - Selecting a table from a DropDownList to scaffold in a GridView
body: <p>I have been struggling with this ASP.NET Dynamic Data problem for days now...  I have a DropDownList containing the table names of all the tables in my data context (.dbml) file.  When I select the DropDownList, it needs to scaffold the selected table in a GridView.  My code works 100% in scaffolding the MetaTable in the GridView (it implements all the rules that I applied in my Meta Classes).</p>
  
  <p>However, filtering only seems to work if I explicitly add the DynamicExpression in the declaration of the QueryExtender:</p>
  
  <pre><code>&lt;asp:QueryExtender ID="GridQueryExtender" TargetControlID="GridDataSource" runat="server"&gt;
      &lt;asp:DynamicFilterExpression ControlID="FilterRepeater" /&gt;
  &lt;/asp:QueryExtender&gt;
  </code></pre>
  
  <p>This in turn requires me to specify the MetaTable explicitly in the LinqDataSource (linqdsData), either programmatically in the Page_Load or in the ASP.NET syntax.</p>
  
  <p>Since the GridView gets scaffolded in the Page_Load part of the life-cycle, the above approach does not work for me, since it takes place in the Page_Init part of the life-cycle.</p>
  
  <p>So my requirement is that as soon as I select another table to populate the GridView with from the DropDownList, the FilterRepeater needs to reflect the filters of the newly selected MetaTable.</p>
  
  <p>Is there any way for me to programmatically update the FilterRepeater in the Page_Load so that it will contain the filters of the MetaTable that I selected in the DropDownList.</p>
  
  <p>The following is some of my code:</p>
  
  <p>ASP.NET Page Code-Behind:</p>
  
  <pre><code> protected void Page_Load(object sender, EventArgs e)
      {
          if (ddlTable.SelectedIndex &gt; 0)
              {
                  string tableName = ddlDataType.SelectedValue;
  
                  linqdsData.TableName = tableName;
  
                  MetaTable mt = ASP.global_asax.DefaultModel.GetTable(tableName);
  
                  GridViewData.SetMetaTable(mt, mt.GetColumnValuesFromRoute(Context));
  
                  GridViewData.EnableDynamicData(mt.EntityType);
  
                  GridViewData.DataSourceID = linqdsData.ID;
              }
  
      }
  </code></pre>
  
  <p>ASP.NET Page: </p>
  
  <pre><code>&lt;asp:Panel runat="server" ID="pnlFilters" CssClass="gridFilterCon" EnableTheming="True"&gt;
              &lt;div class="filterGridHeading"&gt;
                  Filter the grid by:&lt;/div&gt;
              &lt;asp:QueryableFilterRepeater runat="server" ID="FilterRepeater"&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:Label ID="Label1" runat="server" Text='&lt;%# Eval("DisplayName") %&gt;' OnPreRender="Label_PreRender"
                          CssClass="gridFilterLbl" /&gt;
                      &lt;asp:DynamicFilter runat="server" ID="DynamicFilter" /&gt;
                      &lt;br /&gt;
                  &lt;/ItemTemplate&gt;
              &lt;/asp:QueryableFilterRepeater&gt;
              &lt;asp:Button ID="btnFilter" runat="server" Text="OK"
               EnableTheming="False" UseSubmitBehavior="False" OnClick="btnFilter_Click" /&gt;
          &lt;/asp:Panel&gt;
  
  
  &lt;asp:GridView ID="GridViewData" runat="server" OnSelectedIndexChanged="GridViewData_SelectedIndexChanged"
  OnPreRender="GridViewData_PreRender" OnRowDataBound="GridViewData_RowDataBound"
  OnPageIndexChanged="GridViewData_PageIndexChanged" AllowPaging="True" PageSize="50" OnInit="GridViewData_Init"&gt;
              &lt;Columns&gt;
                  ...
              &lt;/Columns&gt;
              &lt;PagerTemplate&gt;
                  &lt;asp:GridViewPager ID="GridViewPager1" runat="server" /&gt;
              &lt;/PagerTemplate&gt;
              &lt;PagerSettings Mode="NumericFirstLast" NextPageText="Next" /&gt;
          &lt;/asp:GridView&gt;
  
       &lt;asp:LinqDataSource ID="linqdsData" runat="server" ContextTypeName="pdcDataContext"
              OnSelected="linqdsData_Selected" OnSelecting="linqdsData_Selecting" EnableUpdate="True"&gt;
          &lt;/asp:LinqDataSource&gt;
          &lt;asp:QueryExtender ID="GridQueryExtender" TargetControlID="linqdsData" runat="server"&gt;
          &lt;/asp:QueryExtender&gt;
  </code></pre>
  
  <p>Your help will be greatly appreciated.</p>