Nițu Alexandru
Nițu Alexandru

Reputation: 714

Custom Asp.Net CheckBox

I want to create a custom checkbox (a regular checkbox but with one more property: a boolean LastValue) in Asp.Net C#, so I can know if the value was changed after a post back. What is the best approach you suggest?

Thank you!

Upvotes: 0

Views: 2196

Answers (2)

fnostro
fnostro

Reputation: 4591

Create a new class that is derived from CheckBox and add a public property LastValue and be sure to save LastValue into the ViewState

From the default .ascx file, modified for class name and LastValue property

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class MyCheckBox : CheckBox
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public string LastValue
        {
            get {
                object s = ViewState["LastValue"];
                return ( (s == null) ? "" : (string) ViewState["LastValue"] ) ;
            }
            set { ViewState["LastValue"] = value; }
        }
    }
}

The @Page at the top of the default .ascx markup:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="WebApplication1.MyCheckBox" %>

A default aspx page with markup containing the user control. After building the webApp the control is automatically place in a generic ToolBox, on my system the toolbox is called "WebApplication1 Components". Dropping the control onto a default page created the following:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<%@ Register assembly="WebApplication1" namespace="WebApplication1" tagprefix="cc1" %>

<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

      <cc1:MyCheckBox ID="MyCheckBox1" runat="server" />

    </div>
    </form>
</body>
</html>

One last thing: this is for simplicity. If you'll notice on most, if not all, asp.net web controls there is the ability to disable the view state. Most of the time it's left on, but the point is that it's up to the programmer. ViewState to save a custom control property will work only so long as viewstate is enabled. Disable it and say goodbye to LastValue. A better (proper?) way would be to override the Save/LoadControlState() methods, but that's more involved so I'll leave it to you to determine how best to implement your requirements.

Upvotes: 2

IanPudney
IanPudney

Reputation: 6041

Aassuming you're working with HTML forms, you will want create a hidden field in the form with a similar name, populate it with your lastValue, and check the checkbox against that.

<input type="hidden" name="checkboxLastValue" value="true">

Upvotes: 0

Related Questions