?
Solved

I see ClientID but Javascript displays undefined

Posted on 2012-08-21
7
Medium Priority
?
1,068 Views
Last Modified: 2012-08-28
1. I have a usercontrol with this button

<asp:Button  ID="btnClear" Text="Clear"  runat="server"  />

Open in new window


2. In UC's code behind, I have a property
public Label PageLabel { get; set; }

Open in new window


3. in UC's page_load, I have this javascript
       
Response.Write(PageLabel.ClientID); 
        string t = "javascript:clrCtrl('" + PageLabel.ClientID + "');";
       btnClear.OnClientClick = "javascript:clrCtrl('" + PageLabel.ClientID + "');";

Open in new window


4. In parent page, i set the label
ucSpeciatly.PageLabel = lMsgFax;

Open in new window


5. in UC, i have this JS code

 function clrCtrl(lblId) {

        alert(lblId);
        //var lblObj = document.getElementById(lblId);
        //alert(lblObj);
//        lblObj.innerHTML = "";
}

Open in new window



6. I put a debug step and I can see the value for PageLabel.ClientID. It's
DashboardContentPlaceHolder1_lMsgFax
I also have response.write and even write the JS call and its:
javascript:clrCtrl('DashboardContentPlaceHolder1_lMsgFax');

But when the JS function is called, I get undefined for lblId in that alert call. What am I missing?
0
Comment
Question by:Camillia
  • 3
  • 3
7 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 38320549
Try using the ID property, rather than the ClientID property.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 38320803
Pls give a URL or the view-source of the code and elements involved
Also look in the console for other errors
0
 
LVL 7

Author Comment

by:Camillia
ID: 38321934
I tried the ID and still got Undefined. I looked at viewsource and the label renders like this

 <td width="310"><span class="feedback"><span id="DashboardContentPlaceHolder1_lMsgFax">Please click 'Next' to refer by fax.</span></span></td>


Using ID, i get lMsgFax. I printed the JS line and it comes out as
javascript:clrCtrl('lMsgFax');

Just the weirdest thing. I use IE9. I cant give the URL because it's a private medical site. What else can I try??
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 38322012
This might be because it is getting the ClientID or ID before the control is added to the naming container, so the value is incorrect at that point for document.getElementById to work.
0
 
LVL 7

Author Comment

by:Camillia
ID: 38322040
oh, so is there another way of doing this?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 38322104
Are you using JQuery?

.find()
http://api.jquery.com/find/

$('li.item-ii').find( $allListElements );

Open in new window

0
 
LVL 7

Author Comment

by:Camillia
ID: 38322157
dang it, i dont know JQuery, let me google and see what i can find.  I want to find that lMsgFax and set it's value to blank.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

850 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