title: Showing search results from mssql server on aspx page after button onServerclick
body: <p>I'd like to pass the date from a .aspx page(the range user chose), and show the searching result from mssql server after the button onserverclick.</p>
  
  <p>This is what I've tried:</p>
  
  <ol>
  <li>Write applets that Pass values without connecting mssql:</li>
  </ol>
  
  <p>HTML</p>
  
  <pre class="lang-html prettyprint-override"><code>&lt;!DOCTYPE html&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head runat="server"&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"/&gt;
      &lt;title&gt;&lt;/title&gt;
      &lt;script type="text/javascript"&gt;
      var i = &lt;%=tVal%&gt; ;
      alert(i);
  
      function AnotherFunction()
      {
      alert("This is another function");
      }
      &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form id="form1" runat="server"&gt;
          &lt;div&gt;
                  &lt;div class="title" style="font-size: 20px"&gt; Date : &lt;/div&gt;
                  &lt;div&gt;
                      &lt;input type="text" runat="server" id="Text" value="123"/&gt;   
                      &lt;!-- type="date" is not supported by any IE, or any Firefox browsers, chrome is one of the only browsers that support type="date" right now. --&gt;
                  &lt;/div&gt;
                  &lt;div&gt;
                      &lt;input type="submit" runat="server" class="btn btn-sm-3 btn-outline btn-wd" id="performanceSearch" value="Search" onServerclick="ServerClick"/&gt;
                  &lt;/div&gt;
          &lt;/div&gt;
      &lt;/form&gt;
  &lt;/body&gt;    
  &lt;/html&gt;
  </code></pre>
  
  <p>C#</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  
  namespace inputButtonTRY
  {
      public partial class ANewAsp : System.Web.UI.Page
      {
          protected string tVal;
          protected void Page_Load(object sender, EventArgs e)
          {
  
          }
          protected void ServerClick(object sender, EventArgs e)
          {
              tVal = Text.Value.ToString();
              Page.ClientScript.RegisterStartupScript(this.GetType(), "myScript", "AnotherFunction();", true);    
          }
      }
  }
  </code></pre>
  
  <blockquote>
    <p>And it works</p>
  </blockquote>
  
  <ol start="2">
  <li>So I work on another applet for connection to sql-server:</li>
  </ol>
  
  <p>HTML
  </p>
  
  <pre><code>&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
      &lt;head runat="server"&gt;
      &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"/&gt;
          &lt;title&gt;&lt;/title&gt;
              &lt;script type="text/javascript"&gt;
              var i = &lt;%=len%&gt; ;
              alert(i);
  
              function changeEventHandler(event) {
                  if(!event.target.value) alert('Please make a choice!');
              }
  
              function performanceSearch_OnClick() {
                  alert("This is another function");
              }
  
          &lt;/script&gt;
      &lt;/head&gt;
      &lt;body&gt;
  
          &lt;div&gt;
              &lt;div style="font-size: 20px"&gt; Date : &lt;/div&gt;
              &lt;div&gt;
                  &lt;input type="date" runat="server" name="date0" id="date0" class="form-control"/&gt;   
                  &lt;!-- type="date" is not supported by any IE, or any Firefox browsers, chrome is one of the only browsers that suppor type="date" right now. --&gt;
              &lt;/div&gt;
              &lt;div&gt;
                  &lt;input type="date"  runat="server" name="date1" id="date1" class="form-control" /&gt;
              &lt;/div&gt;
              &lt;div&gt;
                  &lt;input type="submit" runat="server" id="performanceSearch" value="Search" onServerclick="performanceSearch_ServerClick"/&gt;
              &lt;/div&gt;         
          &lt;/div&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>C#</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Data;
  using System.Data.SqlClient;
  using System.Linq;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  
  namespace simple_example
  {
      public partial class WebForm1 : System.Web.UI.Page
      {
          protected string A, B;
          protected int len;    
          protected void Page_Load(object sender, EventArgs e)
          {
  
          }
  
          protected void performanceSearch_ServerClick(object sender, EventArgs e)
          {
              DateTime Start = Convert.ToDateTime(date0.Value);//name=date0
              DateTime End = Convert.ToDateTime(date1.Value);
              A = Start.ToShortDateString();
              B = End.ToShortDateString();
  
              DataTable dt01 = new DataTable();
              using (SqlConnection con01 = new SqlConnection("Data Source = my_database; Initial Catalog = ; Persist Security Info = True; User ID = myUserID; Password = myPassword"))
              {      ///connect to sql
                  con01.Open();//open connection
                  SqlCommand cmd01 = new SqlCommand("SELECT* from[myDataTable] where [Time] between'" + A + " 00:00:00.000' And '" + B + " 23:59:00.000'order by [Time]", con01); ///determine sql connection
                  SqlDataAdapter da01 = new SqlDataAdapter(cmd01); ///
                  da01.Fill(dt01);
                  con01.Close();
              }
  
              len = dt01.Rows.Count;
              Page.ClientScript.RegisterStartupScript(this.GetType(), "123", "performanceSearch_OnClick()", true);
  
          }
      }
  }
  </code></pre>
  
  <blockquote>
    <p>But it accidentally shows alert while window loaded(which is 0), neither did the second alert: "This is another function" showed up.</p>
  </blockquote>
  
  <p>Thanks for considering my request.</p>
  
  <p>P.S. I'm running in </p>
  
  <ul>
  <li>Web Form, Asp.net, Visual Studio 2017</li>
  <li>SQL Server 2008 </li>
  </ul>