1

I want to Enable Disable my textbox because either user can enter the call preparation time in the text box or it can select the checkbox for unlimited time.

My aspx code

<table width="100%" cellpadding="0" cellspacing="0" border="0" align="center">
    <tr class="rowPadding">
        <td class="labelStyle" width="240px">
            <asp:Label runat="server" ID="lblPrepTime" Width="230px" meta:resourcekey="lblPrepTimeResource1"> Call preparation time (seconds): </asp:Label>
        </td>
        <td class="textBoxStyle" width="100px">
            <asp:TextBox runat="server" ID="txtPrepTime" Width="80px" meta:resourcekey="txtPrepTimeResource1"></asp:TextBox>
        </td>
        <td class="labelPaddingRight" width="300px">
            <asp:CheckBox runat="server" ID="cbUnlimited" Text="unlimited" meta:resourcekey="cbUnlimitedResource1" />
        </td>
    </tr>
</table>

my code behind is

protected void Page_Load(object sender, EventArgs e)
{
   try
   {
      if (!IsPostBack)
      {
          BindAllCallSettings();
      }
   }
   catch (Exception ex)
   {
         Logger.WriteException(ex);
   }
}
2
  • 1
    well you can do it by using scripts Commented Jan 30, 2014 at 12:04
  • Thanks, but problem not solved properly. It only enable disable until page is not refreshed. I want to store these values in the db. So if textbox is disabled then it should be disabled on page load Commented Jan 30, 2014 at 12:13

2 Answers 2

2

Add this method in your client side code..

<script type="text/javascript">
    function EnableDisableCheckBox() {
        if (document.getElementById('<%=cbUnlimited.ClientID%>').checked) {
            document.getElementById('<%=txtPrepTime.ClientID%>').disabled = true;
        }
        else {
            document.getElementById('<%=txtPrepTime.ClientID%>').disabled = false;
        }
    }
</script>

then replace your onclick event with the given below,

<table width="100%" cellpadding="0" cellspacing="0" border="0" align="center">
    <tr class="rowPadding">
        <td class="labelStyle" width="240px">
            <asp:Label runat="server" ID="lblPrepTime" Width="230px" meta:resourcekey="lblPrepTimeResource1"> Call preparation time (seconds): </asp:Label>
        </td>
        <td class="textBoxStyle" width="100px">
            <asp:TextBox runat="server" ID="txtPrepTime" Width="80px" meta:resourcekey="txtPrepTimeResource1"></asp:TextBox>
        </td>
        <td class="labelPaddingRight" width="300px" onclick="EnableDisableCheckBox()">
            <asp:CheckBox runat="server" ID="cbUnlimited" Text="unlimited" meta:resourcekey="cbUnlimitedResource1" />
        </td>
    </tr>
</table>

in your code behind use this, Now whenever the page is refreshed and you bind your data, it will save its state on page load too...

protected void Page_Load(object sender, EventArgs e)
{
    try
    {
        if (!IsPostBack)
        {
            BindAllCallSettings();
        }
        this.txtPrepTime.Enabled = !(this.cbUnlimited.Checked);
    }
    catch (Exception ex)
    {
        Logger.WriteException(ex);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

use javascript:

<asp:CheckBox runat="server" ID="cbUnlimited" Text="unlimited" meta:resourcekey="cbUnlimitedResource1" onchange="myfn()"/>

<script type="text/javascript">
    function myfn(){
    var val=document.getElementById("txtPrepTime");`
    if(this.val==checked)
    {
        val.style.visibility=true;//or false as you want
    } 
    else 
    {
        val.style.visibility=true;//or false as you want  
    }
}
</script>

1 Comment

as per myfn() is concerned, I corrected it and the matter of fact is, 3 people liked it, because they understood what I wanted to say. Any way, my mistake. You can also like this if you think I explained properly why I did not check the checkbox. It it because the onclick() is firing on checkbox itself, and this indicates current object i.e check box. this.value will be the current state of check box. Hope you got it :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.