Showing posts with label determine. Show all posts
Showing posts with label determine. Show all posts

Monday, March 26, 2012

How do I determine if user is logged in?

I want only authenticated users to save some stuff, so if a user is not logged in, I show a modalpopup to let the user log in:

if (User.Identity.IsAuthenticated)
this.SaveStuff();
else
this.ModalPopupExtender1.Show();

SaveStuff()

{

if(User.Identity.IsAuthenticated)

... save stuff

}


When the user then is logged in, I call the SaveStuff-method, but since the page is not reloaded (I guess), I cannot use User.Identity.IsAuthenticated or Membership.GetUser() to determine if user is logged in. How do I determine if the user is logged when calling the method from the modalpopup

can you access

Context.User.Identity.IsAuthenticated? (or put this value in a cookie, then reference the cookie?)


I can access the above, but it returns false even if the user is loggedin when handling the modalpopup. If I refresh the page, it returnstrue. I need to know the provideruserkey of the membershipuser who hasbeen logged in. Would it be safe to put that in a cookie?

Wednesday, March 21, 2012

How can I tell the difference between an AJAX and a regular postback in the Page_Load even

Is there anyway to determine if the page is called through an AJAX or a regular postback in the Page_Load method, kinda like you can determine if it is a postback or the initial request via the Page.IsPostBack property?

I hoped the Page.IsAsync could be used for this but it doesn't seem to work like I expected.

I think when an AJAX does a post back you cannot access any controls like Label's, buttons etc...so try accessing a label by going label.text, if it fails catch the Exception...not a good way to do it, but a cheaky option for a quick test.


if the button clicked or postback occur, the page.ispostback will be true.

if you are using ajax then the page.ispostback will be false.

this can differentiate those methods.


What you're after is ScriptManager.IsInAsyncPostBack.


Sathesh_pandian:

if the button clicked or postback occur, the page.ispostback will be true.

if you are using ajax then the page.ispostback will be false.

Test that.

You'll find that Page.IsPostBack is actually true during partial postbacks.


That's it. ScriptManager.IsInAsyncPostBack does the trick. Thank you.Big Smile


it will not work when we are using it for different page.

calling another page.


That's because that isn't a postback.