document.getElementById(), error in content page content page

Discussion in 'ASP .Net' started by Dave L, Mar 4, 2010.

  1. Dave L

    Dave L Guest

    Could someone explain: I have a java popup window function that I would
    like to pass a querystring ID from a hiddenfield on my asp.net content page.
    The following code works. Note: for this test I simply set the hiddefield
    var TransID.value = 21 in btnPopUp.click event See Item 1. The thing I
    don't understand is why can't I have the function take the TransID as a
    parameter. see Item 2.


    **************Item 1.****************

    <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">

    <script type="text/javascript" >
    function PopUp() {

    var InqID = document.getElementById('<%=TransID.ClientID%>').value;
    alert(InqID);
    }

    </script>
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1"
    Runat="Server">
    <asp:Button ID="btnPopUp" runat="server" Text="Press Me"
    OnClientClick="javascript:popUp();" />

    <asp:HiddenField ID="TransID" runat="server" />
    </asp:Content>



    *************Item 2.****************

    This Fails

    ***********************************


    <script type="text/javascript" >
    function PopUp(obj) {

    var InqID = document.getElementById(obj).value;
    alert(InqID);
    }

    </script>
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1"
    Runat="Server">
    <asp:Button ID="btnPopUp" runat="server" Text="Press Me"
    OnClientClick="javascript:popUp('<%=TransID.ClientID%>');" />

    <asp:HiddenField ID="TransID" runat="server" />
    </asp:Content>


    Any help would be greatly appreaciated.

    Feel free to email me at
     
    Dave L, Mar 4, 2010
    #1
    1. Advertising

  2. Dave L

    Mr. Arnold Guest

    Dave L wrote:
    > Could someone explain: I have a java popup window function that I would
    > like to pass a querystring ID from a hiddenfield on my asp.net content
    > page. The following code works. Note: for this test I simply set the
    > hiddefield var TransID.value = 21 in btnPopUp.click event See Item 1.
    > The thing I don't understand is why can't I have the function take the
    > TransID as a parameter. see Item 2.
    >
    >


    <script type="text/javascript" >
    function PopUp(obj) {

    var InqID = document.getElementById(obj).value;
    alert(InqID);
    }

    </script>
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1"
    Runat="Server">
    <asp:Button ID="btnPopUp" runat="server" Text="Press Me"
    OnClientClick="javascript:popUp('<%=TransID.ClientID%>');" />

    <asp:HiddenField ID="TransID" runat="server" />
    </asp:Content>
    ------------------------------------------------

    Didn't you pass the control as an object to the function?

    Maybe, I am wrong, but the object has the data and you don't need to do
    getEbyID in the function, because you passed the control to the function.

    var InqID = object.value;

    But first do an alert(obj) to even see if you have an object.
     
    Mr. Arnold, Mar 4, 2010
    #2
    1. Advertising

  3. Mr. Arnold wrote:
    > Dave L wrote:
    >> Could someone explain: I have a java popup window function that I
    >> would like to pass a querystring ID from a hiddenfield on my asp.net
    >> content page. The following code works. Note: for this test I simply
    >> set the hiddefield var TransID.value = 21 in btnPopUp.click event See
    >> Item 1. The thing I don't understand is why can't I have the function
    >> take the TransID as a parameter. see Item 2.
    >>
    >>

    >
    > <script type="text/javascript" >
    > function PopUp(obj) {
    >
    > var InqID = document.getElementById(obj).value;
    > alert(InqID);
    > }
    >
    > </script>
    > </asp:Content>
    > <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1"
    > Runat="Server">
    > <asp:Button ID="btnPopUp" runat="server" Text="Press Me"
    > OnClientClick="javascript:popUp('<%=TransID.ClientID%>');" />
    >
    > <asp:HiddenField ID="TransID" runat="server" />
    > </asp:Content>
    > ------------------------------------------------
    >
    > Didn't you pass the control as an object to the function?
    >
    > Maybe, I am wrong, but the object has the data and you don't need to do
    > getEbyID in the function, because you passed the control to the function.
    >
    > var InqID = object.value;
    >
    > But first do an alert(obj) to even see if you have an object.


    No, it's not an object, it's a string. Note the apostrophes around the
    value in the function call.

    --
    Göran Andersson
    _____
    http://www.guffa.com
     
    Göran Andersson, Mar 4, 2010
    #3
  4. Dave L wrote:
    > Could someone explain: I have a java popup window function that I would
    > like to pass a querystring ID from a hiddenfield on my asp.net content
    > page. The following code works. Note: for this test I simply set the
    > hiddefield var TransID.value = 21 in btnPopUp.click event See Item 1.
    > The thing I don't understand is why can't I have the function take the
    > TransID as a parameter. see Item 2.
    >
    >
    > **************Item 1.****************
    >
    > <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
    >
    > <script type="text/javascript" >
    > function PopUp() {
    >
    > var InqID = document.getElementById('<%=TransID.ClientID%>').value;
    > alert(InqID);
    > }
    >
    > </script>
    > </asp:Content>
    > <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1"
    > Runat="Server">
    > <asp:Button ID="btnPopUp" runat="server" Text="Press Me"
    > OnClientClick="javascript:popUp();" />
    >
    > <asp:HiddenField ID="TransID" runat="server" />
    > </asp:Content>
    >
    >
    >
    > *************Item 2.****************
    >
    > This Fails
    >
    > ***********************************
    >
    >
    > <script type="text/javascript" >
    > function PopUp(obj) {
    >
    > var InqID = document.getElementById(obj).value;
    > alert(InqID);
    > }
    >
    > </script>
    > </asp:Content>
    > <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1"
    > Runat="Server">
    > <asp:Button ID="btnPopUp" runat="server" Text="Press Me"
    > OnClientClick="javascript:popUp('<%=TransID.ClientID%>');" />
    >
    > <asp:HiddenField ID="TransID" runat="server" />
    > </asp:Content>
    >
    >
    > Any help would be greatly appreaciated.
    >
    > Feel free to email me at


    The problem is that you can't have a script tag inside the Button tag.

    The easiest is to set the OnClientClick value from code behind:

    btnPopUp.OnClientClick = "PopUp('" + TransID.ClientID + "')";

    (Note that the javascript: protocol is only used when you put script in
    an URL, e.g. in the href attribute of a link. If you use it in the
    onclick attribute, it will instead be interpreted as a label.)

    --
    Göran Andersson
    _____
    http://www.guffa.com
     
    Göran Andersson, Mar 4, 2010
    #4
    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. Ghyslaine Crespe
    Replies:
    4
    Views:
    668
    Evertjan.
    Dec 26, 2004
  2. Replies:
    6
    Views:
    295
  3. wk
    Replies:
    5
    Views:
    198
    Dietmar Meier
    Apr 22, 2005
  4. Martin
    Replies:
    3
    Views:
    167
  5. Replies:
    4
    Views:
    184
    Jonas Raoni
    Nov 21, 2006
Loading...

Share This Page