簡體   English   中英

如何使用Session變量asp.net c#

[英]How to use Session variables asp.net c#

我似乎無法將我的變量傳遞到下一頁。 誰能告訴我我做錯了什么?

var finalValue = value * sqlCouponValue;
finalValue = Math.Round(finalValue, 2);
Session["discountedOrderTotal"] = finalValue.ToString();

我試圖在下一頁再次調用它:

e.Row.Cells[4].Text = "$" + Session["discountOrderTotal"];

有沒有人有任何想法? 我之前從未使用過會話變量,也不確定為什么只返回$ 任何幫助將非常感激。 謝謝!

你有不同的名字。 discountedOrderTotal vs discountOrderTotal

您應該注意的第一件事是會話的名稱。 它應該是相同的,當您檢索會話時,您需要指定類型,因為它返回一個對象。 因此,當您將此調用到下一頁時請嘗試此操作

e.Row.Cells[4].Text = "$" + Session["discountedOrderTotal"].ToString();
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <asp:Table ID="Table1" runat="server">
        <asp:TableRow>
            <asp:TableCell>Transfer Request ID</asp:TableCell>
                           <asp:TableCell>  <asp:Label ID="Label1" runat="server" Text='<%# Eval("TransferRequestId") %>'></asp:Label></asp:TableCell>
                           <asp:TableCell></asp:TableCell>
        </asp:TableRow>
        <asp:TableRow>
             <asp:TableCell>Employee Name</asp:TableCell>
                           <asp:TableCell>  <asp:Label ID="Label3" runat="server" Text='<%# Eval("EmployeeName") %>'></asp:Label></asp:TableCell>
                           <asp:TableCell></asp:TableCell>
        </asp:TableRow>

        <asp:TableRow>
            <asp:TableCell><asp:Button ID="Button1" runat="server" Value="APPROVE" Text="Approve" OnClick="Button1_Click"></asp:Button></asp:TableCell>

            <asp:TableCell><asp:Button ID="Button2" runat="server" value="REJECT" Text="Reject" Onclick="Button2_Click"></asp:Button></asp:TableCell>
        </asp:TableRow>
    </asp:Table>

</asp:Content>
public partial class Approve : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = Convert.ToString(Session["id"]);
        Label3.Text = Convert.ToString(Session["name"]);
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        int id1=Convert.ToInt32(Session["id"]);
        TransferDAL dalob = new TransferDAL();
        int x = dalob.updateapprove(id1);

        Response.Redirect("View.aspx");
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        int id1 = Convert.ToInt32(Session["id"]);
        TransferDAL dalob = new TransferDAL();
        int r = dalob.updatereject(id1);
        Response.Redirect("View.aspx");
    }
}
public partial class View : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            List<TransferBO> List2 = new List<TransferBO>();
            TransferDAL obj1 = new TransferDAL();
            List2 = obj1.view();
            Gridview1.DataSource = List2;
            Gridview1.DataBind();



        }

    }

    protected void Gridview1_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow SelectedRow = Gridview1.SelectedRow;
        Label id1 = (Label)SelectedRow.FindControl("Label1") as Label;
        int id2 = Convert.ToInt32(id1.Text);
        Label id3 = (Label)SelectedRow.FindControl("Label3") as Label;
        string id4 = Convert.ToString(id3.Text);
        Session["id"] = id2;
        Session["name"] = id4;
        Response.Redirect("Approve.aspx");



    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM