.NET Framework Bookmark and Share   
 index > JScript for the .NET Framework > Need help in accessing web control from within Javascript.
 

Need help in accessing web control from within Javascript.

All i am trying to do is to have a button which when clicked should change the text of a label.

following is the code:

this is in the header section of the aspx page.

function faisal()
 {
  var a1 = document.getElementById('<%= Label1.ClientID %>');
  document.getElementById('<%= Label1.ClientID %>').Text = "hello";
  return false;
 }

the following code is in the code behind file

private void Page_Load(object sender, System.EventArgs e)

{

Button1.Attributes.Add("onclick","return faisal()");

}


when i press the button, nothing happens.
if i add an alert box inside the javascript function, it displays the alert box but the text of the label doesnt change.

any ideas?Tongue Tied

Thanks
Faisal Siddiqui


Faisal H. Siddiqui

Dear Friend
   The Code can be written in the following way.

function faisal()
 {
  var a1 = document.getElementById('<%= Label1.ClientID %>');
  document.getElementById('<%= Label1.ClientID %>').value = "hello";
  return false;
 }


In the Above i used 'value' This will work fine.

J Arputha Ravi
 wrote in message
news:c8a19d3b-91bc-491d-ac47-3120799843cf@discussions.microsoft.com
> this is in the header section of the aspx page.
>
> function faisal()
>  {
>   var a1 = document.getElementById('<%= Label1.ClientID %>');
>   document.getElementById('<%= Label1.ClientID %>').Text = "hello";
>   return false;
>  }

I don't know any DHTML element that would have Text property. View the 
source of the HTML page as seen on the client, figure out what HTML 
element corresponds to your label, check DHTML reference to see how to 
change the text of this element. It might be value property, or 
innerText.
-- 
With best wishes,
    Igor Tandetnik 


MigrationUser 1
Hi Faisal,

    Add a breakpoint in this statement to makesure this Label1.ClientID why it is not retrieving.

 I checked it what you are doing exactly but i have added javascript also in code behind then registered as cleint script.

regards,
syed
syedHameed
1.  I don't think that this will work in the <head> section:

function faisal()
 {
  var a1 = document.getElementById('<%= Label1.ClientID %>');
  document.getElementById('<%= Label1.ClientID %>').Text = "hello";
  return false;
 }

as I don't think that Label1 is created at that point and so the ClientID will be an empty string. Move it after the Label1 declaration.

2. As a previous poster pointed out this
   document.getElementById('<%= Label1.ClientID %>').Text = "hello";
will not work as a Label renders as a <span> in html, and there is no Text property. Try this instead:
   document.getElementById('<%= Label1.ClientID %>').innerText = "hello";

You could also use innerHtml if you're adding HTML markup.

NC...

NC01

Dear Friend
   The Code can be written in the following way.

function faisal()
 {
  var a1 = document.getElementById('<%= Label1.ClientID %>');
  document.getElementById('<%= Label1.ClientID %>').value = "hello";
  return false;
 }


In the Above i used 'value' This will work fine.

J Arputha Ravi
salam faisal,you need to do a small change..inspite of .value add .innerHTML property


i.e


var a1 = document.getElementById('<%= Label1.ClientID %>');
document.getElementById('<%= Label1.ClientID %>').innerHTML= "hello";
return false;


Now it works....
  • Proposed As Answer bymd.imtiaz Tuesday, June 16, 2009 7:05 PM
  •  
md.imtiaz

You can use google to search for other answers

Custom Search

More Threads

• calling .net dll method from javascript.
• hidden controls
• ECMA-327 interpreter for Compact Framework
• How to change Image SRC in Pocket IE?
• How do I set a table's visible property to false, and with javascript set it back to visible.
• Help converting code to JScript
• Disable keys
• Calling Javascript from C#.NET
• Using WQL "ASSOCIATORS OF"
• TreeView Control