UserControl Events

Discussion in 'ASP .Net Building Controls' started by Cenon Del Rosario, Sep 2, 2003.

  1. Hello,

    I was wondering if someone can tell me what's wrong with this code.
    Basically, I plan to have a UserControl w/c for this example is a User Login
    form. I will have an event called OnLogin which will be called when the
    login has been authenticated. In the demo I have included here, the control
    tries to raise the event when the Submit button is pressed. Unfortunately,
    is does not work when I wire up the event via the HTML page. It does however
    work when I wire up the event in code.

    Can someone help me fix this. Thank you in advance.

    Here is the code...

    THE USERCONTROL:

    <%@ Control Language="C#" AutoEventWireup=True %>

    <script runat="server">

    private String m_BackColor = "White";

    public event EventHandler OnLogin;

    private void HandleLogin(Object obj, EventArgs e)
    {
    if ((IsPostBack) && (OnLogin != null))
    {
    OnLogin(obj, e);
    }
    }

    public String BackColor
    {
    get
    {
    return m_BackColor;
    }
    set
    {
    m_BackColor = value;
    }
    }

    public String UserId
    {
    get
    {
    return txtUID.Text;
    }
    set
    {
    txtUID.Text = value;
    }
    }

    public String Password
    {
    get
    {
    return txtPWD.Text;
    }
    set
    {
    txtPWD.Text = value;
    }
    }

    void Submit_Click(Object obj, EventArgs e)
    {
    lblMsg.Text = "UserName: <b>" + txtUID.Text + "</b><br>" +
    "Password: <b>" + txtPWD.Text + "</b><br>";
    HandleLogin(this, EventArgs.Empty);
    }

    </script>

    <table style="background-color:<%= BackColor %>;
    font: 10pt verdana;border-width:1;
    border-style:solid;border-color:Black;"
    cellspacing=15>
    <tr>
    <td><b>Login: </b></td>
    <td><asp:Textbox runat="server" id="txtUID" /></td>
    </tr>
    <tr>
    <td><b>Passowrd: </b></td>
    <td><asp:Textbox runat="server" id="txtPWD" TextMode="Password"/></td>
    </tr>
    <td>&nbsp;</td>
    <td>
    <asp:Button runat="server" id="btnLogin" Text="Login"
    OnClick="Submit_Click"/>
    </td>
    </tr>
    </table>
    <p>
    <asp:Label runat="server" id="lblMsg" />

    THE TEST PAGE:

    <%@ Page Language="C#" %>
    <%@ Register TagPrefix="LGF" TagName="LoginForm" src="LoginForm.ascx" %>

    <script runat="server">

    void Page_Load(Object obj, EventArgs e)
    {
    lgfLogin.BackColor = "Silver";
    //lgfLogin.OnLogin += new EventHandler(Frm2_Login);
    }

    void Frm1_Login(Object obj, EventArgs e)
    {
    lblMsg1.Text = "Auto Wireup Event: Logged in at " +
    DateTime.Now.ToString();
    }

    void Frm2_Login(Object obj, EventArgs e)
    {
    lblMsg2.Text = "Manual Wireup Event: Logged in at " +
    DateTime.Now.ToString();
    }

    </script>

    <html>
    <body>
    <form runat="server">
    <lgf:LoginForm runat="server" id="lgfLogin" OnLogin="Frm1_Login" />
    <p>
    <asp:Label runat="server" id="lblMsg1" Text="Not Logged In." />
    <p>
    <asp:Label runat="server" id="lblMsg2" Text="Not Logged In." />
    </form>
    </body>
    </html>
     
    Cenon Del Rosario, Sep 2, 2003
    #1
    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.