会话变量未被看到/使用

本文关键字:使用 变量 会话 | 更新日期: 2023-09-27 18:11:37

我有一个aspx页面,显示等待审查的问题的网格视图,每页显示10个问题。如果一个问题被选中,它将在一个新页面中打开,审稿人可以提交审稿或取消审稿,并返回到需要审稿的问题列表。

目前这工作良好,但有审稿人的要求,这样,如果他们在网格视图的第4页,当他们去到一个问题,他们返回到第4页,如果他们点击取消并返回到问题列表(目前他们返回到第1页)。

所以我设置了几个会话变量来捕获所选的问题,并捕获gridview控件的pageindex以供将来使用。尝试在详细页面的页面加载上为questionId使用会话变量,但没有传递它。在返回到Review List页面时,gridview上设置的pageindex总是0,而不是会话变量。

更新Gridview控件(注意两个控件,一个超链接(一旦另一个链接按钮控件工作,它应该消失):

 <asp:GridView ID="GridView1" runat="server" Caption="Questions Awaiting Review" AllowSorting="True" PagerSettings-Mode="NumericFirstLast" OnPageIndexChanging="GridView1_PageIndexChanging"
CaptionAlign="Top" EmptyDataText="No Questions Pending Review." PageSize="10" AllowPaging="true" PagerStyle-HorizontalAlign="Center" PagerStyle-Font-Size="Large" DataKeyNames="QuestionID"
AutoGenerateColumns="false" AlternatingRowStyle-BackColor="#cccccc">
<Columns>
    <asp:TemplateField>
        <ItemTemplate>
            <asp:Label ID="QuestionID" runat="server" Text='<%# Eval("QuestionID") %>' />
        </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField DataField="KeyObjective" HeaderText="Key Objective" ItemStyle-Width="250" />
    <asp:BoundField DataField="SubmitDate" HeaderText="Submitted Date" ItemStyle-Width="60" />
    <asp:TemplateField>
        <ItemTemplate>
            <asp:HyperLink ID="Details" runat="server" NavigateUrl='<%#"~/Review/ReviewDetail.aspx?Id=" + Eval("QuestionID") +"&PageIndex=" + GridView1.PageIndex %>'>View Question</asp:HyperLink>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField>
    <ItemTemplate>
        <asp:LinkButton ID="Details2" runat="server" Text="Session" OnClick="Session_OnClick"></asp:LinkButton>
    </ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
第二个按钮的OnClick事件:
protected void Session_OnClick(object sender, EventArgs e)
    {
        Session["PageIndex"] = GridView1.PageIndex;
        Session["QuestionId"] = GridView1.SelectedDataKey;
        Response.Redirect("~/Review/ReviewDetail.aspx", false;
    }

详细页面上的连接字符串,现在没有获得参数"QuestionID"的值;):

 SqlCommand command = new SqlCommand("QuestionDetail", Conn);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@QuestionID", SqlDbType.BigInt));
            command.Parameters["@QuestionID"].Value = Convert.ToInt64(Session["QuestionId"]);
            Conn.Open();
            SqlDataReader reader = command.ExecuteReader();

PageLoad和gridview绑定在ReviewList页面,应该使用会话变量来设置页面的网格控制,但总是将默认的页面0:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
                bindGridView();
        }
        else
        {
            if (Convert.ToInt32(Session["PageIndex"]) !=0)
            {
                GridView1.PageIndex = Convert.ToInt32(Session["PageIndex"]);
                bindGridView();
            }
        }
    }
   private void bindGridView()
   {
            string connectionString = WebConfigurationManager.ConnectionStrings["CS1"].ConnectionString;
            string selectSQL = String.Format("Select QuestionID, KeyObjective, SubmitDate from Questions where Author <> '{0}' and QuestionID not in(Select QuestionID from Review where Reviewer = '{0}')", User.Identity.Name);
            SqlConnection con = new SqlConnection(connectionString);
            SqlCommand cmd = new SqlCommand(selectSQL, con);
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            adapter.Fill(ds, "Review");
            GridView1.DataSource = ds;
            GridView1.DataBind();
   }

会话变量未被看到/使用

您的错误可能意味着Request["Id"]为空或不存在。始终检查querystring是否存在,并执行在try-catch块中可能失败的用户输入的转换。

    protected void Page_Load(object sender, EventArgs e)
    {
        long QuestionID = -1;
        //check if the Id QueryString exists
        if (Request.QueryString["Id"] != null)
        {
            //try to convert to int64
            try
            {
                QuestionID = Convert.ToInt64(Request.QueryString["Id"]);
            }
            catch
            {
            }
        }
        //if valid QuestionID
        if (QuestionID >= 0)
        {
            using (SqlConnection connection = new SqlConnection(Common.connectionString))
            using (SqlCommand command = new SqlCommand("QuestionDetail", connection))
            {
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add("@QuestionID", SqlDbType.BigInt).Value = QuestionID;
                //try to execute the stored procedure
                try
                {
                    connection.Open();
                    command.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    //handle sql error
                    Literal1.Text = ex.Message;
                }
            }
        }
    }

为什么要在第一个按钮上做PostBack ?这不仅仅是为了重定向到另一个url。改为:

<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%#"~/Review/ReviewDetail.aspx?Id=" + Eval("QuestionID") %>'>View Question</asp:HyperLink>

或者页面索引也在QueryString中:

<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%#"~/Review/ReviewDetail.aspx?Id=" + Eval("QuestionID") + "&Page=" + GridView2.PageIndex %>'>View Question</asp:HyperLink>

如果你真的想通过PostBack来设置会话,你可以使用OnRowCommand:

<asp:LinkButton ID="LinkButton1" CommandArgument='<%# Eval("QuestionID") %>' runat="server" CommandName="viewQuestion">View Question</asp:LinkButton>
CS

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "viewQuestion")
        {
            Session["PageIndex"] = GridView1.PageIndex;
            Session["QuestionId"] = e.CommandArgument.ToString();
            Response.Redirect("~/Review/ReviewDetail.aspx?Id=" + Convert.ToString(Session["QuestionId"]));
        }
    }