2014-09-01 85 views
0

我正在開發一個從sql數據庫中獲取條目的日曆。 在javascript文件裏,日曆是以編程方式編寫的,並且當它在一天內生成日曆單元格時對其進行硬編碼(在這種情況下是var計數器),我只需要將查詢放入函數中即可如何在JavaScript文件中調用一個sql函數? asp.net c#

JavaScript File

if (counter == 2 //2 is the Harcoded day 
     && mes == 8 //Hardcoded month 
     && anio == 2014) { //Harcoded year 
      PageMethods.Msg(onSuccess); 
      function onSuccess(response) { 
       alert(response); 
      } 

      htmlContent += "<div class='usuer'>Here data retrieved from DB</div>"; 
      htmlContent += "<div class='client'>Here data retrieved from DB</div>"; 
      htmlContent += "<div class='num'>Here data retrieved from DB</div>"; 
      htmlContent += "<div class='status'></div>"; 
     } 

創建後面的代碼的方法,但我不知道如何打發對象事業部獲得查詢結果並將其設置的DIV或拿到專櫃去把它的查詢和獲取具體日期的結果

代碼隱藏

[System.Web.Services.WebMethod] 
    public static string Msg() 
    { 
     return "Hello world"; 


     string connStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString; 
     using (SqlConnection myConnection = new SqlConnection(connStr)) 
     { 
      myConnection.Open(); 
      SqlCommand sqlCommand = new SqlCommand(" SELECT * FROM table",myConnection); 


      //Add parameters to the query 
      //<--- Here? How to get counter of the javascript file? :( 


      SqlDataReader dr = sqlCommand.ExecuteReader(); 
      if (dr != null) 
      { 
       while (dr.Read()) 
       { 
        //<-- Here? How to set the result to the divs? :(

       } 
      } 
     } 
    } 
+0

爲什麼是'迴歸的 「Hello world」;'頂部? – letsjak 2014-09-01 16:27:09

+0

這只是一個例子,並確保通話正常(這是我的第一個功能):c – 2014-09-01 16:29:15

+0

想一下這樣發生的順序; 1.客戶端請求頁面,2.服務器調用代碼生成頁面源代碼,3.調用結束,頁面源代碼傳輸給客戶端,4.客戶端接收頁面源代碼,5.服務器連接關閉,6.客戶端瀏覽器解釋源代碼, ''在客戶機上執行,8,結束翻譯客戶端,9,呈現頁面顯示給客戶端。即在執行_JavaScript_時(步驟「7」),所有密集目的的**,服務器端代碼已經完成,並且與服務器的連接已經關閉。 – 2014-09-01 16:35:25

回答

0

更好地利用Ajax請求將數據加載到您的日曆,但如果你必須

創建,你準備好你的數據,把它作爲參數來查看動作。 在視圖中使用<script>標籤在這裏結束您的代碼。

acction

public ActionResult MyJS() 
    { 
     calendar = new List<DateTime>(); 

     calendar.Add(DateTime.Parse("2010-03-21 12:00")); 
     calendar.Add(DateTime.Parse("2011-03-21 12:00")); 
     calendar.Add(DateTime.Parse("2012-03-21 12:00")); 

     return View(calendar); 
    } 

viev

@model List<DateTime> 
@{ 
    Layout = null; // important 
} 
@foreach (var item in Model) 
{ 
    <text> 
     alert(" @item.ToString() "); 
    </text> 
} 

,並添加到佈局<script src="home/myjs"></script>

+0

你能告訴我一個例子嗎? – 2014-09-01 16:45:07

+0

@ user3748983好的 – Sylwekqaz 2014-09-01 19:34:09

相關問題