gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 21:40:48 25 4
gpt4 key购买 nike

我正在开发一个基于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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com