Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

ASP.NET 4.0 - Accessing Server Controls in Javascript

Posted on 2011-10-18
4
Medium Priority
?
221 Views
Last Modified: 2012-05-12
I'm currently converting a web site developed in ASP.NET 2.5 (VB.NET Code) to ASP.NET 4.0 (VB.NET code).

The problem I'm having (that I did not expect) is accessing server side controls through Javascript.

I have an Textbox and ListBox server controls on the web page.

<asp:ListBox ID="lstStore" runat="server"></asp:ListBox>
<asp:TextBox ID="txtCriteria" runat="server">

The following Javascript works in the ASP.NET 2.5 Web Site:

var list = document.getElementById("lstStore");
var txtCriteria = document.getElementById("txtCriteria");

alert(txtCriteria.value);
alert(list.options.length);

The javascript error I get is "Unable to get the value of the property 'value' on the text box" and "Unable to get the value of the property 'Options' on the list box.

Has something changed with ASP.NET 4.0?  Note:  I do not get any errors on the two 'var' statements.  It is properly identifying the controls on the client side.
0
Comment
Question by:tfewster500
  • 2
4 Comments
 

Author Comment

by:tfewster500
ID: 36990387
Sorry, typo in my sample code.  I am propertly closing the text box tag

<asp:ListBox ID="lstStore" runat="server"></asp:ListBox>
<asp:TextBox ID="txtCriteria" runat="server">"></asp:TextBox>

0
 
LVL 83

Accepted Solution

by:
leakim971 earned 2000 total points
ID: 36990391
try :

var list = document.getElementById("<%= lstStore.ClientID %>");
var txtCriteria = document.getElementById("<%= txtCriteria.ClientID %>");

alert(txtCriteria.value);
alert(list.options.length);

http://weblogs.asp.net/asptest/archive/2009/01/06/asp-net-4-0-clientid-overview.aspx
0
 
LVL 7

Expert Comment

by:kovilpattiBalu
ID: 36990986
One thing is your control name should ne within single quotes.
ie:
var list = document.getElementById('lstStore');
var txtCriteria = document.getElementById('txtCriteria');

Second thing is you can use HTML control name of both lstStore and txtCriteria.
[HTML control name you can get it from view source]
0
 

Author Closing Comment

by:tfewster500
ID: 36994538
Awesome this worked.
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Suggested Courses
Course of the Month14 days, 8 hours left to enroll

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question