Welcome to WebmasterWorld Guest from 54.226.175.101

Forum Moderators: ocean10000

Message Too Old, No Replies

Checkbox State Changed

     
9:05 pm on Jul 20, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 10, 2008
posts: 1130
votes: 0


I have an asp page with a checkbox. I do some checks in a database to see if a user is a regestered member or not. I have a checkbox that also is for is memeber. If the user is a member then it is checked, if they aren't then it isn't checked. What I'm wondering is if there is a way in vb to see if it's state has changed on a button click. I.e if it wen't from unchecked to checked and vice versa. I don't want to do anything if it is in the same state it was when the page loaded. Is this possible? Thanks,
10:02 pm on July 20, 2009 (gmt 0)

New User

10+ Year Member

joined:Dec 11, 2008
posts:11
votes: 0


i know how to do that with javascript, and you could perform an ajax call during an onChange on the list, too. i don't build sites in visual studio, so those are the two methods i would use.
2:41 am on July 21, 2009 (gmt 0)

Administrator

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month

joined:Jan 14, 2004
posts:864
votes: 3


See Sample Code Below. For an Asp.Net server side solution which is triggered only on the checkbox checked/unchecked status has been changed since last postback.


<%@ Page Language="C#" AutoEventWireup="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:CheckBox ID="CheckBox1" runat="server" OnCheckedChanged="CheckBox1_CheckedChanged" />
<br />
<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
<script runat=server language="C#">
void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
//You only want to trigger this if it was a post back
//event (submit Button) that triggered the change.
if (this.IsPostBack == true)
{
if (this.CheckBox1.Checked == true)
{
Label1.Text = "Was previously Unchecked";
}
else
{
Label1.Text = "Was previously Checked";
}
}
}
</script>
7:04 am on July 21, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 1, 2005
posts:732
votes: 0


I prefer to do this completely server-side, comparing the values from the database to that being submitted.

It does require an extra call to the database though, but you could cache the database output on Page Load to reuse on the submit.

[psuedo code]

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

' Load values from the Database

If Not CheckBox1.Checked = Recordset.isAdmin Then
' Checkbox value is not equal to that of the DB
End If

End Sub

4:58 pm on July 21, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 10, 2008
posts: 1130
votes: 0


I ended up using a viewstate variable. I initialize it to false on the page load and then on the checkbox change I set it to true. Then I can check for that on the button click. Here's the code. Thanks to everyone for their help.
Protected Sub memberCheck_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles memberCheck.CheckedChanged
ViewState("checkbox") = True
End Sub