2016-05-04 7 views
0

セッション変数を送信し、ページが変数を読み込み、セッション変数の文字列に基づいてデータを表示する別のページにリダイレクトするボタンがあります。それは動作しますが、ページを更新しないと表示されません。 ViewLeage.aspxためグリッドビューのデータは、ページをリフレッシュするまで表示されません。C#

protected void btnJoin_Click(object sender, EventArgs e) 
{ 
    Button lb = (Button)sender; 
    GridViewRow row = (GridViewRow)lb.NamingContainer; 
    getText1 = row.Cells[1].Text; 
    Session["showName"] = getText1; 
    Response.Redirect("ViewLeague.aspx"); 
} 

とHERESにページのロード:HERESにボタンクリック

string SQLName = "SELECT teamname, draftedPlayer1, player1FP, draftedPlayer2, player2FP, draftedPlayer3, player3FP, draftedPlayer4, player4FP, totalFP FROM Drafted_Table WHERE LeagueName = '" + Name2 + " '"; 

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) 
    { 
     if (this.Session["showName"] != null) 
     { 
      Name2 = (String)this.Session["showName"]; 
     } 
    } 

EDIT:

相続人のデータがGridViewの

private void GetData() 
{ 
    DataTable table = new DataTable(); 

    // get the connection 

    using (SqlConnection conn = new SqlConnection("Data Source= tyler-pc\\sqlexpress; Integrated Security=true; Database=CODFANTASY2")) 

    { 

     // write the sql statement to execute 

     string sql = SQLName; 

     // instantiate the command object to fire 

     using (SqlCommand cmd = new SqlCommand(sql, conn)) 

     { 

      // get the adapter object and attach the command object to it 

      using (SqlDataAdapter ad = new SqlDataAdapter(cmd)) 

      { 

       // fire Fill method to fetch the data and fill into DataTable 

       ad.Fill(table); 

      } 

     } 

    } 

    // specify the data source for the GridView 

    ViewLeagueTable.DataSource = table; 

    // bind the data now 

    ViewLeagueTable.DataBind(); 


} 
に追加されます

ご迷惑をおかけして申し訳ございません。私はここで見ている

+1

まず、あなたが使用しています実際に何かを割り当てる前にSQLクエリの 'Name2'値を返します。 (注意:それを使用する方法もSQLインジェクションの脆弱性を示します)。したがって、この節は常にWHERE LeagueName = '' 'になります。第2に、 'ViewLeague.aspx'の' Page_Load'イベントは実際にユーザーにデータを表示しません。他のイベントがそれをしている場合、そのイベントがトリガーされるまで何も表示されません。 – David

+0

データが追加される部分を追加します – user2993767

+0

大変感謝しています。私はSQL文字列を移動する必要がありました – user2993767

答えて

1

2つの問題...

リダイレクトを実行まず、:

Response.Redirect("ViewLeague.aspx"); 

呼び出されるターゲットページ(ViewLeague.aspx)のイベントのみであるが初期化/ロード/ etc。イベント。ボタンのクリックによってリダイレクトが発生した可能性がありますが、ターゲットページのボタンはクリックされなかったので、他のハンドラは使用されません。このように、そのページが読み込まグリッドに表示する何のためのために、それはPage_Loadハンドラで発生する必要が

:あなたが何かを割り当てる前

protected void Page_Load(object sender, EventArgs e) 
{ 
    //... 
    GetData(); 
} 

第二、あなたは値を使用していますそれへ:

string SQLName = "SELECT teamname, draftedPlayer1, player1FP, draftedPlayer2, player2FP, draftedPlayer3, player3FP, draftedPlayer4, player4FP, totalFP FROM Drafted_Table WHERE LeagueName = '" + Name2 + " '"; 

// Later... 
Name2 = (String)this.Session["showName"]; 

あなたがそれを使用する場合Name2が空だった場合、そのSQL句は次のようになります。

WHERE LeagueName = '' 

したがって、空のLeagueName値のレコードがない場合、表示するデータはありません。値を取得した後、クエリの値を設定する必要があります。 (そしてあなたはSQLインジェクションを防ぐために、パラメータを使用して行うべきです。)

string SQLName = "SELECT teamname, draftedPlayer1, player1FP, draftedPlayer2, player2FP, draftedPlayer3, player3FP, draftedPlayer4, player4FP, totalFP FROM Drafted_Table WHERE LeagueName = @Name"; 

// Later... 
Name2 = (String)this.Session["showName"]; 

// Later... 
var cmd = new SqlCommand(SQLName); 
cmd.Parameters.Add("@Name", SqlDataType.VarChar, 25).Value = Name2; 
// etc. 

(私は、それに応じて調整する列の型とサイズに推測しなければならなかった。)

関連する問題