在onServerclick按钮后在aspx页上显示来自mssql服务器的搜索结果

时间:2019-03-08 07:38:44

标签: javascript c# asp.net sql-server

我想从.aspx页面(用户选择的范围)传递日期,并在onserverclick按钮之后显示来自mssql服务器的搜索结果。

这是我尝试过的:

  1. 编写不通过mssql传递值的applet:

HTML

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <script type="text/javascript">
    var i = <%=tVal%> ;
    alert(i);

    function AnotherFunction()
    {
    alert("This is another function");
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
                <div class="title" style="font-size: 20px"> Date : </div>
                <div>
                    <input type="text" runat="server" id="Text" value="123"/>   
                    <!-- 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. -->
                </div>
                <div>
                    <input type="submit" runat="server" class="btn btn-sm-3 btn-outline btn-wd" id="performanceSearch" value="Search" onServerclick="ServerClick"/>
                </div>
        </div>
    </form>
</body>    
</html>

C#

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);    
        }
    }
}
  

它有效

  1. 所以我在另一个applet上工作以连接到sql-server:

HTML

<html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title></title>
            <script type="text/javascript">
            var i = <%=len%> ;
            alert(i);

            function changeEventHandler(event) {
                if(!event.target.value) alert('Please make a choice!');
            }

            function performanceSearch_OnClick() {
                alert("This is another function");
            }

        </script>
    </head>
    <body>

        <div>
            <div style="font-size: 20px"> Date : </div>
            <div>
                <input type="date" runat="server" name="date0" id="date0" class="form-control"/>   
                <!-- 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. -->
            </div>
            <div>
                <input type="date"  runat="server" name="date1" id="date1" class="form-control" />
            </div>
            <div>
                <input type="submit" runat="server" id="performanceSearch" value="Search" onServerclick="performanceSearch_ServerClick"/>
            </div>         
        </div>
    </body>
</html>

C#

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);

        }
    }
}
  

但是在窗口加载(为0)时意外显示警报,第二个警报也没有显示:“这是另一个功能”。

感谢您考虑我的要求。

P.S。我正在

  • Web窗体,Asp.net,Visual Studio 2017
  • SQL Server 2008

0 个答案:

没有答案