Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. ASP.NET
  4. Javascript to enable/disable textbox based on checkbox [modified]

Javascript to enable/disable textbox based on checkbox [modified]

Scheduled Pinned Locked Moved ASP.NET
questioncsharpjavascriptasp-netbusiness
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Terick
    wrote on last edited by
    #1

    Hi~ I have a user control with a gridvew that contains a checkbox and textbox. The textbox is set to Enabled=false by default. On Page 1: the Header of the checkbox column is "Remove" and there is a Save button. On this page I need all UNCHECKED rows to have an enabled textbox On Page 2: the Header of the checkbox column is "Select". On this page I need all CHECKED rows to have an enabled textbox and all others to be disabled. How do I create the javascript for these requirements? I am using asp.Net and C#. Here is my sample code: UserControl.ascx:

    Current JavaScript:

    function CheckChanged(cb, tb)
    {
    var chkBox=document.getElementById(cb);
    var txtSize=document.getElemetnById(tb);
    if(chkBox.checked)
    {
    txtSize.disabled=true;
    txtSize.style.backgroundColor=\"lightgray\";
    }
    else
    {
    txtSize.disabled= false;
    }
    }

    This javascript is getting written via a StringBuilder in the .aspx.cs file of each page. Currently, I can only get the script to work properly for Page 2. On Page 1, I need the textbox to always be enabled unless something is checked. What is the best way to enable the textboxes on this page? Thanks!

    modified on Wednesday, February 25, 2009 9:39 AM

    T 1 Reply Last reply
    0
    • T Terick

      Hi~ I have a user control with a gridvew that contains a checkbox and textbox. The textbox is set to Enabled=false by default. On Page 1: the Header of the checkbox column is "Remove" and there is a Save button. On this page I need all UNCHECKED rows to have an enabled textbox On Page 2: the Header of the checkbox column is "Select". On this page I need all CHECKED rows to have an enabled textbox and all others to be disabled. How do I create the javascript for these requirements? I am using asp.Net and C#. Here is my sample code: UserControl.ascx:

      Current JavaScript:

      function CheckChanged(cb, tb)
      {
      var chkBox=document.getElementById(cb);
      var txtSize=document.getElemetnById(tb);
      if(chkBox.checked)
      {
      txtSize.disabled=true;
      txtSize.style.backgroundColor=\"lightgray\";
      }
      else
      {
      txtSize.disabled= false;
      }
      }

      This javascript is getting written via a StringBuilder in the .aspx.cs file of each page. Currently, I can only get the script to work properly for Page 2. On Page 1, I need the textbox to always be enabled unless something is checked. What is the best way to enable the textboxes on this page? Thanks!

      modified on Wednesday, February 25, 2009 9:39 AM

      T Offline
      T Offline
      ToddHileHoffer
      wrote on last edited by
      #2

      Can you use an ajax solution to post back and enable / disable all of the controls in server side code? Otherwise, your best bet is to create a public static string property in server side code such as textBoxIds. On the item databound event of your grid, set textBoxIds += (textbox)row.findcontrol("txtbox").clientId + ","; Then add javascript to iterate your comma separated string on client. var textBoxIds = '<%= this.textBoxIds %>'; var Ids = textBoxIds.split(','); for (var i = 0; i < Ids.length; i++) { var tb = document.getElementById(Ids[i]); if (tb != null) { tb.disabled = true; } } Good luck.

      I didn't get any requirements for the signature

      T 1 Reply Last reply
      0
      • T ToddHileHoffer

        Can you use an ajax solution to post back and enable / disable all of the controls in server side code? Otherwise, your best bet is to create a public static string property in server side code such as textBoxIds. On the item databound event of your grid, set textBoxIds += (textbox)row.findcontrol("txtbox").clientId + ","; Then add javascript to iterate your comma separated string on client. var textBoxIds = '<%= this.textBoxIds %>'; var Ids = textBoxIds.split(','); for (var i = 0; i < Ids.length; i++) { var tb = document.getElementById(Ids[i]); if (tb != null) { tb.disabled = true; } } Good luck.

        I didn't get any requirements for the signature

        T Offline
        T Offline
        Terick
        wrote on last edited by
        #3

        Would the ajax solution be better to use? I am not familiar with ajax so I am not sure how to do this. Thanks.

        T 1 Reply Last reply
        0
        • T Terick

          Would the ajax solution be better to use? I am not familiar with ajax so I am not sure how to do this. Thanks.

          T Offline
          T Offline
          ToddHileHoffer
          wrote on last edited by
          #4

          http://www.asp.net/ajax/[^]

          I didn't get any requirements for the signature

          T 1 Reply Last reply
          0
          • T ToddHileHoffer

            http://www.asp.net/ajax/[^]

            I didn't get any requirements for the signature

            T Offline
            T Offline
            Terick
            wrote on last edited by
            #5

            Thank you all for helping me. Also, ToddHileHoffer, thanks for posting the link.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups