views:

1004

answers:

2

Hi everybody,

I have this problem .. I have one "Login" linkbutton and one "UserList" linkbutton on one masterpage. When the user is logged in, and he clicks "UserList" linkbutton, the UserList Page which has the masterpage mentioned above, opens.(This i have achieved).

but if the user is not logged in and he clicks "UserList", the "Login" linkbutton's click should be called. how can i achieve this? Please Help..

A: 

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");
    }
}
Tim S. Van Haren
Please tell me how can i do this LinkButtonLoginAndUserList_Click(object sender, EventArgs e)..i m new to asp.net..
i think ur solution is working.. but just tell me wat to do according to this:
my problem is something else. let me explain. for login i have used Ajax Modal Popup Extender. when i click on Login, a small login screen pops up. the code is:
<cc1:ModalPopupExtender ID="ModalPopupExtender" runat="server" TargetControlID="LinkButton1" PopupControlID="RoundedPanel1" BackgroundCssClass="modalBackground" CancelControlID="btnCancel" Drag="True" /> </cc2:RoundedPanel> where LinkButton1 is Login button. now i want to show this screen again when the user clicks on "User List" but has not logged in. now can anyone tell me what i shud do? and i dont want to hide the UserList Button because it goes along with some othe buttons which would make it improper. that is why i m using this way..
I edited my answer above to reflect the fact that you are using a ModalPopupExtender control. I hope this helps guide you in the right direction.
Tim S. Van Haren
THANKS a lot... your solution was really helpful. but now there is a new problem. there are changes in the application and now the Login button is the only linkbutton on the page.. the "UserList" is included in the Menu and its now a hyperlink.. now what should i do?
Does the UserList page use this master page that we've discussing?
Tim S. Van Haren
yes.. the userlist page also has the same masterpage.
I updated my answer to reflect that the UserList control is now a HyperLink instead of a LinkButton.
Tim S. Van Haren
HyperLinkUserList is not exactly a hyperlink.. i havent kept it from the toolbox. i have used a menu.. and UserList is a menu item. so its a hyperlink that way. please tell me an alternate method for HyperLinkUserList.Attributes.Add("onclick", "document.getElementById('" + LinkButtonLogin.ClientID + "').click();");
A: 

One way to do it is to add an authorization section in your web.config file that will trip the login page when the user clicks the UserList link and the user is not logged in:

<location path="UserList.aspx">
 <system.web>
  <authorization>
   <deny users="?"/>
  </authorization>
 </system.web>
</location>

If if makes sense for your app, you can even hide the Userlist link from non-logged in users by using the <asp:LoginView> control.

CharlieG
my problem is something else. let me explain.for login i have used Ajax Modal Popup Extender. when i click on Login, a small login screen pops up. the code is:<cc1:ModalPopupExtender ID="ModalPopupExtender" runat="server" TargetControlID="LinkButton1" PopupControlID="RoundedPanel1" BackgroundCssClass="modalBackground" CancelControlID="btnCancel" Drag="True" /> </cc2:RoundedPanel>where LinkButton1 is Login button. now i want to show this screen again when the user clicks on "User List" but has not logged in.
now can anyone tell me what i shud do?
and i dont want to hide the UserList Button because it goes along with some othe buttons which would make it improper. that is why i m using this way..