title: GridView and updatepanel - dont change a view
body: <p>I need a help with updatepanel.
  I have a grid like this:</p>
  
  <pre><code>  &lt;asp:UpdatePanel ID="UpdatePanel1" runat="server" EnableViewState="false" UpdateMode="Conditional"&gt;
           &lt;Triggers&gt;
              &lt;asp:AsyncPostBackTrigger ControlID="DropDownList2" EventName="SelectedIndexChanged" /&gt;
          &lt;/Triggers&gt;
          &lt;ContentTemplate&gt;
              &lt;asp:TextBox ID="TextBox2" runat="server" TextMode="MultiLine" Width="294px"&gt;&lt;/asp:TextBox&gt;
              &lt;br /&gt;
              &lt;br /&gt;
              &lt;asp:GridView ID="GridView1" runat="server" AllowSorting="True" 
                  BackColor="#FFFFCC" Width="100%" AutoGenerateSelectButton="True" 
                  BorderWidth="1px" EnableSortingAndPagingCallbacks="True" 
                  onselectedindexchanging="GridView1_SelectedIndexChanging" 
                  onselectedindexchanged="GridView1_SelectedIndexChanged"&gt;
                  &lt;Columns&gt;
                      &lt;asp:BoundField DataField="nazwa_dysku" HeaderText="Nazwa Dysku" 
                          SortExpression="nazwa_dysku" /&gt;
                          &lt;asp:BoundField DataField="folder" HeaderText="Folder" 
                          SortExpression="folder" /&gt;
                          &lt;asp:BoundField DataField="nazwa_pliku" HeaderText="Nazwa Pliku" 
                          SortExpression="nazwa_pliku" /&gt;
                          &lt;asp:BoundField DataField="czas_trwania" HeaderText="Czas trwania" 
                          SortExpression="czas_trwania" /&gt;
                          &lt;asp:BoundField DataField="rozmiar" HeaderText="Rozmiar" 
                          SortExpression="rozmiar" /&gt;
                          &lt;asp:BoundField DataField="data_utworzenia" HeaderText="Data utworzenia" 
                          SortExpression="data_utworzenia" /&gt;
                          &lt;asp:BoundField DataField="data_modyfikacji" HeaderText="Data modyfikacji" 
                          SortExpression="data_modyfikacji" /&gt;
                          &lt;asp:BoundField DataField="sciezka" HeaderText="Lokalizacja" 
                          SortExpression="sciezka" /&gt;
                  &lt;/Columns&gt;
                  &lt;HeaderStyle BackColor="#9A6E71" Font-Size="Medium" /&gt;
                  &lt;SelectedRowStyle BackColor="Red" /&gt;
              &lt;/asp:GridView&gt;
          &lt;/ContentTemplate&gt;
          &lt;Triggers&gt;
              &lt;asp:AsyncPostBackTrigger ControlID="GridView1" EventName="SelectedIndexChanged" /&gt;
              &lt;asp:AsyncPostBackTrigger ControlID="GridView1" EventName="SelectedIndexChanging" /&gt;
          &lt;/Triggers&gt;
      &lt;/asp:UpdatePanel&gt;
  </code></pre>
  
  <p>So when I click on select button he need to change selection and refresh the updatepanel. So:</p>
  
  <pre><code> public bool dane(int numerdysku)
      {
  
          string connectionString = "data source=localhost ;initial catalog=archiwizacjatvs;user id=root;password=wsti";
          MySqlConnection conn = new MySqlConnection(connectionString);
          try
          {
              conn.Open();
              MySqlDataAdapter dadapter = new MySqlDataAdapter("select nazwa_dysku,folder,nazwa_pliku,czas_trwania,rozmiar,data_utworzenia,data_modyfikacji,sciezka from archiwum where nazwa_dysku='" + DropDownList2.SelectedValue.ToString() + "'", conn);
              DataTable tablica = new DataTable();
              dadapter.Fill(tablica);
              DataTableReader datatablereader = tablica.CreateDataReader();
              //DataRow row;
              if (tablica.Rows.Count &gt; 0)
              {
                  /*for (int i = 0; i &lt;= tablica.Rows.Count-1; i++)
                      {
                      row = tablica.Rows[i];
                      TextBox2.Text = row[1].ToString() + " : " + row[2].ToString() + " : " + row[3].ToString() + " : " + row[4].ToString() + " : " + row[5].ToString();
                      }*/
                  GridView1.DataSource = tablica;
                  GridView1.DataBind();
                  conn.Close();
  
              }
              else
              {
                  TextBox2.Text = "Tablica jest pusta!";
              }
              conn.Close();
  
          }
          catch (Exception ex)
          {
              TextBox2.Text = ex.ToString();
          }
          return true;
      }
  
  protected void Page_Load(object sender, EventArgs e)
  {
      string connectionString = "data source=localhost ;initial catalog=archiwizacjatvs;user id=user;password=password";
                MySqlConnection conn = new MySqlConnection(connectionString);
                try
                {
                    conn.Open();
                    MySqlDataAdapter dadapter = new MySqlDataAdapter("select * from archiwum order by nazwa_dysku DESC", conn);
                    DataTable tablica = new DataTable();
                    dadapter.Fill(tablica);
                    DataTableReader datatablereader = tablica.CreateDataReader();
                    DataRow row = tablica.Rows[0];
                    for (int i = 1; i &lt;= Convert.ToInt64(row[1]); i++)
                      {
                      DropDownList2.Items.Add(i.ToString());
                      }
                    conn.Close();
                }
                catch (Exception ex)
                  {
                      TextBox2.Text = ex.ToString();
                  }
  }
  
  protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
      {
          dane(DropDownList2.SelectedIndex);
      }
  
  protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
      {
          GridView1.SelectedIndex =  e.NewSelectedIndex;
          TextBox2.Text = GridView1.SelectedIndex.ToString();
      }
  
     protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
      {
          dane(DropDownList2.SelectedIndex);
      }
  </code></pre>
  
  <p>Its working with first select or never... If I press few times F5 its working again. What I should change? </p>