2012-04-08 58 views
0

上一頁頁面之間傳遞變量,並獲得基於其結果在asp.net

test.Controls.Add(GetButton(thisReader["session_id"].ToString(), "Join Session")); 


protected void Button_Click(object sender, CommandEventArgs e) 
     { 
      Response.Redirect("EnterSession.aspx?session=" + e.CommandArgument.ToString()); 
     } 

在下一頁EnterSession.aspx

我想用session_id返回從MS結果session_name基於session_id。後來我想獲得的session_name對應於session_id的值,並使用這個JS調用值SQL數據庫的方法:

document.getElementById('session_name').value). 

一些想法如何做到這一點。

回答

0

我不完全確定你想要達到什麼(例如,id爲'session_name'的元素是什麼/哪裏?),但我認爲你需要在EnterSession的後面代碼中使用類似下面的內容。 aspx頁面:

protected string _SessionName = null; 
public string SessionName 
{ 
    get 
    { 
     if (null == _SessionName) 
     { 
      using (var connection = new SqlConnection("connString")) 
      { 
       connection.Open(); 
       using (var command = new SqlCommand(" SELECT session_name FROM Sessions WHERE session_id = @SessionId", connection)) 
       { 
        command.Parameters.Add("@SessionId", SqlDbType.Int); 
        command.Parameters["@SessionId"].Value = Convert.ToInt32(Request.QueryString["session"]); 

        using (var reader = command.ExecuteReader()) 
        { 
         if (reader.Read()) 
         { 
          _SessionName = reader.GetString(0); 
         } 
         else 
         { 
          throw new ArgumentException("Invalid session id"); 
         } 
        } 
       } 
      } 
     } 
     return _SessionName; 
    } 
    } 

然後在EnterSession.aspx頁面的標記,你需要這樣的事:

<input type="hidden" id="session_name" /> 
<script type="text/javascript"> 
    document.getElementById('session_name').value = '<%= this.SessionName %>'; 
</script> 
+0

感謝名單了很多。什麼是SqlDbType.Int – buni 2012-04-08 12:09:59

+0

我得到一個錯誤,說使用AddWithValue(parameterName對象值) – buni 2012-04-08 12:11:34

+0

SqlDbType.Int是指您傳遞的值的SQL類型。我不知道爲什麼你會得到這個錯誤,因爲我的代碼在我運行時似乎工作正常。如果您願意,您可以忽略這些參數並將該會話標識作爲SQL語句的一部分傳遞。 – 2012-04-09 09:36:59