简体   繁体   English

ASP.NET C#范围问题

[英]ASP.NET C# scope issue

OK, so I'm tyring to work with some simple stuff in ASP.NET and C# to get the hang of the super super basics, and I'm having trouble finding out how I can get this scope issue to work. 好吧,所以我想在ASP.NET和C#中使用一些简单的东西来获得超级超级基础知识,我很难找到如何让这个范围问题起作用。 At least, I think it's a scope issue! 至少,我认为这是一个范围问题! :) :)

So, here is my Default.aspx presentation markup: 所以,这是我的Default.aspx演示文稿标记:

<%@ Page Language="C#" Inherits="project1.Tree" %>
<!DOCTYPE html>
<html>
<head runat="server">
    <title>project1_test</title>
</head>
<body>
    <form runat="server">
        <p><asp:label runat="server" id="lblOutput" /></p>
        <asp:TextBox ID="txtGrowBy" runat="server" />
        <asp:Button id="btnGrow" runat="server" Text="Grow!!!" />
    </form>
</body>
</html>

And here is my CodeBehind file: 这是我的CodeBehind文件:

using System;
using System.Web;
using System.Web.UI;

namespace project1
{
    public partial class Tree : System.Web.UI.Page
    {

        public int height = 0;

        public void Grow(int heightToGrow) {
            height += heightToGrow;
        }

        protected void Page_Load(Object Source, EventArgs E)    
        {    
            Tree tree1 = new Tree();

            string msg = "Let's plant a tree!<br/>";        

            msg += "I've created a tree with a height of " +    
            tree1.height + " metres.<br/>";    

            lblOutput.Text = msg;
        }

        public virtual void btnGrowClicked (object sender, EventArgs args)
        {
            txtGrowBy.Text = tree1.heightToGrow;
        }

    }
}

Now, I believe the issue lies with the fact that I'm not using a getter and sender, but I'm not 100% sure. 现在,我认为问题在于我没有使用吸气剂和发送者,但我不是百分百肯定。

I take it you're variable height is not being maintained between postbacks? 我认为你在回发之间没有保持可变的height

This is because the web is stateless. 这是因为网络是无国籍的。 Your variables are not maintained between postbacks unless you store the values in Session, ViewState or Hidden Fields. 除非您将值存储在Session,ViewState或Hidden Fields中,否则不会在回发之间维护变量。 So you could do the following to maintain your height value between PostBacks: 因此,您可以执行以下操作来维护PostBack之间的height值:

ASPX: ASPX:

<form runat="server">
    <asp:HiddenField id="hd_Height" runat="server" />
</form>

Code Behind: 代码背后:

public int Height
{
    get
    {
       return int.Parse(hd_Height.Value).ToString();
    }
    set
    {
        hd_Height.Value = value.ToString();
    }
}

There's several immediate problems with your code; 你的代码有几个直接的问题; in the Page_Load method, you're creating a new Tree instance - you don't have to do this, as one was automatically created by IIS when the ASP.NET page was accessed. 在Page_Load方法中,您正在创建一个新的Tree实例 - 您不必这样做,因为在访问ASP.NET页面时,IIS会自动创建一个实例。 Use the this keyword in the Page_Load method to get access to that instance. 在Page_Load方法中使用this关键字可以访问该实例。

Further, nothing ever seems to call the Grow method; 此外,似乎没有任何东西可以称为Grow方法; is this intentional? 这是故意的吗? Shouldn't you be calling that from within the btnGrowClicked method? 你不应该在btnGrowClicked方法中调用它吗?

And finally as GenericTypeTea points out, your height field won't be maintained between Postbacks. 最后,当GenericTypeTea指出时,你的高度字段将不会在回发之间保持。 Easiest way to do this is with session state, eg: 最简单的方法是使用会话状态,例如:

private void PersistHeight(int height)
{
    this.Session["height"] = height;
}

private int RestoreHeight()
{
    return (int)this.Session["height"];
}

You could use viewstate as well (if Mono supports it); 您也可以使用viewstate(如果Mono支持它); simply add the updated value to viewstate before giving control back to the browser. 在将控制权交还给浏览器之前,只需将更新后的值添加到viewstate即可。 Then read from viewstate the original value saved in the last postback and incrementing the new value to it: 然后从viewstate中读取最后一个回发中保存的原始值并将新值递增到它:

public int MyValue { get {return Convert.ToInt32(viewstate["myVal"]);} set {viewstate["myVal"] = value;} } public int MyValue {get {return Convert.ToInt32(viewstate [“myVal”]);} set {viewstate [“myVal”] = value;}}

I hope that makes sense. 我希望这是有道理的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM