Javascript to enable/disable textbox based on checkbox [modified]
-
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
-
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
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
-
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
-
Would the ajax solution be better to use? I am not familiar with ajax so I am not sure how to do this. Thanks.
I didn't get any requirements for the signature
-
I didn't get any requirements for the signature