Writing to an ASP textbox from javascript

Discussion in 'ASP .Net' started by alanliang@gmail.com, Jan 31, 2006.

  1. Guest

    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
     
    , Jan 31, 2006
    #1
    1. Advertising

  2. slagomite Guest

    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
     
    slagomite, Jan 31, 2006
    #2
    1. Advertising

  3. sloan Guest

    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.



    <> wrote in message
    news:...
    >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
    >
     
    sloan, Feb 1, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Q2hyaXM=?=
    Replies:
    3
    Views:
    13,836
    Peter Blum
    Mar 30, 2005
  2. Imran Aziz
    Replies:
    2
    Views:
    11,490
    Imran Aziz
    Sep 5, 2005
  3. agb

    writing two textbox's text in one textbox

    agb, Aug 26, 2003, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    181
    Ken Cox [Microsoft MVP]
    Aug 27, 2003
  4. ald
    Replies:
    3
    Views:
    189
    Axel Dahmen
    Dec 21, 2003
  5. ErwinP
    Replies:
    1
    Views:
    767
    ErwinP
    Aug 19, 2005
Loading...

Share This Page