c# - 单击 C# 中的按钮时变量会重新初始化

标签 c# asp.net boolean

我正在制作一份在线表格。我一开始就在代码中初始化了 4 个变量。当我选择一个下拉列表时,会触发一个事件 (DropDownList4_SelectedIndexChanged ),该事件又调用 Availability()。这里我的 boolean 变量 avail_bus 被分配了一个值。但是,当我单击提交按钮( Button1_Click1)时,变量 avail_bus 被重新初始化为 false。我对此进行了调试,发现单击“提交”(Button1_Click1) 后,控件首先转到页面中代码的顶部,即

public partial class Appform : System.Web.UI.Page
    {
        private bool isNotDup = true;
        private bool avail_bus ;
        private int max_capacity_bus;
        private int realAvailability;
}

然后转到 Button1_click1 。 我怎样才能防止这种情况发生?如果在调用可用性时,avail_bus 的状态更改为 true,则当我单击提交时,它不应重新初始化为 true。

下面是我的代码:

namespace eTransport
{
    public partial class Appform : System.Web.UI.Page
    {
        private bool isNotDup = true;
        private bool avail_bus ;
        private int max_capacity_bus;
        private int realAvailability;

        protected void Page_Load (object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                BindDropDown();


            }

        }

        //Method called when dropdown is selected in Bus Stop. It helps to populate Bus Number
        protected void DropDownList4_SelectedIndexChanged (object sender, EventArgs e)
        {

            AutoPopulateBusStop();
            Availability();

        }


        //Method to load drop down values in Bus Stop. These are populated from database
        protected void BindDropDown ()
        {
           //some code here
        }

        //Method to autopopulate Bus Number based on selection of Bus Stop. The mapping is in the database in the table named -> dropdownlist
        protected void AutoPopulateBusStop ()
        {
            //some code here
        }


        protected void Availability ()
        {
            string constr5 = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
            using (SqlConnection con5 = new SqlConnection(constr5))
            {
                try
                {

                    using (SqlCommand cmd5 = new SqlCommand("select count(*) from etms where BusNo='" + TextBox6.Text.ToString() + "'"))
                    {
                        cmd5.CommandType = CommandType.Text;
                        cmd5.Connection = con5;
                        con5.Open();
                        int capacity_from_db = Convert.ToInt16(cmd5.ExecuteScalar());
                        realAvailability = max_capacity_bus - capacity_from_db;

                        if (realAvailability > 0)
                        {
                            avail_bus = true;
                            TextBox2.Text = realAvailability.ToString() + " seats available ";
                            TextBox2.ForeColor = System.Drawing.ColorTranslator.FromHtml("#008000");

                        }
                        else
                        {

                            TextBox2.Text = "Seats Not available. Please choose another Stop";
                            TextBox2.ForeColor = System.Drawing.ColorTranslator.FromHtml("#ff1919");
                        }

                    }
                }
                catch (Exception ex)
                {
                    Response.Write(ex);
                }

            }

        }


        protected void Button1_Click1 (object sender, EventArgs e)
        {


            if (isNotDup)
            {
                if (avail_bus)
                {
                   // Submit the Form
                }
                else
                {
                    Label14.Text = "Bus Seats not available!";
                    Label15.Text = null;
                }
            }
        }



        protected void PhoneNumberValidatation (object source, ServerValidateEventArgs args)
        {

           //some code here



        }


         }
}

最佳答案

这个问题有三种可能的解决方案。

Static - 这将创建一个可供所有页面访问的实例(全局)。

private static avail_bus = true;

Session State - 这使您能够在用户导航时存储和检索用户的值。

// Get...
private bool avail_bus = (bool)Session["avail_bus"];
// Set
Session["avail_bus"] = true;

Control.ViewState - 获取状态信息字典,允许您在同一页面的多个请求中保存和恢复服务器控件的 View 状态。

public bool avail_bus
{
    get { return  ViewState["avail_bus"] == null ? false : (bool)ViewState["avail_bus"]; }
    set { ViewState["avail_bus"] = value; }
}

关于c# - 单击 C# 中的按钮时变量会重新初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32941828/

相关文章:

c# - WinForms:无需转到 FormWindowState.Normal 即可找到最小化表单的大小

html - css 填充在媒体中不起作用

c# - javascript通过后面的代码确认

c# - 在 div 中设置默认按钮 - 但我不知道 ID/类

javascript - if (true || true || false) 语句在 javascript 中是否为真?

c# - 发生意外的鼠标单击事件 .NET Compact Framework 3.5

c# - 强制 EF ApplicationUser 加载导航属性

c# - 在 ASP.NET MVC 的自定义模型 Binder 中绑定(bind)属于另一个对象的列表

c++ - boolean 函数不能正常工作

c - boolean 值的轻量级矩阵