Passing Server Side Control to Client Side Script

Hi All,

I have an asp:DropDownList and an asp:TextBox on my form. I want to disable or enable the textbox depending on the selection in the dropdown. I am using master pages and the problem is that when I use the onChange event for the dropdown on the client side, I don't know the ID of the the textbox on the client side to disable or enable it.

 How can something like this be done?


4/13/2009 4:46:52 PM 24353 articles. 2 followers. Follow

3 Replies

Similar Articles

[PageSpeed] 26
Get it on Google Play
Get it on Apple App Store

You can use the ClientID property of a control to determine the ID, something like:


<script type="text/javascript">
    function ShowMeTheID(){
        alert('<%= DropDownList1.ClientID %>');

Hope that helps.


Don't forget to click "Mark as Answer" on the post that helped you. This credits that member, earns you a point and marks your thread as Resolved so everyone will know you have been helped.
4/13/2009 4:57:52 PM

Like this?

<asp:DropDownList id="DropDownList1" onchange="dropDownList1OnChange(this);" runat="server">
 <asp:listitem value="">Select one</asp:listitem>
 <asp:listitem value="1">Item 1</asp:listitem>
 <asp:listitem value="2">Item 2</asp:listitem>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

<script type="text/javascript">
function dropDownList1OnChange(elementRef)
 var textBoxRef = document.getElementById('<%= TextBox1.ClientID %>');
 // Or:
 //var textBoxRef = document.getElementById('<%= (Page.Master.FindControl("TextBox1")).ClientID %>');

 // Disable if "Item 1" is selected in the DropDownList,
 // Enable otherwise...
 textBoxRef.disabled = (elementRef.value == '1') ? true : false;
// -->

Note that I attached the event handler to the element in the HTML declaration. This will work, but will produce a compiler warning. The proper way to do this for a server control is in the Page_Load handler of the server-side code like this:
 DropDownList1.Attributes.Add("onchange", "dropDownList1OnChange(this);");


4/13/2009 6:33:35 PM

That was perfect. Works beautifuly. Thank you so much for your help.

4/14/2009 3:26:00 AM

Similar Artilces:

client-side scripting or server-side scripting
I want to perform data validation in my page using javascript before the data is sent to my server, but I am very confuse that I don't know which way to perform, client-side validation or server-side validation? My javascript bible told me that javascript is a client-side scripting for data validation in client pc, but when I look in my bible, the validation part is done at server side. The worst is that, I find no way to use javascript in .asp or .aspx environment. I am really confuse with all these, could any one help me on this? Thanks. Use .NET validator co...

Validation Controls in .NET ( Client side or server side?)
 i have a very basic question. We have all these validation controls in ( required field, range validator and others) question is the required field validator is a server side control. So does that mean that it does a post back? it client side or server side control  The only controls that post back are buttons and other inputs where you set AutoPostback="True", such as a dropdownlist or a textbox.  Another way of posting back is using an UpdatePanel and placing a trigger in it for a specified control and/or event. All controls are considered clien...

How to access dynamically Html controls (client side controls) using C# server side scripts
Folks,I have problem try to to access dynamically Html controls (client side controls) using C# server side scripts. Please give me helps.     The error is as follows Exception Details: System.InvalidCastException: Specified cast is not valid.Source Error: Line 79: { Line 80: Control c = Page.FindControl("ddlTC" + i); Line 81: ((DropDownList)c).DataSource = mdt; Line 82: ((DropDownList)c).DataTextField = "ClassID"; Line 83: ((DropDownList)c).DataValueField = "Course"; My aspx page source codes: ...<...

How to pass param values from server side to client side scripts
Hi, I have a page containing a datagrid with a list of geographic positions of interesting places. Users can filter this grid. Clicking an item the system displays the place on a map using MS Virtual Earth map component that exposes a client side javascript api. My problem is that the filtering is done on server side, but the map runs on client side. So during page load, I need to create long strings containing complex javascript codes on the server side, and add it as an event handler attribute to my components. This causes a lot of problems, for exemple...

passing data to a server side textbox from client side script
Qu: passing data to a server side textbox from client side java script.Does anyone know how to do this?Thanks,Graham. Remember that when you view an aspx page in your browser, you are viewing straight html code (view the source).  Thus, access the textbox as you would a normal <input type="text> tag.HTH, rchern.Churned Ajax ToolkitPlease choose "Mark as Answer" for the posts that lead you to a solution....

Can you pass information from client side script to server side
Is it possible to pass information from client side to server side without the use of cookies? i am creating textbox's that i generate in a loop that are displayed to the screen and i need to access the values in those boxes. I can get them with javascript but dont know how to use asp to get them??? thanks The last sample on this page shows how your client-side script can send information back to your server-side code. I hope this helps.Alister...

Call server-side script from client-side script
Hi to all, I would like to ask you How can i call from client-side script a server-side script and to have the result from the server-side script in the client-side script. For example, i wand to call a function writing in server-side (DynaScript) script from a client-side script (JAVASCRIPT) and to have an answer from the function. Thanks in advance. Nick, One way that you could get round this is to write your client script code as you would server script eg. <SCRIPT language=javascript> function getValue()... would be written as: <% psDocument.WriteLn(&...

client side script Vs Server side Script
Which is better ? Using Client Side validations with Java Script or Using Server Side validation controls provided in You should use both... Client-side for a rich user experience and server-side to support browsers without javascript support or with javascript turned off....

How to call a client side script from a server side script?
Hi guys, I have a Button1_Click function which gets executed on the server side as soon as Button1 is clicked. I have a client side function, test, which is  written in vbscript. I need to call this test function from the Button1_Click function. Can anyone of you please tell me how to do this? The test function is inside an asp:content tag (Please excuse my terminology if I am wrong, I am new to all this).  Thanks and Regards, RJ Have you tried to use the below code Button1.Attributes.Add("onclick","test()"); HCHaissam Abdul MalakMCAD.NET| Blog | Yes, ...

Difference between client side scripting and server side scripting
Hi,      Can u  plz, explain why the server side scripting is necessary, inorder to write the client side scripting. What is the main difference between the client side scripting and server side scripting. Client-side scripting is code that is executed on the client side, such as JavaScript. Server-side scripting is code that is executed on the server, such as VB, VB.NET, C#, or J#. For a better definition of server-side scripting and when it's necessary, please visit OlshanASPInsider | Microsoft MVP...

Combining a server-side control with a client-side control!
Hi all, I am trying to combine a server-side control with a client-side control. I have created a listbox in both sides, the client-side control's code is: <select size="4" name="lstFruit" id="Select1" ondblclick="lst_dblClick" visible="false"> <option value="Banana">Banana</option> <option value="Apple">Apple</option> <option value="Mango">Mango</option> <option value="Orange">Orange</option> </select> The other one is the server-side control which has this code:   I wanted to add the ondblClick attribute to the se...

Client-Side scripting vs Server-Side scripting
hello I need an article that spaeks about Client-Side scripting vs Server-Side scripting,and when i need to use one of them thank you for the help and for the time here are some good links to a bunch of articles on that subject...

What are diff Server-side control and Client-side control ?
What are diff Server-side control and Client-side control ? and diff server -side coding and client -side coding ? please help I have answered the first part of your question in your previous posting. Server-side coding languages are ASP, ASP.NET, PHP, CGI (Perl) and many, many more. They are used to create dynamic web content (based on additional information that is attached to the request) and are often used to extract information from databases. Client-side coding languages are JavaScript, VBScript, EMACScript and are used to create dynamic content that responds to the u...

Client-Side and Server Side Scripting
Hi, im currenty in the process of trying to get an page to work with both <script> and <script runat="server" tags... idea being, i have a <script> tag which holds all the client side scripting for my webpage, at the moment this is just some slider controls which i have created (the scripting behind this is actually from someone on a different forum, which i have modified to do what i want..) - but i also want a <script runat="server"> tag to hold all the code for the buttons pressed etc in my web-page. im trying my hardest not to use a ...

Web resources about - Passing Server Side Control to Client Side Script -

Passing off - Wikipedia, the free encyclopedia
This article includes a list of references , related reading or external links , but its sources remain unclear because it lacks inline citations ...

Facebook Gets Passing Grade From Irish Agency Audit - AllFacebook
Facebook received its report card in the form of a detailed audit by the Office of the Irish Data Protection Commissioner, and its grades were ...

Whether passing the 9/11 Health Bill,... - Kirsten Gillibrand - Facebook
Whether passing the 9/11 Health Bill, repealing DADT, or my call to action for women, I have always urged New Yorkers to make their voices heard. ...

Thread: Buck-passing bullshit.
The Repubs are tempting fate, trying to allocate blame for Obama's loss in November. Obviously it's a little presumptuous. If Romney wins, it's ...

Scripting News: People are asking about the history of RSS today because of Aaron's passing.
People are asking about the history of RSS today because of Aaron's passing. In April 2004 I put together a timeline of the various specs that ...

AfroCat ◆ Cute and free pet game ◆ Perfect for passing the time! on the App Store
Read reviews, compare customer ratings, see screenshots, and learn more about AfroCat ◆ Cute and free pet game ◆ Perfect for passing the time!. ...

walmart donated a palette of water. everyone was great about passing the bottles back
Explore pinguino's photos on Flickr. pinguino has uploaded 16442 photos to Flickr.

Astronauts Mark Leonard Nimoy’s Passing - YouTube
NASA Astronaut Mike Fincke and ESA European Space Agency Astronaut Luca Parmitano reflect on the inspiration that actor Leonard Nimoy’s character ...

UAE remembers Zayed 10 years after his passing
Several activities will be taking place on Ramadan 19th marking the 10th anniversary of Sheikh Zayed’s passing.

Should IVF be used to avoid passing on mutations?
Should IVF be used to avoid passing on mutations?

Resources last updated: 1/8/2016 8:50:50 PM