Solved

Retrieving a checkbox value

Posted on 2007-11-20
23
508 Views
Last Modified: 2012-08-14
I am using the LoggingIn event with my log in control.

Within this event I am trying to get the value of the RememberMe checkbox in VB.

I use the following code...

        Dim rememberme As CheckBox = CType(FindControl("RememberMe"), CheckBox)

I tried both of these and the first doesn't error, but also doesn't hit the code when the checkbox is checked or even if it isn't:

        If Not rememberme Is Nothing Then

This second one errors with: "System.NullReferenceException: Object reference not set to an instance of an object."

            If (rememberme.Checked) Then

How do I check to see if the checkbox is checked?

thanks.
0
Comment
Question by:BobCSD
23 Comments
 
LVL 3

Expert Comment

by:john_steed
Comment Utility
Hi,

Does the following returns nothing ?
Dim rememberme As CheckBox = CType(FindControl("RememberMe"), CheckBox)

Make sure you add the name of the panel before the "FindControl"
If you find the control (if it doesn't return nothing) you can use the property checked like in your example

Hope this helps
0
 
LVL 7

Expert Comment

by:nisarkhan
Comment Utility
where is that checkbox ? is that inside gridview ?
if yes then

Protected Sub editGrid_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)

Dim chkRememberMe As CheckBox = TryCast(e.Row.FindControl("RememberMe"), CheckBox)

End Sub
0
 
LVL 37

Expert Comment

by:samtran0331
Comment Utility
Bob,

Have you tried putting the login control name before the findcontrol?


Dim rememberme As CheckBox = CType(YourLoginControlName.FindControl("RememberMe"), CheckBox)

Open in new window

0
 
LVL 1

Author Comment

by:BobCSD
Comment Utility
No, it's not in a gridview.

And yes, from another task, I've tried Login1.Findcontrol... etc. and it doesn't work.

It returns nothing.
0
 
LVL 1

Author Comment

by:BobCSD
Comment Utility
This is what I have tried:

        Dim rememberme As CheckBox = CType(FindControl("Me.Login1.RememberMe"), CheckBox)
        Dim rememberme As CheckBox = CType(FindControl("Login1.RememberMe"), CheckBox)
        Dim rememberme As CheckBox = CType(FindControl("RememberMe"), CheckBox)
 
0
 
LVL 37

Expert Comment

by:samtran0331
Comment Utility
>>And yes, from another task, I've tried Login1.Findcontrol... etc. and it doesn't work.

hmmm...quite odd...the below works for me

aspx:
    <form id="form1" runat="server">
    <div>
        <asp:Login ID="Login1" runat="server">
        </asp:Login>
    </div>
    </form>

    Protected Sub Login1_LoggingIn(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LoginCancelEventArgs) Handles Login1.LoggingIn

        Dim chk As CheckBox = CType(Login1.FindControl("RememberMe"), CheckBox)

        If chk.Checked Then

            Response.Write("Forget Me Not.")

        Else

            Response.Write("Forget Me.")

        End If

    End Sub

Open in new window

0
 
LVL 1

Author Comment

by:BobCSD
Comment Utility
I did have the control setup an editable template so I could change wording, etc. within the control.

but everything should still be called the same i'd think:

                <asp:Login ID="Login1" runat="server"
                    OnLoggingIn="OnLoggingIn" ...

                                               <asp:CheckBox ID="RememberMe" runat="server" Text="Check this box to keep you logged in on this computer." />

your function has a:
 Handles Login1.LoggingIn

but if I have that in mine, it errors about the Handles clause.

I also have tried this with and without the:
AutoEventWireup="false"
in the @ Page section.


 
0
 
LVL 1

Author Comment

by:BobCSD
Comment Utility
If I add your example function to my code, I get this error:

Compiler Error Message: BC30506: Handles clause requires a WithEvents variable defined in the containing type or one of its base types.

Source Error:


Line 5:      Inherits System.Web.UI.Page
Line 6:  
Line 7:      Protected Sub Login1_LoggingIn(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LoginCancelEventArgs) Handles Login1.LoggingIn
Line 8:          Dim chk As CheckBox = CType(Me.Login1.FindControl("RememberMe"), CheckBox)
Line 9:          If chk.Checked Then
 

---
Evidentally, having it broken into a template gets rid of the events in the control and I have to manually add them.

I do have: OnLoggingIn="OnLoggingIn"  in my original, and it is hitting it. But somehow I need to perhaps have a "WithEvents" variable to recognize the Login1?

Do you know what that is?

0
 
LVL 37

Expert Comment

by:samtran0331
Comment Utility
>>Do you know what that is?

When you use codebehind/web application model, the events are wired to the aspx control using the "Handles"

As to the original question, let me convert my test to a template and see what happens.
0
 
LVL 37

Expert Comment

by:samtran0331
Comment Utility
hmmm...converted to inline vb and converted the login to template and it still works....
<%@ Page Language="VB" %>
 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 

<script runat="server">
 

    Protected Sub Login1_LoggingIn(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LoginCancelEventArgs)

        Dim chk As CheckBox = CType(Login1.FindControl("RememberMe"), CheckBox)

        If chk.Checked Then

            Response.Write("Forget Me Not.")

        Else

            Response.Write("Forget Me.")

        End If

    End Sub
 

    Protected Sub Login1_Authenticate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.AuthenticateEventArgs)
 

    End Sub

</script>
 

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>Untitled Page</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <asp:Login ID="Login1" runat="server" OnLoggingIn="Login1_LoggingIn" OnAuthenticate="Login1_Authenticate">

            <LayoutTemplate>

                <table border="0" cellpadding="1" cellspacing="0" style="border-collapse: collapse">

                    <tr>

                        <td>

                            <table border="0" cellpadding="0">

                                <tr>

                                    <td align="center" colspan="2">

                                        Log In</td>

                                </tr>

                                <tr>

                                    <td align="right">

                                        <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">User Name:</asp:Label></td>

                                    <td>

                                        <asp:TextBox ID="UserName" runat="server"></asp:TextBox>

                                        <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName"

                                            ErrorMessage="User Name is required." ToolTip="User Name is required." ValidationGroup="Login1">*</asp:RequiredFieldValidator>

                                    </td>

                                </tr>

                                <tr>

                                    <td align="right">

                                        <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label></td>

                                    <td>

                                        <asp:TextBox ID="Password" runat="server" TextMode="Password"></asp:TextBox>

                                        <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password"

                                            ErrorMessage="Password is required." ToolTip="Password is required." ValidationGroup="Login1">*</asp:RequiredFieldValidator>

                                    </td>

                                </tr>

                                <tr>

                                    <td colspan="2">

                                        <asp:CheckBox ID="RememberMe" runat="server" Text="Remember me next time." />

                                    </td>

                                </tr>

                                <tr>

                                    <td align="center" colspan="2" style="color: red">

                                        <asp:Literal ID="FailureText" runat="server" EnableViewState="False"></asp:Literal>

                                    </td>

                                </tr>

                                <tr>

                                    <td align="right" colspan="2">

                                        <asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="Log In" ValidationGroup="Login1" />

                                    </td>

                                </tr>

                            </table>

                        </td>

                    </tr>

                </table>

            </LayoutTemplate>

        </asp:Login>

    </div>

    </form>

</body>

</html>

Open in new window

0
 
LVL 37

Expert Comment

by:samtran0331
Comment Utility
Also, since I now have 2 test pages for this, one as inline and one as codebehind, I can illustrate the need for "Handles"...

If you use codebehind, your aspx page directive should look something like this:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="LoginControl.aspx.vb" Inherits="LoginControl" %>

and the control itself on the aspx doesn't have any events wired up to it on the aspx:
 <asp:Login ID="Login1" runat="server">

and when you wire up events, Visual Studio will add the "Handles" clause to controls like this:
Protected Sub Login1_LoggingIn(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LoginCancelEventArgs) Handles Login1.LoggingIn


if you use the web site model, events are wired up differently.
the page directive is simply:
<%@ Page Language="VB" %>


the control has the events wired up in the aspx tag:
<asp:Login ID="Login1" runat="server" OnLoggingIn="Login1_LoggingIn" OnAuthenticate="Login1_Authenticate">

and the actual code events don't have the "Handles" part:
Protected Sub Login1_LoggingIn(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LoginCancelEventArgs)



It's just 2 different models of programming...
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Author Comment

by:BobCSD
Comment Utility
Thanks for all your help. I don't know what is is with that page.

But I did what you did and created a new page with a custom control from scratch, broke it into a new template. Then I manually began adding things one at a time.

So far it hasn't broken. It is now identical to the other page, but the other one is broken.

I do remember having changed the name of the file and the code behind class behind it, and I wonder if that has anything to do with it. Either way, rebuilding it as a new page seems to work fine.

0
 
LVL 37

Expert Comment

by:samtran0331
Comment Utility
>>I do remember having changed the name of the file and the code behind class behind it, and I wonder if that has anything to do with it.

It's possible, but as long as the page directive on the aspx page matches the codebehind filename and the class name, it shouldn't be a problem...

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="LoginControl.aspx.vb" Inherits="LoginControl" %>

One other possible cause is that the code/page is getting pulled from the "server" cache.
Regarless if you use IIS or the internal VS web server, it all gets dumped into this directory:
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files

If you notice "unexplainable oddities", or changes in code not reflected in testing, go to that folder and delete everything in it...you might have to either restart IIS or close VS/VWD before it lets you delete everything in there...
0
 
LVL 1

Author Comment

by:BobCSD
Comment Utility
Okay, I "broke" it again.... Here is what I did. I put a LoginView control around it... Please try that on yours? thanks.

        <asp:LoginView ID="LoginView1" runat="server">
            <LoggedInTemplate>
                You are logged in as
                <asp:LoginName ID="LoginName1" runat="server" />
                <br />
            </LoggedInTemplate>
            <AnonymousTemplate>

LoginControl Goes here....

            </AnonymousTemplate>
        </asp:LoginView>    
0
 
LVL 37

Expert Comment

by:samtran0331
Comment Utility
...your last post explains it!
since it's nested inside the loginview, the logincontrol is in a different part of the control tree...
will take me a bit to set up the test and test it...but it should be something along the lines of:

Dim lctrl as logincontrol
lctrl = ctype(loginview1.findcontrol("login1"),logincontrol)

dim chk as checkbox
chk = ctype(lctrl.findcontrol("RememberMe"),checkbox)

...basically you have to find the logincontrol inside the loginview before you can find the checkbox inside of the logincontrol...
0
 
LVL 37

Accepted Solution

by:
samtran0331 earned 500 total points
Comment Utility
yup...that was it (I hope)...I used the logged in template just for the demo, but try this:

aspx:
        <asp:LoginView ID="LoginView1" runat="server">
        <LoggedInTemplate>Logged in
        <asp:Login ID="Login1" runat="server" OnLoggingIn="Login1_LoggingIn" OnAuthenticate="Login1_Authenticate">
            <LayoutTemplate>
                <table border="0" cellpadding="1" cellspacing="0" style="border-collapse: collapse">
                    <tr>
                        <td>
                            <table border="0" cellpadding="0">
                                <tr>
                                    <td align="center" colspan="2">
                                        Log In</td>
                                </tr>
                                <tr>
                                    <td align="right">
                                        <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">User Name:</asp:Label></td>
                                    <td>
                                        <asp:TextBox ID="UserName" runat="server"></asp:TextBox>
                                        <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName"
                                            ErrorMessage="User Name is required." ToolTip="User Name is required." ValidationGroup="Login1">*</asp:RequiredFieldValidator>
                                    </td>
                                </tr>
                                <tr>
                                    <td align="right">
                                        <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label></td>
                                    <td>
                                        <asp:TextBox ID="Password" runat="server" TextMode="Password"></asp:TextBox>
                                        <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password"
                                            ErrorMessage="Password is required." ToolTip="Password is required." ValidationGroup="Login1">*</asp:RequiredFieldValidator>
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan="2">
                                        <asp:CheckBox ID="RememberMe" runat="server" Text="Remember me next time." />
                                    </td>
                                </tr>
                                <tr>
                                    <td align="center" colspan="2" style="color: red">
                                        <asp:Literal ID="FailureText" runat="server" EnableViewState="False"></asp:Literal>
                                    </td>
                                </tr>
                                <tr>
                                    <td align="right" colspan="2">
                                        <asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="Log In" ValidationGroup="Login1" />
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>
            </LayoutTemplate>
        </asp:Login>
       
        </LoggedInTemplate>
        <AnonymousTemplate>
       
        </AnonymousTemplate>
        </asp:LoginView>

    Protected Sub Login1_LoggingIn(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LoginCancelEventArgs)

        Dim lctrl As Login

        lctrl = CType(LoginView1.FindControl("Login1"), Login)

        

        Dim chk As CheckBox = CType(lctrl.FindControl("RememberMe"), CheckBox)

        If chk.Checked Then

            Response.Write("Forget Me Not.")

        Else

            Response.Write("Forget Me.")

        End If

    End Sub

Open in new window

0
 
LVL 1

Author Comment

by:BobCSD
Comment Utility
You're certainly earning your points on this one!

Well, LoginView is a valid name, whereas logincontrol wasn't. so I tried it as below...

Protected Sub Login1_LoggingIn(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LoginCancelEventArgs) Handles Login1.LoggingIn

        Dim lctrl As LoginView
        lctrl = CType(LoginView1.FindControl("login1"), LoginView)

It still gives me this error so the Handles thing is still choking:
BC30506: Handles clause requires a WithEvents variable defined in the containing type or one of its base types.
Protected Sub Login1_LoggingIn(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LoginCancelEventArgs) Handles Login1.LoggingIn
0
 
LVL 1

Author Comment

by:BobCSD
Comment Utility
Okay, just got your code. crossed paths, so ignore mine until I get back with you.
0
 
LVL 1

Author Comment

by:BobCSD
Comment Utility
yep. that works. yay!

thanks so much!
0
 
LVL 37

Expert Comment

by:samtran0331
Comment Utility
Using my last example, you shouldn't have an issue with the "Handles"...keep in mind, withouth the "Handles", you need to have the event wired up in the control:

<asp:Login ID="Login1" runat="server" ***OnLoggingIn="Login1_LoggingIn"***
0
 
LVL 1

Author Closing Comment

by:BobCSD
Comment Utility
samtran went above and beyond call of duty on this.
0
 
LVL 1

Author Comment

by:BobCSD
Comment Utility
in short, you have to find the control within the control...
  Protected Sub Login1_LoggingIn(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LoginCancelEventArgs)
        Dim lctrl As Login
        lctrl = CType(LoginView1.FindControl("Login1"), Login)
       
 
but overall, your original answer was accurate,I just had more problems than that.
0
 
LVL 1

Author Comment

by:BobCSD
Comment Utility
oops put this on the wrong post. sorry.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now