title: The gridview is not filtered based on a pre selected value in a A dropdownlist using the model binding of the feature in ASP.NET 4.5
body: <p><strong>Note: This is ASP.NET Web Forms Model Binding in .NET 4.5 and NOT MVC</strong></p>
  
  <p>I'm  using  the  feature in ASP.NET 4.5 of the  model binding  to bind a dropdownlist and gridview  in a webform (not MVC) to methods in DAL and it's working fine as described in <a href="https://docs.microsoft.com/en-us/aspnet/web-forms/overview/presenting-and-managing-data/model-binding/sorting-paging-and-filtering-data" rel="nofollow noreferrer">filtering data with model binding and web forms</a></p>
  
  <p>The dropdownlist :</p>
  
  <pre><code> AutoPostBack="True"
  </code></pre>
  
  <p>I tried to set an initial value in the dropdownlist using the following java script:</p>
  
  <pre><code>    &lt;script&gt;
          function setSelectedValue(s, v) {
              for (var i = 0; i &lt; s.options.length; i++) {
                  if (s.options[i].text == v) {
                      s.options[i].selected = true;
                      return;
                  }
              }
          }
  
          setSelectedIndex(document.getElementById('ddlDates'), "somevalue");
      &lt;/script&gt;
  </code></pre>
  
  <p>The initial value is ok.</p>
  
  <p>It's supposed that the gridview is refreshed (filtered) based  on that value when the form is first loaded, but  that is not happened.</p>
  
  <p>It's filtered only when you pick a value from dropdownlist.</p>
  
  <p>What I missed to auto refresh the gridview when the WebForm is initially loaded?</p>
  
  <p><strong>Update</strong></p>
  
  <p>The Question Is Completely different than <a href="https://stackoverflow.com/questions/29995880/error-when-modifying-asp-net-dropdownlist-with-javascript">Error when modifying ASP.NET DropDownList with javascript</a></p>
  
  <p>I have no errors and the problem is related to data binding feature in asp.net WebForm 4.5 behaviour at start of form.</p>
  
  <p>I removed the java Script code, and used declarative declaration:</p>
  
  <pre><code>     &lt;asp:DropDownList ID="ddlDateId" runat="server" AutoPostBack="True" CssClass="form-control"
                  SelectMethod="GetMyDropDownItems"
                   SelectedValue="&lt;%# GetDefault()  %&gt;"
                  AppendDataBoundItems="True"&gt;
              &lt;/asp:DropDownList&gt;
  
              &lt;asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="True"
                  ItemType="Store.merit"
                  SelectMethod="GetMerits"
                CssClass="table table-striped"&gt;               
              &lt;/asp:GridView&gt;
  </code></pre>
  
  <p><em>Code behind</em></p>
  
  <pre><code>     public IEnumerable&lt;string&gt; GetMyDropDownItems()
              {
                  //Call DAL methods and return IEnumerable&lt;string&gt;
              }
  
          //note the [Control] Attribute that use the ddl ddlDateId
  
        public IEnumerable&lt;Merit&gt; GetMerits([Control] string ddlDateId)
              {
                   //Call DAL methods and return IEnumerable&lt;Merit&gt;
              }
  </code></pre>
  
  <p><strong>At start of form loading:</strong></p>
  
  <p>The ddl is OK filled with data , the default value is Ok
  The grid view is empty </p>
  
  <p>The Datagrid is empty  means that the parameter 'ddlDateId' that is passed to the  method 'GetMerits' is null, but really it has a value.       </p>
  
  <p>Even if I removed  the default value  expression <code>SelectedValue="&lt;%# GetDefault()  %&gt;"</code>, and using the first item in the ddl , the grid view is empty</p>
  
  <p><strong>When selecting item</strong></p>
  
  <p>In selecting item from the list (ddl) the gridview is refreshed and working fine.</p>
  
  <p>So, the question is why Asp.net framework pass null value(and really not)  to the Select method of gridView At the Start Of Form.</p>