-1

ログインページでセッションを設定し、あるページのグリッドビューデータを別のページに渡したい。

slno  title     author    publication    takenby
  1    book1    author1   pub1           sendrequest (button)

グリッドビューを示す上の画像。[ブックのリクエスト]ボタン(最初のボタン)をクリックしたとき。sendrequest.aspxページにリダイレクトされます。その行データを持ってくる必要があります(すなわち)

slno=1
title=book1
Author=author1
publication=publ

私は以下のコードuser.aspx.csを試しました

 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
            {
                string user = (string)(Session["User"]);
                if (e.CommandName.Equals("SendRequestCmd"))
                {
                    if (user == null)
                    {
                        Session["slno"] = null;
                        Session["bookname"] = null;
                        var clickedRow = ((Button)e.CommandSource).NamingContainer as GridViewRow;
                        // now access the cells like this
                        SlNo = clickedRow.Cells[0].Text;
                        Session["slno"] = SlNo.ToString();
                        BookName = clickedRow.Cells[1].Text;
                        Session["bookname"] = BookName.ToString();
                    }
                }
            }

sendquest.aspx.cs

protected void Page_Load(object sender, EventArgs e)
        {
            string slno = "", bookname = "";
            slno = (string)(Session["slno"]);
            bookname = (string)(Session["bookname"]);
            if (slno == null || bookname == null)
            {
                Response.Write("serial no: " + slno + "\nbookname: " + bookname);
            }
        }
        protected void Logout()
        {
            Session.Clear();
        }

しかし、私はエラーが発生しました

slno=(string)(Session["slno"]);
bookname=(string)(Session["bookname"]);

なぜ?誰かがそれを修正しますか?他にもっと良い方法を言いますか?

4

2 に答える 2

0

エラーを投稿しないと、問題が何であるかを確認するのは困難です。アプリケーションでセッションが有効になっていることを確認できます。

これを行う別の方法(より良い場合があります)は、id / slnoのみを(QueryStringを介して)他のページに渡し、ルックアップを実行してその段階でデータを取得することです。

于 2012-10-12T11:05:30.050 に答える
0

必ず予約用の型(クラス)を作成します

slno title author publicationプロパティと

public class Booking
{
    public Booking()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    public int slno { get; set; }
    public string Title { get; set; }
    public string author { get; set; }
    public string publication { get; set; }

}

予約タイプのオブジェクトのすべてのプロパティを設定し、そのオブジェクトをセッションに渡します

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string user = (string)(Session["User"]);
        if (e.CommandName.Equals("SendRequestCmd"))
        {
            if (user == null)
            {
                Booking b = new Booking();
                b.slno = clickedRow.Cells[0].Text;
                b.Title = BookName.ToString();

                Session["Booing"] = b;

            }
        }
    }

受信側で、そのセッション値をそのオブジェクトに再度ケースに入れて、オブジェクト名にドットを使用するだけで各プロパティにアクセスします。

   Booking b=(Booking)Session["Booking"]
       int slno=b.slno;
    string Title=b.Title;

エラーを投稿して提案や修正を行うことはできませんが、私の答えは、セッションを使用してあるページから別のページに複数の値を渡す方法です。

ありがとう

于 2012-10-12T11:08:55.730 に答える