Try this:
In the HTML of the MasterPage:
Define an event handler for LinkButtonLogin's onclick event:
<asp:linkbutton id="LinkButtonLogin" runat="server"
text="Login" onclick="LinkButtonLogin_Click"></asp:linkbutton>
Create a dummy button that is hidden from view. Then, for the ModalPopupExtender, change the TargetControlID of the ModalPopupExtender control from LinkButtonLogin to ButtonInvisible. This effectively fakes the ModalPopupExtender into being hideable/showable from your code.
<asp:button id="ButtonInvisible" runat="server" style="display: none;" />
In the codebehind of the MasterPage:
protected void Page_Load(object sender, EventArgs e)
{
/*
This adds a client-side event to your HyperLink control that mimics
LinkButtonLogin's onclick event, but ONLY if the current user is not
logged in.
*/
if (!UserIsLoggedIn())
{
HyperLinkUserList.Attributes.Add("onclick",
"document.getElementById('" +
LinkButtonLogin.ClientID + "').click();");
}
}
protected void LinkButtonLogin_Click(object sender, EventArgs e)
{
// check if the user is logged in.
if (!UserIsLoggedIn())
{
// show the modal login window
ModalPopupExtender.Show();
}
else
{
/*
This assumes that you always want a user to
go to the UserList page upon being logged in.
You can add some code here to redirect to
different pages based on certain criteria.
*/
Response.Redirect("userlist.aspx");
}
}