0

The controls on aspx page are like this Submitted Submission Date

I want, if the check box is checked, then the textbox will be enabled I wrote

  if(chkSubmitted.Checked)
        {
            txtSubmissionDate.Enabled = true;
        }

in the page load event. But when the page is loaded this checkbox having no effect on. Whats going wrong?

3
  • 1
    Can you post your page_load code? Remember you need to check for postback? Commented Jul 18, 2011 at 15:31
  • Do you have viewstate enabled on the chkSubmitted control? Commented Jul 18, 2011 at 15:31
  • That code should be in the Page_PreRender event. Commented Jul 18, 2011 at 15:33

4 Answers 4

1

If you want the action of clicking the checkbox to enable the textbox, you'll need to do a postback when the box is checked by setting AutoPostBack="True":

<asp:CheckBox runat="server" ID="chkSubmitted" AutoPostBack="True" />

Or, you could use JavaScript:

<asp:CheckBox runat="server" ID="chkSubmitted" onclick="setSubmissionDateEnabled()" />

function setSubmissionDateEnabled()
{
    var chkSubmitted = document.getElementById("<%= chkSubmitted.ClientID %>");
    var txtSubmissionDate = document.getElementById("<%= txtSubmissionDate.ClientID %>");
    txtSubmissionDate.disabled = !chkSubmitted.checked;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thnx you for the answer, but now I am having a new prob, I have placed the controls inside an update panel. But, there are other Update panel also, when this(txtbox enabled) is being updated, other ajax control is also updated which I don want, can you tell me how to solve this, or I shuold place this as new problem.
Yes, please post this as a new question.
1

First set autopostback property to true of checkbox

write following code in checkbox_Selectedindexchanged event

if(chkSubmitted.Checked)
{             
txtSubmissionDate.Enabled = true;  
} 
else
{
txtSubmissionDate.Enabled = false;
}

Comments

0

Thats most likely because the default state of txtSubmissionDate is enabled already.

Try this in your page_load:

txtSubmissionDate.enabled = !chkSubmitted.Checked

To clarify, the textbox should not (!) be enabled when the Checkbox is.

Comments

0

Put this in Page_PreRender event. At this stage it will capture the user affected state of chkSubmitted.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.