[RSS]

Cambiando el Estado de un CheckBox

Algunas veces deseamos controlar el estado de un checkbox o cambiarlo segùn determinadas condiciones.

Pasos:

1. Creamos un proyecto Web.

2. En el diseñador agregamos un checkbox y dos botones.

  • Dar click derecho en el checkbox y luego seleccionar Add Binding Attribute, para agregar los atributos al checkbox, de manera que los podamos usar en nuestro código.

Generando automáticamente

 private Checkbox checkbox1 = new Checkbox();

    public Checkbox getCheckbox1() {
        return checkbox1;
    }

    public void setCheckbox1(Checkbox c) {
        this.checkbox1 = c;
    }

3.Damos click derecho en el botón Habilitar, y seleccionamos Edit Action Event Handler.

A continuación, agregamos el código: this.checkbox1.setSelected(true);, el método setSelected con valor true, marca el checkbox como seleccionado, y un valor de false, quita la marca.

public String button1_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        this.checkbox1.setSelected(true);
        return null;
    }

4. Agregar el código al botón deshabilitar

public String button2_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        this.checkbox1.setSelected(false);
        return null;
    }
  • Aqui se muestra la ejecución al hacer click en el botón Habilitar
  • Al hacer click en el botón Deshabilitar, se quita la selección en el checkbox.
Regresar

Attachments

action.png Info on action.png 12888 bytes
binding.png Info on binding.png 36214 bytes
boton1.png Info on boton1.png 7041 bytes
boton2.png Info on boton2.png 6538 bytes
page1.png Info on page1.png 28600 bytes