title: changing DetailsViewMode on Page_Load event
body: <p>I have a simple DetailsView control connected to a data source consisting of two T-SQL queries (which work fine). However, when I try to change the mode (depending on the id in the URL) to "Edit" on Page_Load event nothing shows while "Insert" works fine. Everything seems properly data-bound.</p>
  
  <p>Also, how do I access the BoundField value (the text itself) from code-behind (FindControl always returns null for some reason)? I want to have a default value there when the page loads.</p>
  
  <p>Markup:</p>
  
  <pre><code>&lt;body&gt;
      &lt;form id="form1" runat="server"&gt;
      &lt;div class="register" runat="server"&gt;
          &lt;asp:DetailsView ID="DV" runat="server" HorizontalAlign="Center" 
              Height="100px" Width="170px" 
              AutoGenerateRows="False" DataSourceID="RegisterUser"
              OnItemCommand="Button_click"
              OnItemInserted="Insert_click" OnItemUpdated="Edit_click"&gt;
              &lt;Fields&gt;
                  &lt;asp:BoundField DataField="username" HeaderText="Name" 
                      SortExpression="username" /&gt;
                  &lt;asp:TemplateField HeaderText="Password"&gt;
                      &lt;EditItemTemplate&gt;
                          &lt;asp:TextBox ID="Password" runat="server" TextMode="Password" Text='&lt;%# Bind("userPassword") %&gt;'&gt;&lt;/asp:TextBox&gt;
                      &lt;/EditItemTemplate&gt;
                  &lt;/asp:TemplateField&gt;
                  &lt;asp:BoundField DataField="userEmail" HeaderText="Email" 
                      SortExpression="userEmail" /&gt;
                  &lt;asp:CommandField ShowInsertButton="True" ShowEditButton="True"  /&gt;
              &lt;/Fields&gt;
          &lt;/asp:DetailsView&gt;
          &lt;asp:SqlDataSource ID="RegisterUser" runat="server" 
              ConnectionString="&lt;%$ ConnectionStrings:usersConnectionString %&gt;" 
              InsertCommand="INSERT INTO korisnik(username, userPassword, userEmail) VALUES (@username, @userPassword, @userEmail)" 
              UpdateCommand="UPDATE korisnik SET userPassword = @userPassword, userEmail = @userEmail WHERE (username = @username)"&gt;
              &lt;InsertParameters&gt;
                  &lt;asp:Parameter Name="username" Type="String" /&gt;
                  &lt;asp:Parameter Name="userPassword" Type="String" /&gt;
                  &lt;asp:Parameter Name="userEmail" Type="String" /&gt;
              &lt;/InsertParameters&gt;
              &lt;UpdateParameters&gt;
                  &lt;asp:Parameter Name="userPassword" Type="String" /&gt;
                  &lt;asp:Parameter Name="userEmail" Type="String" /&gt;
                  &lt;asp:Parameter Name="username" Type="String" /&gt;
              &lt;/UpdateParameters&gt;
          &lt;/asp:SqlDataSource&gt;
          &lt;asp:Label ID="Message_label" ForeColor="red" Visible="false" runat="server" Text="[messageLabel]"&gt;&lt;/asp:Label&gt;
      &lt;/div&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p>Code-behind:</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {
      if (!IsPostBack)
      {
          string id = Request.QueryString["id"];
          // EDIT mode
          if (id == "Change_data_button")
          {
              DV.DefaultMode = DetailsViewMode.Edit;
              //DV.DataSource = RegisterUser;
              //DV.DataBind();
              DV.Fields[0].Visible = false; // preventing the value from being changed (one cannot change a username)
  
              /* found this snippet somewhere but FindControl returns null */
              TextBox user;
              user = (TextBox)DV.FindControl("username");
              user.Text = Session["LoggedUser"].ToString();
          }
          // INSERT mode
          else if (id == "Register_button")
          {
              DV.DefaultMode = DetailsViewMode.Insert;
          }
      }
  }
  </code></pre>