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");
}
}
2条答案
按热度按时间nwo49xxi1#
一种方法是在web.config文件中添加一个authorization部分,当用户单击UserList链接而用户未登录时,该部分将跳转登录页面:
如果这对您的应用有意义,您甚至可以使用
<asp:LoginView>
控件对未登录用户隐藏Userlist链接。js81xvg62#
试试这个:
在主页的HTML中:
为LinkButtonLogin的onclick事件定义事件处理程序:
创建一个隐藏在视图中的虚拟按钮。然后,对于ModalPopupExtender,将ModalPopupExtender控件的TargetControlID从LinkButtonLogin更改为ButtonInvisible。这实际上将ModalPopupExtender伪装成可在代码中隐藏/显示。
在母版页的代码隐藏中: