Writing to an ASP textbox from javascript

A

alanliang

I want to be able to populate an asp textbox with logitude and
lattitude values whenever a user selects a point or creates a marker on
google maps. Simply, how do I change an asp's textbox value through
javascript?

Thanks,

Alan
 
S

slagomite

In general, you'll have to generate the client-side code on the
server-side (e.g., in your Page.PreRender handler), in order to
reference the resulting client ID for the textbox. For example:

HTML:
<asp:TextBox id="txtLat" runat="server" />
<asp:TextBox id="txtLong" runat="server" />

[Code-Behind]
protected TextBox txtLat;
protected TextBox txtLong;
....
private void Page_PreRender(...)
{
StringBuilder sb;

sb = new StringBuilder();
sb.Append("<script language='javascript'>\r\n");
sb.Append("function ChangeCoords(strLat, strLong)\r\n");
sb.Append("{\r\n");
sb.Append("    document.getElementById('");
sb.Append(this.txtLat.ClientID"); sb.Append("') = strLat;\r\n");
sb.Append("    document.getElementById('");
sb.Append(this.txtLong.ClientID"); sb.Append("') = strLong;\r\n");
sb.Append("}\r\n");
sb.Append("</script>\r\n");
this.RegisterClientScript("_PageScripts", sb.ToString());
}


Hope this helps,
Luke
 
S

sloan

Check out
http://msdn.microsoft.com/library/d...UIPageClassRegisterClientScriptBlockTopic.asp

Page.RegisterClientScriptBlock Method

that is to get the javascript written to the page (when you do a viewsource
on the hmtl page).

You still have to write your script function.


The trick is figuring out what the official name of the text box in the form
is.
The previous post does a good job, using the getElementById method.

you can also do something like



this.frmMain.myTextBox.value;

where frmMain is the name of your form , in your <form name='frmMain'
id='frmMain'> declaration.

Aka, getElementById is probably better, but just showing you how you might
hard reference it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top