2 quests 1) get current user creationdate 2) control out of context

Last post 09-04-2008 7:13 AM by Wencui Qian - MSFT. 11 replies.

Sort Posts:

  • 2 quests 1) get current user creationdate 2) control out of context

    09-01-2008, 5:17 AM
    • Member
      155 point Member
    • dolittle
    • Member since 11-30-2004, 8:00 AM
    • Posts 136

    Hi,

    2 questions here. How would I go about in getting the current logged in user creationdate? I'm trying to calculate an expiry date based on creation date. Also why would the ExpDate label control be out of context?

    protected void Page_Load(object sender, EventArgs e)

    {


    DateTime CreateDate = Membership.CreationDate;;
    DateTime ExpireDate;
    TimeSpan DaysLeft;
    ExpireDate = CreateDate.AddDays(30);
    DaysLeft =
    DateTime.Now.Subtract(ExpireDate);
    ExpDate.Text =
    Convert.ToString(DaysLeft);

     

    }

     

  • Re: 2 quests 1) get current user creationdate 2) control out of context

    09-01-2008, 12:54 PM
    • All-Star
      17,556 point All-Star
    • guru_sarkar
    • Member since 08-31-2007, 12:00 AM
    • Posts 2,663

     MembershipUser user = Membership.GetUser(username);
            DateTime createdDate = user.CreationDate;

    For the label no idea...just check if it exist/same name, etc.. 

  • Re: 2 quests 1) get current user creationdate 2) control out of context

    09-03-2008, 7:03 AM
    • Member
      155 point Member
    • dolittle
    • Member since 11-30-2004, 8:00 AM
    • Posts 136

     

    isn't Membership.GetUser(username); supposed to pickup the current logged in use? username gives a out of context error.

  • Re: 2 quests 1) get current user creationdate 2) control out of context

    09-04-2008, 2:26 AM

    Hi dolittle,

    How would I go about in getting the current logged in user creationdate?

    For this question, you could get the current username via "User.Identity.Name" and then get the creation date by Membership API. For example:

    DateTime CreateDate = Membership.GetUser(User.Identity.Name).CreationDate;

     

    Also why would the ExpDate label control be out of context?
     

    This may be caused by the confusing control reference. A simple way is to remove the label control in the page, and add a new one again. 

     

    Thanks.

    David Qian
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: 2 quests 1) get current user creationdate 2) control out of context

    09-04-2008, 3:18 AM
    • Member
      155 point Member
    • dolittle
    • Member since 11-30-2004, 8:00 AM
    • Posts 136

    Hi Wencui,

    Think I'm almost there but not quite, hence my code. I'm getting The name 'User' does not exist in this current context. I've tries adding 'using System.Security' but that didn't help. Any help would be greatly appreciated.

    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.Security;

    namespace XX.Public.account.MasterPages

    {

    public partial class AccountMaster : System.Web.UI.MasterPage

    {

     

    public void Page_Load(object sender, EventArgs e)

    {

    if (Membership.GetUser(User.Identity.Name) = null)

    {

    Membership.GetUser(User.Identity.Name) = "no active user";

    }

    DateTime CreateDate = Membership.GetUser(User.Identity.Name).CreationDate;
    DateTime ExpireDate;
    TimeSpan DaysLeft;

    ExpireDate = CreateDate.AddDays(30);
    DaysLeft =
    DateTime.Now.Subtract(ExpireDate);
    Response.Write(DaysLeft);

    }

  • Re: 2 quests 1) get current user creationdate 2) control out of context

    09-04-2008, 4:04 AM
    Answer

    Hi dolittle,

    Since the User is a property of the Page object, you can't use it in a MasterPage object.

    However, you could get the page reference via the Page property of the MasterPage object. For example:

    if (Membership.GetUser(Page.User.Identity.Name) = null) 
    {
    
    Membership.GetUser(Page.User.Identity.Name) = "no active user"; 
    }
    
    Thanks. 
    David Qian
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: 2 quests 1) get current user creationdate 2) control out of context

    09-04-2008, 5:19 AM
    • Member
      155 point Member
    • dolittle
    • Member since 11-30-2004, 8:00 AM
    • Posts 136

    Great the user membership part worked. I'll mark as answer. My head is exploding on the latter part which seems to be the easiest task for VS08. What in he heck is going on with this out of context. Label1.Text = DaysLeft; Tried new masterpage, new rebuild etc., tried everything I coud possibly think of. Some threads appear to hae the same problem. If anyone can bring something new to the table thanks.

  • Re: 2 quests 1) get current user creationdate 2) control out of context

    09-04-2008, 5:26 AM

    Hi dolittle,

    It's my pleasure to help you.

    The second question is a little strange. Could you post the related code here so that we can help you better?

    Thanks.

    David Qian
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: 2 quests 1) get current user creationdate 2) control out of context

    09-04-2008, 5:39 AM
    • Member
      155 point Member
    • dolittle
    • Member since 11-30-2004, 8:00 AM
    • Posts 136

    yes sure

     aspx file

    <%@ Master Language="C#" AutoEventWireup="true" CodeBehind="AccountMaster_LoggedIN.Master.cs" Inherits="Company.Public.Account.MasterPages.AccountMaster_LoggedIN" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >

    <head id="Head1" runat="server">

    <meta http-equiv="X-UA-Compatible" content="IE=7" />

    <asp:ContentPlaceHolder id="Header" runat="server" />

    <title>Company</title>

    <style type="text/css">

    .style1

    {

    text-align: right;

    }

    #menu

    {

    text-align: left;

    }

    </style>

    </head>

    <body>

    <form id="form1" runat="server">

    <div id="roundborder">

     

    <div id="topheader" style="text-align: left">

    <asp:Image SkinID="Toppic" runat="server" ID="toppic" AlternateText="" ImageUrl="../App_Themes/PublicTheme/images/AccountTop.jpg"/></div>

    <div id="menu">

    <asp:Menu ID="menu1" runat="server" DataSourceID="SiteMapDataSource1"

    Orientation="Horizontal" style="text-align: left" />

     

    </div>

    <div class="style1">

    <asp:LoginStatus ID="LoginStatus1" runat="server" LogoutPageUrl="~/login.aspx"

    LogoutText="Log me out" />

    &nbsp;&nbsp;&nbsp; Currently Logged in as:

    <asp:LoginName ID="LoginName1" runat="server" />

    &nbsp;&nbsp;&nbsp;Days Left: &nbsp;<asp:Label ID="Label1" runat="server"  Text=""></asp:Label>

    </div>

    <div id="container">

     

    <div id="content">

    <div id="ccolumnblank">

    <asp:ContentPlaceHolder ID="CenterColumn" runat="server" />

    </div>

    </div>

     

    </div>

     

    </div>

    Copyright 2008 Company

    <asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server"

    showstartingnode="true"/>

    </form>

    </body>

     

     

    cs file 

     

    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    namespace Company.Public.Account.MasterPages

    {

    public partial class AccountMaster_LoggedIN : System.Web.UI.MasterPage

    {

    public void Page_Load(object sender, EventArgs e)

    {

    DateTime CreateDate = Membership.GetUser(Page.User.Identity.Name).CreationDate;

    DateTime ExpireDate;

    TimeSpan DaysLeft;

    ExpireDate = CreateDate.AddDays(30);

    DaysLeft =
    DateTime.Now.Subtract(ExpireDate);

    Label1.Text = DaysLeft;

     

    }

    }

    }

  • Re: 2 quests 1) get current user creationdate 2) control out of context

    09-04-2008, 6:43 AM

    Hi dolittle,

    Based on my test, it's not caused by the Master page. It's possibly caused by the page which uses the Master page.

    Since you've defined Label1 in Master page, you can't use "Label1.Text = xx" in other page which inherits the Master page. Otherwise, it'll cause the name conflict.

    A quick way to locate the error is to search the "Label1" key words in the page which inherits the Master page. You have to both rename the label control in aspx page and modify the label name in code-behind. For example:

    <asp:Label ID="Label3" runat="server" Text=""></asp:Label>

    In code-behind:

    Label3.Text = time; 

    Thanks.

    David Qian
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: 2 quests 1) get current user creationdate 2) control out of context

    09-04-2008, 6:54 AM
    • Member
      155 point Member
    • dolittle
    • Member since 11-30-2004, 8:00 AM
    • Posts 136

    I've actually given this a unique name before with the same resut. Now I've named them both expirydaysleft so that should be unque enough for any other page not to be using it. Still I get this. Verry frustrating. Looking for a tall tree or bridge at this point. Crying

     

  • Re: 2 quests 1) get current user creationdate 2) control out of context

    09-04-2008, 7:13 AM

    Hi dolittle,

    Based on the current situation, it's difficult for us to figure out the exact error without the details. But definitely, it's a compile error. Please double click the error when compiling, and the Visual Studio IDE will automatically locate the error line. Then you could check the error.

    If this still can't resolve the problem, please provide us more details.

    Thanks.

    David Qian
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Page 1 of 1 (12 items)