Visible the text box after select the item in dropdownlist but before postback in cli

calvin

Regular
Joined
Nov 6, 2003
Messages
71
Hi,

I need to visible a textbox and RequiredFieldValidator when user select the item in dropdownlist before click the submit button. The autopostback of dropdownlist is set to false and textbox & Validator visible also set to false. I want to call function in client script(Javascript) to call out the textbox. For example, If select item "Other", then the textbox is visible to enter data. The control to validate of requiredfieldvalidator is set to this textbox. Other than that item will hide the textbox & validator. I using ASP.NET with VB to do the data entry form.
Thank you.

Calvin
 
You should place your textbox inside a <DIV> tag, set the ID of the div tag to something like "div_txt" or whatever and write a javascript function that sets the visibility of the tag based off of the current value of an element passed to the function. Then just set the drop down list to call the function when the text changes, passing itself as the parameter. like this:
<SCRIPT language=javascript>
function SetVis(element){
if(element.value == "Other"){
document.getElementById('div_txt').style.visibility= 'false';
}
else
document.getElementById('div_txt').style.visibility= 'true';
}
</SCRIPT>

...html down here...
<DIV style='VISIBILITY:true;' id='div_txt'>
<ASP:TEXTBOX id='txt' runat='server'>
</DIV>
<ASP:DROPDOWNLIST id='cboList' runat='server'>

Now add the following in your codebehind page_load event:
this.cboList.Attributes.Add("ontextchanged","SetVis(this);");

Hope this helps,

Ron
 
Dear Ron,

I get an error message of "Object Expected". I can't write the code below in page_load, the"this" get error of "Name 'this' is not delared".

Your code:
this.cboList.Attributes.Add("ontextchanged","SetVis(this);");

I changed it to My code:
cboList.Attributes.Add("ontextchanged","SetVis();");

What for we need to put "this" in code? Instead of this, what I need to do? :confused:

Thank you Ron for help
Calvin
 
Back
Top