www.webdeveloper.com
Results 1 to 7 of 7

Thread: Accessing ASP:Textbox using Javascript

  1. #1
    Join Date
    Apr 2007
    Posts
    13

    Accessing ASP:Textbox using Javascript

    I am trying to reference a text box in a parent/opening window from a popup window using javascript. For a normal HTML input text box it works fine, but I cannot get it to work with an ASP Textbox.

    I can use window.opener.document.getElementById("Text1").value to get the value from the normal HTML text box, but the same doesn't work for the ASP textbox. If I try it, I get the error “Object required”. Likewise, I have tried using

    window.opener.document.getElementById("Text2.ClientID").value

    and that gives the same error, while

    window.opener.document.getElementById("<%=Text2.ClientID%>").value

    gives me the error “The name 'Text2' does not exist in the current context”


    How do I reference an ASP text box in the parent window like the above?

    Thanks

  2. #2
    Join Date
    Apr 2007
    Posts
    3

    Try this

    Try this

    document.Form1.text1.value

  3. #3
    Join Date
    Dec 2006
    Location
    Scotland
    Posts
    450
    I would need to see the html in the aspx code.

    In general if you have..

    <asp:textbox id="foo" runat="server"/>

    ..you should be able to simply use..

    document.getElementById("foo").value

    ..to access the element on the page. Obviously you must do this lookup after the page has rendered the textbox otherwise it will error. (Make sure that isn't your problem before reading the rest.)

    However if you have the textbox inside a component (ascx) that is then added to the aspx you will find that the id is altered by .NET due to it assuming you are too stupid to manage your own id's and make them unique. This means if you have a textbox that is part of a component that has the class name of "myComponent", you will find that while you use the same asp:textbox code as shown above, it is actually rendred on the client with an id of "myComponent_foo". Somewhat frustrating as you are expecting the id to be minus the prefix.

    If this is the problem then there are several solutions to it. One of them is to provide the prefix to the client as well. You sort of do it already with your code via the .clientID property. Passing that down to the client will provide you with the runtime prefix that is applied.

    Other than that you can wrap your textbox inside a non web control, like so..

    <span id="myTextbox"><asp:textbox id="foo" runat="server"/></span>

    You still need the id/runat for the web control, but the containing span is not amended by the .NET framework. With that you can do..

    document.getElementById("myTextbox").getElementsByTagName("input")[0]

    ..to pick up the element.

    This whole re-writing of the id is a right pain in the ass (if that is your problem) and can only be properly resolved by overwriting the HTML writer class and that raises problems itself.

    Hope that helps.
    Essential Links: DOM | JS | CSS
    ---
    #javascript on EfNet: Direct Link
    ---
    I've known you since you were a twenty, and I was twenty, and thought that some years from now, a purple little, little, lady will be perfect, for this dirty old and useless clown...
    Gogol Bordello

  4. #4
    Join Date
    Dec 2006
    Location
    Scotland
    Posts
    450
    Quote Originally Posted by webdevmj
    Try this

    document.Form1.text1.value
    That would only work if the form had the name of "Form1". The .NET framework doesn't automatically add that.

    There is also the debate about using name attributes on anything other than form elements, and not the form tag itself, but as VS2003 produces code that at best will validate xhtml 1.0 transitional it's probably a moot point.
    Essential Links: DOM | JS | CSS
    ---
    #javascript on EfNet: Direct Link
    ---
    I've known you since you were a twenty, and I was twenty, and thought that some years from now, a purple little, little, lady will be perfect, for this dirty old and useless clown...
    Gogol Bordello

  5. #5
    Join Date
    Apr 2007
    Posts
    3
    The code was used in a user control that can be loaded in a web form with default id "Form1"

  6. #6
    Join Date
    Apr 2012
    Posts
    2
    Please help.

    The value that gets passed using javascript between a popup window and parent opener window, cannot be recorded in the program on Submit button. The passed value reflects in the textbox in parent/opener window but when Submit button is clicked, the contents of the textbox posted onto the database show they are blank which means it just superficially shows that passed value in textbox but not recorded into it.

    Thanks,
    Dipti

  7. #7
    Join Date
    Apr 2012
    Posts
    2
    Quote Originally Posted by desai14 View Post
    Please help.

    The value that gets passed using javascript between a popup window and parent opener window, cannot be recorded in the program on Submit button. The passed value reflects in the textbox in parent/opener window but when Submit button is clicked, the contents of the textbox posted onto the database show they are blank which means it just superficially shows that passed value in textbox but not recorded into it.

    Thanks,
    Dipti
    My textbox on parent form was readonly. Changing that solved the problem. Thanks.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles