Sunday, October 4, 2009

Sign in from any content page and navigate to same or differnt page as required.

- There are many actions on web page that requires login of a user.
like Comments, alerts, downloads etc.
- Thus make a login-popup by modalpopupextender of ajax tool kit.
- Put it in master page and Attached (TargetControlID) it to a temporary
link button which is kept in a hidden div
as


IN MASTER page:

<div visible="false" style="display:none" >
<cc1:ModalPopupExtender ID="modalpopSignin" OnCancelScript="javascript: return clearSigninPopup();" BackgroundCssClass="modalBackground"
runat="server" TargetControlID="lnk_click" CancelControlID="lnk_BtnClose" PopupControlID="pnl_LoginCommon">
</cc1:ModalPopupExtender>
<asp:LinkButton ID="lnk_click" runat="server">a</asp:LinkButton>
</div>


1. OnCancelScript="javascript: return clearSigninPopup();"
this javascript function: clearSigninPopup is used to clear all textbox and error message(validators, lables)
so that when popup panel is displayed it is totally clear


2. BackgroundCssClass="modalBackground"
to make background as gray with some opacity to show pop ha came with background blur.
make css and put class as:

.modalBackground {
filter: alpha(opacity=70); background-color: gray; opacity: 0.7;
}
add refernce of that css in that webpage.

3. TargetControlID="lnk_click" : attach it to a dummy linkbutton as it is mandatory for a modalpoup extender to attach to one control to run.

4. CancelControlID="lnk_BtnClose" : in panel ther is a cross link buuton to close that popup. give that linkbtn id here

5. PopupControlID="pnl_LoginCommon" : This is most important step.
it is the pael which is to be shown on pop up.
Remember please give a backgroung color to popup .. it is must to show poup window properly
ex:

<asp:Panel ID="pnl_LoginCommon"
runat="server" Height="271px" Width="570px" DefaultButton="lnk_LogIn">
<table style="border: #034a61 5px solid; background-color: white; width: 100%;" cellspacing="0" >
<tr>
......
<tr>
</table>
</asp:Panel>

In Content Page:

1. make 2 hidden feild:- To store user is a login or not , and to what page it has to redirect

<input id="hidLoginId" runat="server" type="hidden" />
<input id="hidRedirect" runat="server" type="hidden" />

adding runat="server" is mandatory

In its code behind's page_load methord write this:

protected void Page_Load(object sender, EventArgs e)
{
if (Session["LoginId"] != null)
{
//user is logined
hidLoginId.Value = Session["LoginId"].ToString();
}
else
{ //user is not logined
hidLoginId.Value = "Unknown";
}
}


2.Create a link button to write comment , download or any thing.
example:
<asp:LinkButton ID="lnkPersAlert"
OnClientClick="javascript:return signInPop()" onClick="lnkPersAlert_on_Click"
runat="server" >
<img src="DownloadMP3.gif"
style="border:none ;width:190px; margin-left:5px;"
title="Download this mp3" />
</asp:LinkButton>

OnClientClick link button calls a given javascript and onClick it postback to server
for given event handler"lnkPersAlert_on_Click"


3.write a javascript : to check if user is login

a. if user login then return true and execut server side function :-
b. if user is not logined then show modal popup of sign (in master page).


function signInPop()
{
//hidden feild stores is user a loginin or not
var hidLogin= document.getElementById('<%=hidLoginId.ClientID%>');

//hidden feild stores page name where to navigate ex: Comments_main.aspx
var hidRedirect = document.getElementById('<%=hidRedirect.ClientID%>');

var IsLogined = hidLogin.value;
// alert('hidLogin ' + hidLogin +'IsLogined '+ IsLogined);

if( IsLogined == "Unknown")
{
//user is not login show popup

$find('ctl00_modalpopSignin').show();
//this 'ctl00_modalpopSignin' is taken by rendering page in IE browser
//and foundthe client id of the modalpopupextender

hidRedirect.value ="Comments_main.aspx";

return false; // do not past back and execute sever side link btn code.
}
else
{ return true; }
}

4. when user either create a login or sign in by that popupmodal extender.

after login or new registartion .. in code behind function of master page.
write code as:

ContentPlaceHolder cph1 = main;
HtmlInputHidden hidContent = (HtmlInputHidden)cph1.FindControl("hidRedirect");

if (hidContent == null)
{
// if no information where to navigate
Response.Redirect("LoginPage2.aspx");

}
else
{
string strRedirect = hidContent.Value;
Response.Redirect(strRedirect);

}

this is to get a hidden feild from content page , get its value and redirect it there..

that's it....
any problem email me: suyash123@gmail.com

No comments:

Post a Comment