0


I'm trying to turn textbox enable and disable depends on checked checkbox with java script, is there anyway to do this?
here is my code

<script type="text/javascript">
    function ed1(bEnable, textBoxID, textBoxID2) {
        document.getElementById("<%= textBoxID.ClientID %>").disabled = !bEnable
        document.getElementById("<%= textBoxID2.ClientID %>").disabled = !bEnable
    }
</script>

here is checkbox and text box

<asp:CheckBox ID="CH0" runat="server" Font-Names="Tahoma" onclick="ed1(this.checked, 'TB1', 'TB2');" 
            Font-Size="X-Small" Style="font-size: 12px; right: 20px; color: #006699;
            font-family: Tahoma; position: absolute; top: 25px; width: 80px;"
            />
<asp:TextBox ID="TB0" runat="server" EnableViewState="False" Height="12px" MaxLength="2"
            Style="font-size: 11px; right: 110px; vertical-align: middle; color: #0099ff; font-family: tahoma;
            position: absolute; top: 25px; text-align: left; width: 15px; " Enabled="false"
            Wrap="False" ValidationGroup="1234567890"></asp:TextBox>
<asp:TextBox ID="TB1" runat="server" EnableViewState="False" Height="12px" MaxLength="2"
            Style="font-size: 11px; right: 140px; vertical-align: middle; color: #0099ff; font-family: tahoma;
            position: absolute; top: 25px; text-align: left; width: 15px; " Enabled="false"
            Wrap="False" ValidationGroup="1234567890"></asp:TextBox>

it won't work as asp doesn't undrestand what textBoxID means! (it has been declared on javascript!)
I'm getting following error:

BC30451: 'textBoxID' is not declared. It may be inaccessible due to its protection level.

if I put the actual IDs instead of textBoxID2 it will works great but as I have many check box and many text box to enable and disable making countless javascript doesn't seems a good idea!
let me know if this can be solvable and thanks in advance

Erik Schierboom
  • 16,301
  • 10
  • 64
  • 81
Sijav
  • 1,525
  • 17
  • 21
  • 1
    It will not work if you have separate javascript file , but if you have this code in aspx page it will work. – Devesh Jul 22 '13 at 07:41
  • it's on the end of the aspx file before content tag where the same check box and text box exists! but it doesn't still work! EDIT: here is the error BC30451: 'textBoxID' is not declared. It may be inaccessible due to its protection level. – Sijav Jul 22 '13 at 07:42
  • is textboxID and textboxID2 is not the id of the actual asp.net control ? Do you want to loop through all the control and disable it on the fly ? Check this one http://stackoverflow.com/questions/2617629/how-to-get-all-elements-inside-div-that-starts-with-a-known-text – Devesh Jul 22 '13 at 07:50
  • @Devesh Thanks for replay they're server sideID I've added the asp textbox and checkbox code – Sijav Jul 22 '13 at 07:57
  • no answer?! I just have to make so many function? It's the last thing? – Sijav Jul 22 '13 at 08:45

3 Answers3

1

Found another solution by accident, we can't put asp code into qoute but we can put javascript string code into asp code :D :D so it should be like this =>

onclick='<%# "ed1(this.checked, " & TB1.ClientID & ", " & TB2.ClientID & ");" %>' 

also the java script code must be =>

<script type="text/javascript">
    function ed1(bEnable, textBoxID, textBoxID2) {
        document.getElementById(textBoxID).disabled = !bEnable;
        document.getElementById(textBoxID2).disabled = !bEnable;
    }
</script>

Hope this helps someone in future :-)

Sijav
  • 1,525
  • 17
  • 21
0

Change your code

onclick="ed1(this.checked, 'TB1', 'TB2');" 

to

onclick="ed1(this.checked, '<%= TB1.ClientID %>', '<%= TB2.ClientID %>');"

And also modify your JavaScript function

<script type="text/javascript">
    function ed1(bEnable, textBoxID, textBoxID2) {
        document.getElementById(textBoxID).disabled = !bEnable;
        document.getElementById(textBoxID2).disabled = !bEnable;
    }
</script>
Satpal
  • 132,252
  • 13
  • 159
  • 168
  • Thanks for your answer but I already have tried that before, it just won't work, I have checked the client source it will remain as `ed1(this.checked, '<%= TB1.ClientID %>', '<%= TB2.ClientID %>');` so again it doesn't working EDIT: I think it's because of the quotation mark for java script ... anyway to exit from quotation mark, do our code then re enter? – Sijav Jul 22 '13 at 08:19
0

As it seems there was no answer to this
So I'm just doing lot's and lot's of copy paste for create as much function as checkbox exists,
So it may be like this

<script type="text/javascript">
    function ed00(bEnable) {
        document.getElementById("<%= TB000.clientID %>").disabled = !bEnable
    }
</script>

and the checkbox and textbox is

    <asp:CheckBox ID="CH000" runat="server" onclick="ed00(this.checked);"/>
    <asp:TextBox ID="TB000" runat="server"/>

If anyone get to any answer other than this please let me and others know, thanks

Sijav
  • 1,525
  • 17
  • 21