setvalue of asp:Label with JavaScript

  • Thread starter Thread starter Anton ml. Vahèiè
  • Start date Start date
A

Anton ml. Vahèiè

Hi,

Can anyone tells me how can I set value of asp:Label field with JavaScript?

Anton, ml.
 
Basically I want to get some values from client browser and use/write them
in my asp:label.

Anton, ml.
 
How about creating a cookie using javascript and then picking this up using
your code behind?
 
Hi,

That's good idea, but I don't want to have cookies behind my code... I
read somewhere that it can be done with RegisterHiddenField. I create a
hiddenfield even change value, but then how I read hidden field back to my
variable in asp.net program... Examples about it are not too perfect in MS
documentation.

Anton, ml.
 
Hello Anton,

I believe you have to something like this:

var elMyElement = document.getElementByID("ElementName");

elMyElement.innerHTML = "your text here";

Adam
 
Dan said:
How about creating a cookie using javascript and then picking this up using
your code behind?

<form id="frmMain" method="post" runat="server">
<asp:Label ID="lbl" Runat="server"></asp:Label>
</form>


private void Page_Load(object sender, System.EventArgs e)
{
this.RegisterStartupScript("initLabel", "<script>document.all['" +
lbl.ClientID + "'].innerText='some value';</script>");
}
 
A label with the id "myLabel" renders as a span if the page uses master
pages the id will be changed, it will have a prefix starting with ctl00. you
need to find that Id in the source and then use it in your JavaScript to
change the spans innerText or innerHTML

to get the client id of the server control you can also use
<%=myLabel.clientId%>
 
Back
Top