我有这个问题.. 我在一个母版页上有一个“登录”链接按钮和一个“用户列表”链接按钮。当用户登录并单击“UserList”链接按钮时,将打开具有上述母版页的 UserList 页面。(我已经实现了)。
但是如果用户没有登录并且他点击了“UserList”,则应该调用“Login”链接按钮的点击。我怎样才能做到这一点?请帮忙..
我有这个问题.. 我在一个母版页上有一个“登录”链接按钮和一个“用户列表”链接按钮。当用户登录并单击“UserList”链接按钮时,将打开具有上述母版页的 UserList 页面。(我已经实现了)。
但是如果用户没有登录并且他点击了“UserList”,则应该调用“Login”链接按钮的点击。我怎样才能做到这一点?请帮忙..
尝试这个:
在MasterPage的 HTML 中:
为LinkButtonLogin的 onclick 事件定义一个事件处理程序:
<asp:linkbutton id="LinkButtonLogin" runat="server"
text="Login" onclick="LinkButtonLogin_Click"></asp:linkbutton>
创建一个从视图中隐藏的虚拟按钮。然后,对于ModalPopupExtender,将ModalPopupExtender控件的TargetControlID从LinkBut tonLogin 更改为ButtonInvisible。这有效地将ModalPopupExtender伪装成可从您的代码中隐藏/显示。
<asp:button id="ButtonInvisible" runat="server" style="display: none;" />
在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");
}
}
一种方法是在您的 web.config 文件中添加一个授权部分,当用户单击 UserList 链接并且用户未登录时,该部分将触发登录页面:
<location path="UserList.aspx">
<system.web>
<authorization>
<deny users="?"/>
</authorization>
</system.web>
</location>
如果对您的应用有意义,您甚至可以使用<asp:LoginView>
控件对未登录用户隐藏用户列表链接。