title: auto generate number of days in another textbox upon selection of start and end date in asp.net c#
body: <p>i got a task for creating leave application for  the employees.. it contains 3 tabs one with apply leave which has </p>
  
  <ol>
  <li>type for leave</li>
  <li>description</li>
  <li>begin date</li>
  <li>end date </li>
  <li>number of days </li>
  </ol>
  
  <p>those begin and end date contains calendar extenders and once i select both dates , cursor should point on number of days <code>textbox</code> and it should calculate the total number of days leave will be taken...  please help me sort this out.. </p>
  
  <p>i have tried this </p>
  
  <h2>cs</h2>
  
  <pre><code>    protected void BtnApply_Click(object sender, EventArgs e)
      {
          MTMSDTO objc = new MTMSDTO();
  
          int Flag = 0;
  
          LblLogdInUser.Text = Session["EmpName"].ToString();
          objc.LoggedInUser = LblLogdInUser.Text;
          objc.TypeofLeave = DrpTypeofLeave.SelectedItem.Text;
  
          string date;
          date = Convert.ToDateTime(TxtBeginDate.Text).ToString("dd/MM/yyyy");
  
          DateTime dt = new DateTime();
          dt = Convert.ToDateTime(date);
  
          objc.BeginDate = dt;
          objc.EndDate = Convert.ToDateTime(TxtEndDate.Text);
          objc.Description = TxtDescription.Text;
          objc.NumofDays = Convert.ToInt32(TxtNumofDays.Text);
          //objc.EmpName = LblLogdInUser.Text;
  
  
          int X = obj.InsertLeave(objc);
          {
              if (X &gt;= 0)
              {
                  Flag = 1;
              }
              else
              {
                  Flag = 0;
  
              }
          }
  
          if (Flag == 1)
          {
              LblSuccess.Visible = true;
              LblSuccess.Text = "Data Added Successfully";
              DrpTypeofLeave.ClearSelection();
              TxtBeginDate.Text = "";
              TxtEndDate.Text = "";
              TxtDescription.Text = "";
              TxtNumofDays.Text = "";
          }
          else
          {
              LblErr.Visible = true;
              LblErr.Text = "Failed To Add Data!!!";
          }
      }
  
      protected void TxtNumofDays_TextChanged1(object sender, EventArgs e)
      {
          MTMSDTO objc = new MTMSDTO();
          TxtNumofDays.Text = Session["NumofDays"].ToString();
          objc.NumofDays = Convert.ToInt32(TxtNumofDays.Text);
      }
  
      protected void TxtEndDate_TextChanged(object sender, EventArgs e)
      {
          DateTime BeginDate = Convert.ToDateTime(TxtBeginDate.Text);
          DateTime EndDate = Convert.ToDateTime(TxtEndDate.Text);
          TimeSpan diff = EndDate.Subtract(BeginDate);
          TxtNumofDays.Text = diff.Days.ToString();
          TxtNumofDays.Focus();
      }
  </code></pre>
  
  <h2>aspx</h2>
  
  <pre><code>    &lt;asp:Panel ID="Panel1" runat="server" Height="567px" Width="858px"&gt;
                          &lt;table style="width:100%; height: 587px;"&gt;
                              &lt;tr&gt;
                                  &lt;td class="style50"&gt;
                                      &lt;/td&gt;
                                  &lt;td class="style51"&gt;
                                      &lt;asp:TabContainer ID="TabContainer1" runat="server" ActiveTabIndex="0" 
                                          Height="550px" Width="833px"&gt;
                                          &lt;asp:TabPanel ID="TabLAP" runat="server" HeaderText="Leave Application"&gt;
                                              &lt;HeaderTemplate&gt;
                                                  &lt;span class="style40"&gt;&lt;strong&gt;Leave Application&lt;/strong&gt;&lt;/span&gt;
                                              &lt;/HeaderTemplate&gt;
                                              &lt;ContentTemplate&gt;
                                                  &lt;table style="width:101%; height: 505px; margin-left: 0px;"&gt;
                                                      &lt;tr&gt;
                                                          &lt;td class="style160" style="font-weight: 700;"&gt;
                                                          &lt;/td&gt;
                                                          &lt;td class="style161" style="font-weight: 700; "&gt;
                                                              &lt;asp:Label ID="LblLogdInUser" runat="server" BorderColor="#0061C1" 
                                                                  BorderWidth="1px" Font-Bold="True" Font-Names="Verdana" Font-Size="X-Small" 
                                                                  ForeColor="#0061C1" Height="23px" Visible="False" Width="123px"&gt;&lt;/asp:Label&gt;
                                                              &lt;asp:RoundedCornersExtender ID="LblLogdInUser_RoundedCornersExtender" 
                                                                  runat="server" BorderColor="Black" Enabled="True" Radius="4" 
                                                                  TargetControlID="LblLogdInUser"&gt;&lt;/asp:RoundedCornersExtender&gt;
                                                          &lt;/td&gt;
                                                          &lt;td class="style162" style="font-weight: 700; "&gt;
                                                          &lt;/td&gt;
                                                      &lt;/tr&gt;
                                                      &lt;tr&gt;
                                                          &lt;td class="style159" style="font-weight: 700; "&gt;
                                                              Type Of Leave:&lt;/td&gt;
                                                          &lt;td class="style156"&gt;
                                                              &lt;asp:DropDownList ID="DrpTypeofLeave" runat="server" 
                                                                  AppendDataBoundItems="True" BorderColor="#0061C1" BorderWidth="1px" 
                                                                  Font-Bold="True" Font-Names="Verdana" Font-Size="X-Small" ForeColor="#0061C1" 
                                                                  Height="29px" onselectedindexchanged="DrpTypeofLeave_SelectedIndexChanged1" 
                                                                  style="text-align: left" Width="123px"&gt;&lt;asp:ListItem Selected="True" Value="0"&gt;Please Select&lt;/asp:ListItem&gt;&lt;asp:ListItem 
                                                                      Value="1"&gt;Sick Leave&lt;/asp:ListItem&gt;&lt;asp:ListItem Value="2"&gt;Casual Leave&lt;/asp:ListItem&gt;&lt;asp:ListItem 
                                                                      Value="3"&gt;Earned Leave&lt;/asp:ListItem&gt;
                                                              &lt;/asp:DropDownList&gt;
                                                              &lt;asp:RoundedCornersExtender ID="DrpTypeofLeave_RoundedCornersExtender" 
                                                                  runat="server" BorderColor="Black" Enabled="True" Radius="4" 
                                                                  TargetControlID="DrpTypeofLeave"&gt;&lt;/asp:RoundedCornersExtender&gt;
                                                          &lt;/td&gt;
                                                          &lt;td class="style141" style="font-weight: 700; "&gt;
                                                          &lt;/td&gt;
                                                      &lt;/tr&gt;
                                                      &lt;tr&gt;
                                                          &lt;td class="style146" style="font-weight: 700; "&gt;
                                                              Description:&lt;/td&gt;
                                                          &lt;td class="style154" style="font-weight: 700; "&gt;
                                                              &lt;asp:TextBox ID="TxtDescription" runat="server" BorderColor="#0061C1" 
                                                                  BorderWidth="1px" Font-Bold="True" Font-Names="Verdana" Font-Size="X-Small" 
                                                                  ForeColor="#0061C1" Height="25px" style="text-align: left" TextMode="MultiLine" 
                                                                  Width="103px"&gt;&lt;/asp:TextBox&gt;
                                                              &lt;asp:RoundedCornersExtender ID="TxtDescription_RoundedCornersExtender" 
                                                                  runat="server" BorderColor="Black" Enabled="True" Radius="4" 
                                                                  TargetControlID="TxtDescription"&gt;&lt;/asp:RoundedCornersExtender&gt;
                                                          &lt;/td&gt;
                                                          &lt;td class="style144" style="font-weight: 700; "&gt;
                                                          &lt;/td&gt;
                                                      &lt;/tr&gt;
                                                      &lt;tr&gt;
                                                          &lt;td class="style159" style="font-weight: 700; "&gt;
                                                              Begin Date:&lt;/td&gt;
                                                          &lt;td class="style156" style="font-weight: 700; "&gt;
                                                              &lt;asp:TextBox ID="TxtBeginDate" runat="server" BorderColor="#0061C1" 
                                                                  BorderWidth="1px" Font-Bold="True" Font-Names="Verdana" Font-Size="X-Small" 
                                                                  ForeColor="#0061C1" Height="25px" style="text-align: left" Width="73px"&gt;&lt;/asp:TextBox&gt;
                                                              &lt;asp:ImageButton ID="ImageButton1" runat="server" 
                                                                  AlternateText="Click to show calendar" ImageUrl="Images/calendar.png" /&gt;
                                                              &lt;asp:CalendarExtender ID="TxtBeginDate_CalendarExtender" runat="server" 
                                                                  Enabled="True" Format="dd/MM/yyyy" PopupButtonID="ImageButton1" 
                                                                  PopupPosition="Right" TargetControlID="TxtBeginDate" 
                                                                  TodaysDateFormat="dd/MM/yyyy"&gt;&lt;/asp:CalendarExtender&gt;
                                                              &lt;asp:RoundedCornersExtender ID="TxtBeginDate_RoundedCornersExtender" 
                                                                  runat="server" BorderColor="Black" Enabled="True" Radius="4" 
                                                                  TargetControlID="TxtBeginDate"&gt;&lt;/asp:RoundedCornersExtender&gt;
                                                              &lt;asp:CompareValidator ID="chkBeginDate" runat="server" 
                                                                  ControlToValidate="TxtBeginDate" Display="Dynamic" 
                                                                  ErrorMessage="You must supply a valid start date" ForeColor="Red" 
                                                                  Operator="DataTypeCheck" Type="Date" /&gt;&lt;td class="style141" 
                                                                  style="font-weight: 700; "&gt;
                                                              &lt;/td&gt;
                                                          &lt;/td&gt;
                                                      &lt;/tr&gt;
                                                      &lt;tr&gt;
                                                          &lt;td class="style159" style="font-weight: 700; "&gt;
                                                              End Date:&lt;/td&gt;
                                                          &lt;td class="style156" style="font-weight: 700; "&gt;
                                                              &lt;asp:TextBox ID="TxtEndDate" runat="server" BorderColor="#0061C1" 
                                                                  BorderWidth="1px" Font-Bold="True" Font-Names="Verdana" Font-Size="X-Small" 
                                                                  ForeColor="#0061C1" Height="25px" ontextchanged="TxtEndDate_TextChanged" 
                                                                  style="text-align: left" Width="73px" AutoPostBack ="true"&gt;&lt;/asp:TextBox&gt;
                                                              &lt;asp:ImageButton ID="ImageButton2" runat="server" 
                                                                  AlternateText="Click to show calendar" ImageUrl="Images/calendar.png" /&gt;
                                                              &lt;asp:CalendarExtender ID="TxtEndDate_CalendarExtender" runat="server" 
                                                                  Enabled="True" Format="dd/MM/yyyy" PopupButtonID="ImageButton2" 
                                                                  PopupPosition="Right" TargetControlID="TxtEndDate" 
                                                                  TodaysDateFormat="dd/MM/yyyy"&gt;&lt;/asp:CalendarExtender&gt;
                                                              &lt;asp:RoundedCornersExtender ID="TxtEndDate_RoundedCornersExtender" 
                                                                  runat="server" BorderColor="Black" Enabled="True" Radius="4" 
                                                                  TargetControlID="TxtEndDate"&gt;&lt;/asp:RoundedCornersExtender&gt;
                                                              &lt;asp:CompareValidator ID="chkEndDate" runat="server" 
                                                                  ControlToValidate="TxtEndDate" Display="Dynamic" 
                                                                  ErrorMessage="You must supply a valid end date" ForeColor="Red" 
                                                                  Operator="DataTypeCheck" Type="Date" /&gt;&lt;br /&gt;
                                                              &lt;asp:CompareValidator ID="cmpBeginAndEndDates" runat="server" 
                                                                  ControlToCompare="TxtBeginDate" ControlToValidate="TxtEndDate" 
                                                                  Display="Dynamic" ErrorMessage="The end date must be after the start date" 
                                                                  ForeColor="Red" Operator="GreaterThan" /&gt;&lt;/td&gt;
                                                          &lt;td class="style141" style="font-weight: 700; "&gt;
                                                          &lt;/td&gt;
                                                      &lt;/tr&gt;
                                                      &lt;tr&gt;
                                                          &lt;td class="style159" style="font-weight: 700; "&gt;
                                                              Number of Days:&lt;/td&gt;
                                                          &lt;td class="style156" style="font-weight: 700; "&gt;
                                                              &lt;asp:TextBox ID="TxtNumofDays" runat="server" BorderColor="#0061C1" 
                                                                  BorderWidth="1px" Font-Bold="True" Font-Names="Verdana" Font-Size="X-Small" 
                                                                  ForeColor="#0061C1" Height="25px" ontextchanged="TxtNumofDays_TextChanged1" 
                                                                  style="text-align: left" Width="73px"&gt;&lt;/asp:TextBox&gt;
                                                              &lt;asp:RoundedCornersExtender ID="TxtNumofDays_RoundedCornersExtender" 
                                                                  runat="server" BorderColor="Black" Enabled="True" Radius="4" 
                                                                  TargetControlID="TxtNumofDays"&gt;&lt;/asp:RoundedCornersExtender&gt;
                                                          &lt;/td&gt;
                                                          &lt;td class="style141" style="font-weight: 700; "&gt;
                                                          &lt;/td&gt;
                                                      &lt;/tr&gt;
                                                      &lt;tr&gt;
                                                          &lt;td class="style164" style="font-weight: 700; "&gt;
                                                              &lt;br /&gt;
                                                              &lt;asp:Label ID="LblSuccess" runat="server" Font-Bold="True" Font-Names="Verdana" 
                                                                  Font-Size="X-Small" ForeColor="#009933" Height="14px" style="text-align: right" 
                                                                  Visible="False"&gt;&lt;/asp:Label&gt;
                                                              &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
                                                              &lt;br /&gt;
                                                              &lt;asp:Label ID="LblErr" runat="server" Font-Bold="True" Font-Names="Verdana" 
                                                                  Font-Size="X-Small" ForeColor="#CC0000" Height="14px" Visible="False"&gt;&lt;/asp:Label&gt;
                                                          &lt;/td&gt;
                                                          &lt;td class="style163" style="font-weight: 700; "&gt;
                                                              &amp;nbsp;&amp;nbsp;&lt;asp:Button ID="BtnApply" runat="server" BackColor="White" 
                                                                  BorderColor="#0061C1" BorderWidth="1px" CssClass="ButtonClass" Font-Bold="True" 
                                                                  Font-Names="Verdana" Font-Size="Small" ForeColor="#0061C1" Height="21px" 
                                                                  OnClick="BtnApply_Click" Text="Apply" Width="65px" /&gt;
                                                              &lt;asp:RoundedCornersExtender ID="BtnApply_RoundedCornersExtender1" 
                                                                  runat="server" BorderColor="Black" Enabled="True" Radius="4" 
                                                                  TargetControlID="BtnApply"&gt;&lt;/asp:RoundedCornersExtender&gt;
                                                              &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
                                                              &lt;asp:Button ID="BtnCancel" runat="server" BackColor="White" 
                                                                  BorderColor="#0061C1" BorderWidth="1px" CssClass="ButtonClass" Font-Bold="True" 
                                                                  Font-Names="Verdana" Font-Size="Small" ForeColor="#0061C1" Height="21px" 
                                                                  OnClick="BtnCancel_Click" Text="Cancel" Width="65px" /&gt;
                                                              &lt;asp:RoundedCornersExtender ID="BtnCancel_RoundedCornersExtender" 
                                                                  runat="server" BorderColor="Black" Enabled="True" Radius="4" 
                                                                  TargetControlID="BtnCancel"&gt;&lt;/asp:RoundedCornersExtender&gt;
                                                              &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
                                                          &lt;/td&gt;
                                                          &lt;td class="style165" style="font-weight: 700; "&gt;
                                                          &lt;/td&gt;
                                                      &lt;/tr&gt;
                                                      &lt;tr&gt;
                                                          &lt;td class="style159" style="font-weight: 700; "&gt;
                                                              &amp;nbsp;&lt;/td&gt;
                                                          &lt;td class="style156" style="font-weight: 700; "&gt;
                                                              &amp;nbsp;&lt;/td&gt;
                                                          &lt;td class="style141" style="font-weight: 700; "&gt;
                                                              &amp;nbsp;&lt;/td&gt;
                                                      &lt;/tr&gt;
                                                  &lt;/table&gt;
                                              &lt;/ContentTemplate&gt;
                                          &lt;/asp:TabPanel&gt;
                                             &lt;/asp:Panel&gt;
  </code></pre>
  
  <p>I'm getting the number of days value in my database but its not showing in the <code>textbox</code> and cursor is not pointing too to the <code>textbox</code> of number of days wen i run the project..  </p>