how to get ID of embedded control in javascript?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dan

    #1

    how to get ID of embedded control in javascript?

    Hi,

    There is a detailsview which contains a textbox in editmode.
    How to get the id of the textbox in javascript?
    I tried this:
    <asp:DetailsVie w ID="DetailsView 1" runat="server" >
    <asp:TemplateFi eld>
    <EditItemTempla te>
    <asp:TextBox ID="TextBox1" runat="server"> </asp:TextBox>
    </EditItemTemplat e>
    </asp:TemplateFie ld>
    </asp:DetailsView >

    <script type="text/javascript">
    var txt=document.ge tElementById('T extBox1')
    ....

    Thanks
    Dan


  • Nathan Sokalski

    #2
    Re: how to get ID of embedded control in javascript?

    I have never used the DetailsView control, but I have used templated
    controls and I have more than enough experience with ASP.NET to know that
    the generated id is almost never the one given to the server control ID
    attribute. You will need to use the FindControl() method and the ClientID
    property.
    --
    Nathan Sokalski
    njsokalski@hotm ail.com
    有声小说网为广大读者提供热门小说在线免费阅读,本站收集的网络文学小说情节跌宕起伏,有声小说网是值得书友们收藏的小说在线阅读网。


    "Dan" <nm@zszsz.xvwro te in message
    news:OOjAU2WLJH A.1500@TK2MSFTN GP06.phx.gbl...
    Hi,
    >
    There is a detailsview which contains a textbox in editmode.
    How to get the id of the textbox in javascript?
    I tried this:
    <asp:DetailsVie w ID="DetailsView 1" runat="server" >
    <asp:TemplateFi eld>
    <EditItemTempla te>
    <asp:TextBox ID="TextBox1" runat="server"> </asp:TextBox>
    </EditItemTemplat e>
    </asp:TemplateFie ld>
    </asp:DetailsView >
    >
    <script type="text/javascript">
    var txt=document.ge tElementById('T extBox1')
    ...
    >
    Thanks
    Dan
    >

    Comment

    • Mark Rae [MVP]

      #3
      Re: how to get ID of embedded control in javascript?

      "Dan" <nm@zszsz.xvwro te in message
      news:OOjAU2WLJH A.1500@TK2MSFTN GP06.phx.gbl...
      How to get the id of the TextBox in JavaScript?
      var txt=document.ge tElementById('< %=TextBox1.Clie ntID%>')


      --
      Mark Rae
      ASP.NET MVP


      Comment

      • Dan

        #4
        Re: how to get ID of embedded control in javascript?

        Thanks for replying,
        but with my code, i get the error:
        'TextBox1' is not declared'

        Textbox1 is embedded in detailsview1.




        "Mark Rae [MVP]" <mark@markNOSPA Mrae.netschreef in bericht
        news:uI4JjBYLJH A.5164@TK2MSFTN GP04.phx.gbl...
        "Dan" <nm@zszsz.xvwro te in message
        news:OOjAU2WLJH A.1500@TK2MSFTN GP06.phx.gbl...
        >
        >How to get the id of the TextBox in JavaScript?
        >
        var txt=document.ge tElementById('< %=TextBox1.Clie ntID%>')
        >
        >
        --
        Mark Rae
        ASP.NET MVP
        http://www.markrae.net

        Comment

        • Hillbilly

          #5
          Re: how to get ID of embedded control in javascript?

          When written inline into script that is generally linked to a separate file
          the statement implies the <%=TextBox1.Cli entID%expressio n is parsed when
          the page is compiled. Is this some kind of gift I have been ignorant of?


          "Mark Rae [MVP]" <mark@markNOSPA Mrae.netwrote in message
          news:uI4JjBYLJH A.5164@TK2MSFTN GP04.phx.gbl...
          "Dan" <nm@zszsz.xvwro te in message
          news:OOjAU2WLJH A.1500@TK2MSFTN GP06.phx.gbl...
          >
          >How to get the id of the TextBox in JavaScript?
          >
          var txt=document.ge tElementById('< %=TextBox1.Clie ntID%>')
          >
          >
          --
          Mark Rae
          ASP.NET MVP
          http://www.markrae.net

          Comment

          • Mark Rae [MVP]

            #6
            Re: how to get ID of embedded control in javascript?

            "Dan" <nm@zszsz.xvwro te in message
            news:%23xn0WfgL JHA.5692@TK2MSF TNGP04.phx.gbl. ..
            >>How to get the id of the TextBox in JavaScript?
            >>
            >var txt=document.ge tElementById('< %=TextBox1.Clie ntID%>')
            >
            Thanks for replying, but with my code, I get the error:
            'TextBox1' is not declared'
            >
            Textbox1 is embedded in detailsview1.
            Well there's your problem...

            JavaScript, like all languages derived from C, is case-sensitive - TextBox1
            is not the same as Textbox1...


            --
            Mark Rae
            ASP.NET MVP


            Comment

            • Dan

              #7
              Re: how to get ID of embedded control in javascript?

              This is my aspx code:

              <asp:DetailsVie w ID="DetailsView 1" runat="server" >
              <asp:TemplateFi eld>
              <EditItemTempla te>
              <asp:TextBox ID="TextBox1" runat="server"> </asp:TextBox>
              </EditItemTemplat e>
              </asp:TemplateFie ld>
              </asp:DetailsView >

              and javascript:

              <script type="text/javascript">
              var txt=document.ge tElementById('< %=TextBox1.Clie ntID%>')

              With this, i get the error: 'TextBox1' is not declared' and i don't see a
              difference between 'TextBox1' and 'TextBox1'.
              It is surely my problem, but i post this for help. Thanks



              "Mark Rae [MVP]" <mark@markNOSPA Mrae.netschreef in bericht
              news:ue3q6ugLJH A.4772@TK2MSFTN GP03.phx.gbl...
              "Dan" <nm@zszsz.xvwro te in message
              news:%23xn0WfgL JHA.5692@TK2MSF TNGP04.phx.gbl. ..
              >
              >>>How to get the id of the TextBox in JavaScript?
              >>>
              >>var txt=document.ge tElementById('< %=TextBox1.Clie ntID%>')
              >>
              >Thanks for replying, but with my code, I get the error:
              >'TextBox1' is not declared'
              >>
              >Textbox1 is embedded in detailsview1.
              >
              Well there's your problem...
              >
              JavaScript, like all languages derived from C, is case-sensitive -
              TextBox1 is not the same as Textbox1...
              >
              >
              --
              Mark Rae
              ASP.NET MVP
              http://www.markrae.net

              Comment

              • Dan

                #8
                Re: how to get ID of embedded control in javascript?

                This is my aspx code:

                <asp:DetailsVie w ID="DetailsView 1" runat="server" >
                <asp:TemplateFi eld>
                <EditItemTempla te>
                <asp:TextBox ID="TextBox1" runat="server"> </asp:TextBox>
                </EditItemTemplat e>
                </asp:TemplateFie ld>
                </asp:DetailsView >

                and javascript:

                <script type="text/javascript">
                var txt=document.ge tElementById('< %=TextBox1.Clie ntID%>')

                With this, i get the error: 'TextBox1' is not declared' and i don't see a
                difference between 'TextBox1' and 'TextBox1'.
                It is surely my problem, but i post this for help. Thanks


                "Mark Rae [MVP]" <mark@markNOSPA Mrae.netschreef in bericht
                news:ue3q6ugLJH A.4772@TK2MSFTN GP03.phx.gbl...
                "Dan" <nm@zszsz.xvwro te in message
                news:%23xn0WfgL JHA.5692@TK2MSF TNGP04.phx.gbl. ..
                >
                >>>How to get the id of the TextBox in JavaScript?
                >>>
                >>var txt=document.ge tElementById('< %=TextBox1.Clie ntID%>')
                >>
                >Thanks for replying, but with my code, I get the error:
                >'TextBox1' is not declared'
                >>
                >Textbox1 is embedded in detailsview1.
                >
                Well there's your problem...
                >
                JavaScript, like all languages derived from C, is case-sensitive -
                TextBox1 is not the same as Textbox1...
                >
                >
                --
                Mark Rae
                ASP.NET MVP
                http://www.markrae.net

                Comment

                • Mark Rae [MVP]

                  #9
                  Re: how to get ID of embedded control in javascript?

                  "Dan" <nm@zszsz.xvwro te in message
                  news:eZobW7gLJH A.5164@TK2MSFTN GP04.phx.gbl...
                  This is my aspx code:
                  >
                  <asp:DetailsVie w ID="DetailsView 1" runat="server" >
                  <asp:TemplateFi eld>
                  <EditItemTempla te>
                  <asp:TextBox ID="TextBox1" runat="server"> </asp:TextBox>
                  </EditItemTemplat e>
                  </asp:TemplateFie ld>
                  </asp:DetailsView >
                  >
                  and javascript:
                  >
                  <script type="text/javascript">
                  var txt=document.ge tElementById('< %=TextBox1.Clie ntID%>')
                  >
                  With this, i get the error: 'TextBox1' is not declared' and i don't see a
                  difference between 'TextBox1' and 'TextBox1'.
                  It is surely my problem, but i post this for help. Thanks
                  Look at your previous post - you wrote:

                  "Textbox1 is embedded in detailsview1"

                  Yet the DetailsView is not called "detailsvie w1", nor does it contain a
                  TextBox called "Textbox1"

                  If you do a View Source, what is the munged name of the DetailsView and what
                  is the name of the munged TextBox? Please be accurate...


                  --
                  Mark Rae
                  ASP.NET MVP


                  Comment

                  • Dan

                    #10
                    Re: how to get ID of embedded control in javascript?

                    in the page source, i see: 'DetailsView1_T extBox1'

                    var txt=document.ge tElementById('D etailsView1_Tex tBox1')

                    this line works. All i wanted to know is whether there was a better way
                    (using ClientID) to get the id. I find this method (searching in source) a
                    little bit too basic ..


                    "Mark Rae [MVP]" <mark@markNOSPA Mrae.netschreef in bericht
                    news:u13vjGhLJH A.2164@TK2MSFTN GP02.phx.gbl...
                    "Dan" <nm@zszsz.xvwro te in message
                    news:eZobW7gLJH A.5164@TK2MSFTN GP04.phx.gbl...
                    >
                    >This is my aspx code:
                    >>
                    ><asp:DetailsVi ew ID="DetailsView 1" runat="server" >
                    ><asp:TemplateF ield>
                    ><EditItemTempl ate>
                    ><asp:TextBox ID="TextBox1" runat="server"> </asp:TextBox>
                    ></EditItemTemplat e>
                    ></asp:TemplateFie ld>
                    ></asp:DetailsView >
                    >>
                    >and javascript:
                    >>
                    ><script type="text/javascript">
                    >var txt=document.ge tElementById('< %=TextBox1.Clie ntID%>')
                    >>
                    >With this, i get the error: 'TextBox1' is not declared' and i don't see a
                    >difference between 'TextBox1' and 'TextBox1'.
                    >It is surely my problem, but i post this for help. Thanks
                    >
                    Look at your previous post - you wrote:
                    >
                    "Textbox1 is embedded in detailsview1"
                    >
                    Yet the DetailsView is not called "detailsvie w1", nor does it contain a
                    TextBox called "Textbox1"
                    >
                    If you do a View Source, what is the munged name of the DetailsView and
                    what is the name of the munged TextBox? Please be accurate...
                    >
                    >
                    --
                    Mark Rae
                    ASP.NET MVP
                    http://www.markrae.net

                    Comment

                    • Mark Rae [MVP]

                      #11
                      Re: how to get ID of embedded control in javascript?

                      "Dan" <nm@zszsz.xvwro te in message
                      news:uGjkeOhLJH A.1160@TK2MSFTN GP04.phx.gbl...
                      >If you do a View Source, what is the munged name of the DetailsView and
                      >what is the name of the munged TextBox? Please be accurate...
                      >
                      In the page source, I see: 'DetailsView1_T extBox1'
                      >
                      var txt=document.ge tElementById('D etailsView1_Tex tBox1')
                      >
                      This line works. All I wanted to know is whether there was a better way
                      (using ClientID) to get the ID. I find this method (searching in source) a
                      little bit too basic ..
                      How about this:

                      document.getEle mentById('<%=De tailsView1.Find Control("TextBo x1").ClientID%> ');


                      --
                      Mark Rae
                      ASP.NET MVP


                      Comment

                      • Dan

                        #12
                        Re: how to get ID of embedded control in javascript?

                        This gives the error:
                        Object reference not set to an instance of an object


                        "Mark Rae [MVP]" <mark@markNOSPA Mrae.netschreef in bericht
                        news:OP2a5YhLJH A.4796@TK2MSFTN GP05.phx.gbl...
                        "Dan" <nm@zszsz.xvwro te in message
                        news:uGjkeOhLJH A.1160@TK2MSFTN GP04.phx.gbl...
                        >
                        >>If you do a View Source, what is the munged name of the DetailsView and
                        >>what is the name of the munged TextBox? Please be accurate...
                        >>
                        >In the page source, I see: 'DetailsView1_T extBox1'
                        >>
                        >var txt=document.ge tElementById('D etailsView1_Tex tBox1')
                        >>
                        >This line works. All I wanted to know is whether there was a better way
                        >(using ClientID) to get the ID. I find this method (searching in source)
                        >a little bit too basic ..
                        >
                        How about this:
                        >
                        document.getEle mentById('<%=De tailsView1.Find Control("TextBo x1").ClientID%> ');
                        >
                        >
                        --
                        Mark Rae
                        ASP.NET MVP
                        http://www.markrae.net

                        Comment

                        • Mark Rae [MVP]

                          #13
                          Re: how to get ID of embedded control in javascript?

                          "Dan" <nm@zszsz.xvwro te in message
                          news:ePPMudhLJH A.2348@TK2MSFTN GP05.phx.gbl...
                          >How about this:
                          >>
                          >document.getEl ementById('<%=D etailsView1.Fin dControl("TextB ox1").ClientID% >');
                          >
                          This gives the error:
                          Object reference not set to an instance of an object
                          Hmm - OK.

                          This discussion looks similar to your problem:
                          A broad category of Microsoft tools, languages, and frameworks for software development. Designed to support developers in building, debugging, and deploying applications across various platforms.



                          --
                          Mark Rae
                          ASP.NET MVP


                          Comment

                          • Dan

                            #14
                            Re: how to get ID of embedded control in javascript?

                            Do you mean you give up?
                            Is this so a weird problem?
                            Anyway, thanks for your time ...
                            I'll do it by searching in the code.

                            "Mark Rae [MVP]" <mark@markNOSPA Mrae.netschreef in bericht
                            news:%23M78kthL JHA.740@TK2MSFT NGP03.phx.gbl.. .
                            "Dan" <nm@zszsz.xvwro te in message
                            news:ePPMudhLJH A.2348@TK2MSFTN GP05.phx.gbl...
                            >
                            >>How about this:
                            >>>
                            >>document.getE lementById('<%= DetailsView1.Fi ndControl("Text Box1").ClientID %>');
                            >>
                            >This gives the error:
                            >Object reference not set to an instance of an object
                            >
                            Hmm - OK.
                            >
                            This discussion looks similar to your problem:
                            A broad category of Microsoft tools, languages, and frameworks for software development. Designed to support developers in building, debugging, and deploying applications across various platforms.

                            >
                            >
                            --
                            Mark Rae
                            ASP.NET MVP
                            http://www.markrae.net

                            Comment

                            • Mark Rae [MVP]

                              #15
                              Re: how to get ID of embedded control in javascript?

                              "Dan" <nm@zszsz.xvwro te in message
                              news:eJRLF%23hL JHA.4324@TK2MSF TNGP05.phx.gbl. ..
                              >>>How about this:
                              >>>>
                              >>>document.get ElementById('<% =DetailsView1.F indControl("Tex tBox1").ClientI D%>');
                              >>>
                              >>This gives the error:
                              >>Object reference not set to an instance of an object
                              >>
                              >Hmm - OK.
                              >>
                              >This discussion looks similar to your problem:
                              >http://forums.asp.net/t/1075056.aspx
                              >
                              Do you mean you give up?
                              Not at all.
                              Is this so a weird problem?
                              No idea - it worked fine for me...

                              Was the DetailsView definitely in Edit mode when you were trying to
                              reference the TextBox...?


                              --
                              Mark Rae
                              ASP.NET MVP


                              Comment

                              Working...