Accessing Login Control's Button from Code-Behind

Discussion in 'ASP .Net' started by Jeff Lynch, Feb 24, 2006.

  1. Jeff Lynch

    Jeff Lynch Guest

    I'd like to add the following attributes to the Login control's Login Button
    to create a CSS rollover effect. How can I access the control's login button
    from the page's code-behind?

    LoginButton.Attributes.Add("onmouseover", "this.className =
    'buttonsmallover'");
    LoginButton.Attributes.Add("onmouseout", "this.className = 'buttonsmall'");
     
    Jeff Lynch, Feb 24, 2006
    #1
    1. Advertisements

  2. Hi Jeff,

    Welcome to the MSDN newsgroup.

    From your description, I understand you have an ASP.NET 2.0 page which uses
    the "LoginControl" and you're wondering how to get the reference to the
    login button on the "LoginControl" so as to do some
    customization(registering some client scripts), correct?

    Based on my research, the "LoginControl" doesn't provide buildin property
    for accessing the inner login button control. However, since the login
    button is created as one child control in the "LoginControl"'s composite
    control hierarchy, we can consider using the "Control.FindControl" to get
    the reference of the login button. Form the client-side html source of a
    test page containing login control, I find that the login button's id is
    always assigned with "LoginButton" and login button's direct parent
    NamingContainer is just the "LoginControl". Therefore, we can use the
    "LoginButton" as the control id to call the "FindControl" method. For
    example:

    ===============================
    protected void Page_Load(object sender, EventArgs e)
    {
    Control ctrl = Login1.FindControl("LoginButton");

    if (ctrl != null)
    {
    Button btn = (Button)ctrl;
    btn.Attributes.Add("onmouseout", "alert(this.name);");

    }
    else
    {
    Response.Write("<br/>Null");
    }

    }

    ===============================

    BTW, for such question on how to get a nested child control's reference in
    a certain composite control, the ASP.NET web page's output trace is a good
    helper. We can turn on the page's trace in the @Page directive so that the
    runtime will printout the page's control hierarchy at the bottom of the
    page's html content. e.g:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="login.aspx.cs"
    Inherits="login" Trace="true" %>

    This can greatly ease our work on inspecting the page's control structure.

    Hope this helps.

    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
     
    Steven Cheng[MSFT], Feb 27, 2006
    #2
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.