简体   繁体   English

获取集属性c#的问题

[英]Issue With Get Set Properties c#

im stuck with this and im really going to bang my head against a wall in a minutes I have a logging page where the user enters there username and password and clicks login when they have pressed this a function is called to get all the user details ie firstname surname Email AccountType Examtaken etc within the function im trying to set "Get; Set;" 我坚持了这一点,我真的会在几分钟之内把我的头撞到墙上。我有一个登录页面,用户在此处输入用户名和密码,然后在他们按下此按钮后单击登录,就会调用一个函数来获取所有用户详细信息,即尝试设置“获取;设置;”功能中的名字等姓电子邮件帐户类型 Properties so i can use them on the home page, but for some stupid reason un-known to me its not working! 属性,以便我可以在主页上使用它们,但是由于某些愚蠢的原因,我不知道它不起作用! below you will see the method that is called when the user clicks login 在下面,您将看到用户单击登录时调用的方法

public class ExamMethods
{
    public int UserID { get; set; }
    public string FirstName { get; set; }
    public string Surname { get; set; }
    public string Email { get; set; }
    public int AccountType { get; set; }
    public bool ExamTaken { get; set; }

    private enum Datafield
    {
        UserID,
        FirstName,
        Surname,
        Email,
        AccountType,
        ExamTaken,
    }


    public Hashtable CheckLogin(Hashtable Usercredentials)
    {
        try
        {
            SqlConnection Connection = new SQLAccess().CreateConnection();
            SqlCommand Command = new SqlCommand("GetUserDetails", Connection);
            Command.CommandType = System.Data.CommandType.StoredProcedure;
            foreach (DictionaryEntry SpParameter in Usercredentials)
            {
                Command.Parameters.Add(new SqlParameter(SpParameter.Key.ToString(), SpParameter.Value.ToString()));
            }
            SqlDataReader da = Command.ExecuteReader();

            while (da.Read())
            {
                Details.Add("UserID", da["UserID"]);
                UserID = (da.IsDBNull((int)Datafield.UserID)) ? 0 : da.GetInt32((int)Datafield.UserID);
                Details.Add("FirstName", da["FirstName"]);
                FirstName = (da.IsDBNull((int)Datafield.FirstName)) ? "" : da.GetString((int)Datafield.FirstName);
                Details.Add("Surname", da["Surname"]);
                Surname = (da.IsDBNull((int)Datafield.Surname)) ? "" : da.GetString((int)Datafield.Surname);
                //Details.Add("AccountType", da["AccountType"]);
                //AccountType = (da.IsDBNull((int)Datafield.AccountType)) ? 0 : da.GetInt32((int)Datafield.AccountType);
                //Details.Add("ExamTaken", da["ExamTaken"]);
                //ExamTaken = (da.IsDBNull((int)Datafield.ExamTaken)) ? false : da.GetBoolean((int)Datafield.ExamTaken);
            }
            Connection.Close();
            da.Close();
            return Details;
        }
        catch
        {
            Console.WriteLine("Error Checking Login Details");
            return Details;
        }
    }
}

as you can see from the above in the while(da.read) im assigning the values to a hashtable and the get set methods when debuggin i can see values going in! 如您在while(da.read)中从上面看到的那样,在调试时将值分配给哈希表和get set方法,我可以看到值输入了! and im 100% this values arent null or empty 我100%表示此值不为null或为空

the code then reverts back to the login page with it results if all is fine then response.redirect to the home page where the user can take the exam, but in the page load of the home.aspx i have a label which i want to popualte with the users name so i reference the get propertie on the ExamMethods class but its null? 然后,代码返回到登录页面,如果一切正常,则返回结果。response.redirect到用户可以进行考试的主页,但是在home.aspx的页面加载中,我有一个标签,我想用户名popualte,所以我引用了ExamMethods类的get属性,但它为null? how can this be possible? 这怎么可能? what am i missing? 我想念什么? This is the code for the behind home.aspx page 这是后面的home.aspx页面的代码

  public partial class Home : System.Web.UI.Page
    {

       Hashtable UpdateUser = new Hashtable();
       protected void Page_Load(object sender, EventArgs e)
       {
           Methods.ExamMethods obj_UserDetails = new Methods.ExamMethods();
           if (Request.QueryString["uid"] == null)
           {
               Response.Redirect("Login.aspx");
           }
           else
             lblUserName.Text = obj_UserDetails.FirstName;

       }

Is it because I have used reponse.redirect from the login page to the home page that the get set methods are nothing? 是因为我使用了从登录页面到主页的reponse.redirect的get set方法无效吗?

Variables aren't shared among web pages when you are working with web pages. 使用网页时,网页之间不会共享变量。 As you might know HTTP is a stateless protocol. 您可能知道HTTP是无状态协议。

So how do I do this? 那么我该怎么做呢?

You need state management. 您需要状态管理。 You need a way to pass the information around. 您需要一种方法来传递信息。 From what I see, Sessions seems to be best place to store this data that you require to use in multiple pages. 从我看来, Sessions似乎是存储您需要在多个页面中使用的数据的最佳位置。

But sessions are not the only state management option you have. 但是会话不是您拥有的唯一状态管理选项。 You have many more depending on what you want to store, how much you want to store and where/when you want to access them. 根据要存储的内容,要存储的数量以及访问它们的位置/时间,还有更多信息。

I suggest you read up on ASP .NET State Management also read up on recommendations to understand which state management feature to use in which scenario. 我建议您阅读ASP.NET状态管理,也阅读建议以了解在哪种情况下使用哪种状态管理功能。

In Page_Load method you're creating a new instance of Methods.ExamMethods() each time so all of its properites are not initialized. Page_Load方法中,您每次都在创建一个Methods.ExamMethods()的新实例,因此不会初始化其所有属性。 After you are redirected to login page, perform login and are redirected back, Page_Load is executed again and a new instance of the class is created. 重定向到登录页面后,执行登录并被重定向回,再次执行Page_Load并创建该类的新实例。

The preffered way of doing it would be just reading user's data from database based on uid when you have it defined in the QueryString . 最好的方法是在QueryString定义了基于uid数据库后,从数据库中读取用户数据。

protected void Page_Load(object sender, EventArgs e)
{
    Methods.ExamMethods obj_UserDetails = new Methods.ExamMethods();

    if (Request.QueryString["uid"] == null)
    {
        Response.Redirect("Login.aspx");
    }
    else
    {
        if (!Page.IsPostback)
        {
            //read value of uid parameter
            int uid = Request.QueryString["uid"];
            //access database to retrieve user's details
            obj_UserDetails = GetUserDetails(uid);
            lblUserName.Text = obj_UserDetails.FirstName;
        }
    }

}

It's also worth monetioning that you can use Page.IsPostback attribute to fill controls with user's data. 值得一提的是,您可以使用Page.IsPostback属性将用户数据填充到控件中。 On postbacks ViewState mechanism should preserve controls' contents. 在回发时, ViewState机制应保留控件的内容。

There are also some other mechanisms that allow you to pass data between pages, like Session state . 还有其他一些机制可让您在页面之间传递数据,例如Session state This can be used if you need user details on more pages than just those two. 如果您需要更多页面上的用户详细信息而不仅仅是那两个页面,可以使用此方法。 This way you don't have to access the database to often. 这样,您就不必经常访问数据库。

Using Server.Transer() method to redirect user to another page and using PreviousPage object can give you access to other page as well, but I suppose using QueryString and reading data directly on Home page would be the most appropriate in your case. 使用Server.Transer()方法将用户重定向到另一个页面,并使用PreviousPage对象也可以使您访问其他页面,但是我想使用QueryString并直接在首页上读取数据将是最适合您的情况。

Links that can be helpful: 可能有用的链接:

Instances don't stay alive when you browse through pages on the web, you can store things you need in a session and get it when the new page loads you can do this when you need to store the data 当您浏览Web页面时,实例不会保持活动状态,您可以在会话中存储所需的内容,并在新页面加载时获取它,而在需要存储数据时可以执行此操作

Session["data"] = new Data("SomeData");

and you can do this when you need the data again 当您再次需要数据时可以执行此操作

var data = Session["data"] as Data;
if(data != null)
//do something with the data

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

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