Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ASP: dynamically enable textbox with check box

Status
Not open for further replies.

dkaplan

Programmer
Jan 29, 2001
98
US
I am trying to dynamically enable (or disable) an ASP textBox when the check changes in the adjacent checkBox.

When I put "OnCheckedChanged" in the CheckBox I get the error: "CS0149: Method name expected"

The CheckBox is:
<asp:checkbox id="S5_other" runat="server" text="Other" OnCheckedChanged="EnableTextBox()" Checked="True"></asp:checkbox>

EnableTextBox() is in javascript

<Script language =javascript>

function EnableTextBox()
{
...
enableField("myTextBox", true);
...
}
</SCRIPT>

Thanks in advance,


 
OnTextChanged is a server side event. the javascript is a client side event. the two do not mix.

1st you need to decide where you want to do the work. server or client. client is definitely the right choice for this. then you need to add a click/change javascript event to the checkbox.
Code:
mycheckbox.Attributes.Add("onchange", "enableField('"+mytextbox.ClientID+"', true);"
note that you must reference the textbox by the ClientID, not the ID.
[/code]

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Thanks, Jason.

My question will undoubetedly reveal how new I am at this.

I'm not sure I understand "reference by client ID."

I've tried both:

S5_other.Attributes.Add("onchange");

and document.all("S5_other").Attributes.Add("onchange");

but get an error both ways.



 
the code i provided above is server code (.net) not client code (js). either way Attributes.Add("onchange"); has no meaning. you provided the key (event in this case), but not the value (what to do on change).

ClientID = the html id rendered from webforms. the client id doesn't always equal the server id. as controls are nested within other controls the webforms html engine will autogenerate the id for you.

if you are new to web development, and/or webforms development, take some time to understand the web environment before jumping right into the code. things you want to understand

web:
request/response
get vs. put
css
a good js library like jquery, prototype, moo tools, dojo
html
the web is stateless

webforms:
webforms (asp.net) life cycle
the fact that asp.net != webforms
webfroms hacks the stateless environment to attempt to make it look stateful
webforms has a concepts of viewstate, postback, and events
how the different components of webforms work: Page(aspx), UserControl (ascx), Global (asax), generic handlers (ashx), master pages (master).

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top