2012-04-08 9 views
0

前のページページ間の変数を渡すと、次のページEnterSession.aspxに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()); 
     } 

で、それに基づいて結果を取得

私は、MSからの結果session_nameを返すためにsession_idを使用したいですSQLデータベースは、私がsession_idに対応session_nameの値を取得し、このJSメソッドを使用して値を呼び出したいsession_id .Thenをもと:

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

これを行うにはいくつかのアイデア。

答えて

0

私はあなたが達成しようとしていることを完全には分かっていません(例えば、どこに 'session_name'というIDの要素がありますか?)、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

+0

AddWithValue(parameterNameオブジェクト値)を使用するとエラーが発生します。 – buni

+0

SqlDbType.Intは渡す値のSQL型を参照します。私はそれを実行するときに私のコードが正常に動作するように見えるので、なぜあなたがそのエラーを得るのか分かりません。パラメータを無視して、必要に応じてセッションIDをSQL文の一部として渡すことができます。 –

関連する問題