c# - 从一种方法访问全局声明的字符串变量会给出 null 值

标签 c# asp.net postback

我正在开发一个基于asp.net和c#的网络应用程序,我在下面指定了两种方法。

public partial class ClerkReception_CreateRecords : System.Web.UI.Page
{
string patid;   
protected void ddryear_textchanged(object sender, EventArgs e)
{
    string month = "";
    if (ddrmonth.SelectedItem.Text == "Jan")
    {
        month = "01";

    }
    else if (ddrmonth.SelectedItem.Text == "Feb")
    {
        month = "02";
    }
    else if (ddrmonth.SelectedItem.Text == "Mar")
    {
        month = "03";
    }



    string year;
    year = ddryear.SelectedItem.Text;
    string locid = Session["Location"].ToString();

    patid = locid + month + year;//Ex:AT112013


    myConnection obj = new myConnection();

    //string result = obj.fnDisplayManualRecords(year, month, locid);
    string result = obj.fnDisplayManualRecords1(patid);

    txtlast.Text = result.ToString();
    if (ddrmonth.SelectedItem.Text != null || ddryear.SelectedItem.Text != null)
    {
        txtlast.Visible = true;
        lbllast.Visible = true;
        BtnProceed.Visible = true;
    }

}

这是从下拉列表中选择项目时使用的方法,其中 patid 返回值。

我需要在另一个方法中访问相同的 patid 值,如下所示,因此我将 patid 声明为全局变量,以便我可以在任何方法中访问该值。但它给出 null。如何从一个方法中检索值方法到另一种方法?

protected void BtnProceed_Click(object sender, EventArgs e)
{


    string x = patid;//shows null
    using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString))
    {
        using (SqlCommand cmd = new SqlCommand("select top 1(SUBSTRING(patientid,9,4)) as MaxpatientID  from Patient_Data where PatientID like '"+patid+"%' order by PatientID desc;", cn))
        {
            try
            {

                cn.Open();

                using (SqlDataReader rdr = cmd.ExecuteReader())
                {
                    //int Patcount;
                    if (rdr.Read())
                    {
                        int Patcount = int.Parse(rdr["MaxpatientID"].ToString());
                        // if(Patcount == 0)


                    }
                }
            }
            catch (Exception ex)
            {

                // handle errors here
            }

        }
    }

}
}

最佳答案

全局变量是在 asp.net 的回发之间创建/初始化的,并且它们不会保留回发之间的值,因为 http 是无状态协议(protocol),您需要使用 ViewState 为此。您可以通过here阅读有关ViewStateStateless协议(protocol)的更多信息。 。

ViewState中设置值

ViewState["patid"] =  locid + month + year;//Ex:AT112013;

ViewState获取值

string patid = ViewState["patid"].ToString();

查看状态

View state's purpose in life is simple: it's there to persist state across postbacks. (For an ASP.NET Web page, its state is the property values of the controls that make up its control hierarchy.) This begs the question, "What sort of state needs to be persisted?" To answer that question, let's start by looking at what state doesn't need to be persisted across postbacks. Recall that in the instantiation stage of the page life cycle, the control hierarchy is created and those properties that are specified in the declarative syntax are assigned. Since these declarative properties are automatically reassigned on each postback when the control hierarchy is constructed, there's no need to store these property values in the view state. You can read more about viewstate here.

关于c# - 从一种方法访问全局声明的字符串变量会给出 null 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20486798/

相关文章:

c# - 在 C# 中防止 SQL 注入(inject) asp.net

asp.net - 如何 "warm-up" Entity Framework ?什么时候得到 "cold"?

asp.net - 无法加载文件或程序集 kre-clr-win - 如何指定 KRE?

c# - 使用 javascript 值和 doPostback?

c# - 列表在新表单后返回 0

c# - 是否可以更新获取的数据

javascript - 无法修改 Controls 集合,因为该控件包含代码块(即 <% ... %>)错误无法正常工作

javascript - jQuery 选择器在 ASP.NET 回发后不起作用(没有 UpdatePanel,不是 AJAX)

javascript - GridView 中的 ASP.NET Javascript 值在回发之间丢失

c# - 使用 Mock 进行 Nunit 测试。接口(interface)实例