Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 527
  • Last Modified:

Retrieving a checkbox value

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
Starr Duskk
Asked:
Starr Duskk
1 Solution
 
john_steedCommented:
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
 
nisarkhanCommented:
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
 
samtran0331Commented:
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Starr DuskkASP.NET VB.NET DeveloperAuthor Commented:
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
 
Starr DuskkASP.NET VB.NET DeveloperAuthor Commented:
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
 
samtran0331Commented:
>>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
 
Starr DuskkASP.NET VB.NET DeveloperAuthor Commented:
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
 
Starr DuskkASP.NET VB.NET DeveloperAuthor Commented:
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
 
samtran0331Commented:
>>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
 
samtran0331Commented:
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
 
samtran0331Commented:
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
 
Starr DuskkASP.NET VB.NET DeveloperAuthor Commented:
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
 
samtran0331Commented:
>>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
 
Starr DuskkASP.NET VB.NET DeveloperAuthor Commented:
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
 
samtran0331Commented:
...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
 
samtran0331Commented:
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
 
Starr DuskkASP.NET VB.NET DeveloperAuthor Commented:
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
 
Starr DuskkASP.NET VB.NET DeveloperAuthor Commented:
Okay, just got your code. crossed paths, so ignore mine until I get back with you.
0
 
Starr DuskkASP.NET VB.NET DeveloperAuthor Commented:
yep. that works. yay!

thanks so much!
0
 
samtran0331Commented:
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
 
Starr DuskkASP.NET VB.NET DeveloperAuthor Commented:
samtran went above and beyond call of duty on this.
0
 
Starr DuskkASP.NET VB.NET DeveloperAuthor Commented:
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
 
Starr DuskkASP.NET VB.NET DeveloperAuthor Commented:
oops put this on the wrong post. sorry.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now