Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. FindControl("id"); does not work - another alternative?

FindControl("id"); does not work - another alternative?

Scheduled Pinned Locked Moved ASP.NET
helpquestion
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lvq684
    wrote on last edited by
    #1

    Hi. I have a problem with selecting a control from codebehind. This is my scenario: 1. i get a string from a webservice (session): string boxstring = "<input type="text" id="MainContent_TextBoxStopRoadName01" name="ctl00$MainContent$TextBoxStopRoadName01">"; 2. i insert this string to a <div id="boxcontainer"> from codebehind like this: boxcontainer.InnerHtml=boxstring; 3. Page loads 4. User types text in the box, and clicks a button to submit it 5. How do i find this textfield from codebehind from its id? I would like to get the field as an TextBox control, but the text value is the most important. I´ve tried several things, like Findcontrol("id") and Findcontrol("id").Findcontrol("id") and so on. But it doesnt work. Another thing does work though, if instead of a string, I create the textfield as an TextBox control, and then insert it: BoxContainer.Controls.Add(TextBox); - then it works! But it is not a possible solution in my situation. I only have the textbox (among many other elements) in a string. Really hope you can help! Thanks :)

    M E D N 4 Replies Last reply
    0
    • L lvq684

      Hi. I have a problem with selecting a control from codebehind. This is my scenario: 1. i get a string from a webservice (session): string boxstring = "<input type="text" id="MainContent_TextBoxStopRoadName01" name="ctl00$MainContent$TextBoxStopRoadName01">"; 2. i insert this string to a <div id="boxcontainer"> from codebehind like this: boxcontainer.InnerHtml=boxstring; 3. Page loads 4. User types text in the box, and clicks a button to submit it 5. How do i find this textfield from codebehind from its id? I would like to get the field as an TextBox control, but the text value is the most important. I´ve tried several things, like Findcontrol("id") and Findcontrol("id").Findcontrol("id") and so on. But it doesnt work. Another thing does work though, if instead of a string, I create the textfield as an TextBox control, and then insert it: BoxContainer.Controls.Add(TextBox); - then it works! But it is not a possible solution in my situation. I only have the textbox (among many other elements) in a string. Really hope you can help! Thanks :)

      M Offline
      M Offline
      Manas Bhardwaj
      wrote on last edited by
      #2

      The documentation for FindControl says: Searches the page naming container for a server control with the specified identifier. http://msdn.microsoft.com/en-us/library/system.web.ui.control.findcontrol(v=VS.71).aspx Hope this helps.

      Manas Bhardwaj Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.

      1 Reply Last reply
      0
      • L lvq684

        Hi. I have a problem with selecting a control from codebehind. This is my scenario: 1. i get a string from a webservice (session): string boxstring = "<input type="text" id="MainContent_TextBoxStopRoadName01" name="ctl00$MainContent$TextBoxStopRoadName01">"; 2. i insert this string to a <div id="boxcontainer"> from codebehind like this: boxcontainer.InnerHtml=boxstring; 3. Page loads 4. User types text in the box, and clicks a button to submit it 5. How do i find this textfield from codebehind from its id? I would like to get the field as an TextBox control, but the text value is the most important. I´ve tried several things, like Findcontrol("id") and Findcontrol("id").Findcontrol("id") and so on. But it doesnt work. Another thing does work though, if instead of a string, I create the textfield as an TextBox control, and then insert it: BoxContainer.Controls.Add(TextBox); - then it works! But it is not a possible solution in my situation. I only have the textbox (among many other elements) in a string. Really hope you can help! Thanks :)

        E Offline
        E Offline
        Espen Harlinn
        wrote on last edited by
        #3

        A page on the server is a transient object recreated whenever the browser requests the page. That's true for postbacks and callbacks too

        1 Reply Last reply
        0
        • L lvq684

          Hi. I have a problem with selecting a control from codebehind. This is my scenario: 1. i get a string from a webservice (session): string boxstring = "<input type="text" id="MainContent_TextBoxStopRoadName01" name="ctl00$MainContent$TextBoxStopRoadName01">"; 2. i insert this string to a <div id="boxcontainer"> from codebehind like this: boxcontainer.InnerHtml=boxstring; 3. Page loads 4. User types text in the box, and clicks a button to submit it 5. How do i find this textfield from codebehind from its id? I would like to get the field as an TextBox control, but the text value is the most important. I´ve tried several things, like Findcontrol("id") and Findcontrol("id").Findcontrol("id") and so on. But it doesnt work. Another thing does work though, if instead of a string, I create the textfield as an TextBox control, and then insert it: BoxContainer.Controls.Add(TextBox); - then it works! But it is not a possible solution in my situation. I only have the textbox (among many other elements) in a string. Really hope you can help! Thanks :)

          D Offline
          D Offline
          DoctorMick
          wrote on last edited by
          #4

          I can't see any way of getting the control because it isn't a part of the pages controls but you should be able to get the value from the Request.Form collection after postback.

          1 Reply Last reply
          0
          • L lvq684

            Hi. I have a problem with selecting a control from codebehind. This is my scenario: 1. i get a string from a webservice (session): string boxstring = "<input type="text" id="MainContent_TextBoxStopRoadName01" name="ctl00$MainContent$TextBoxStopRoadName01">"; 2. i insert this string to a <div id="boxcontainer"> from codebehind like this: boxcontainer.InnerHtml=boxstring; 3. Page loads 4. User types text in the box, and clicks a button to submit it 5. How do i find this textfield from codebehind from its id? I would like to get the field as an TextBox control, but the text value is the most important. I´ve tried several things, like Findcontrol("id") and Findcontrol("id").Findcontrol("id") and so on. But it doesnt work. Another thing does work though, if instead of a string, I create the textfield as an TextBox control, and then insert it: BoxContainer.Controls.Add(TextBox); - then it works! But it is not a possible solution in my situation. I only have the textbox (among many other elements) in a string. Really hope you can help! Thanks :)

            N Offline
            N Offline
            Not Active
            wrote on last edited by
            #5

            lvq684 wrote:

            1. i get a string from a webservice (session): string boxstring = "";

            This really doesn't make sense to me. What is the purpose of this webservice? The html is useless, it doesn't have any context. Since the control is being added to the innerHtml of the div at runtime it is not available during postback; it isn't part of the page's control collection. You could accomplish what you are asking by using a Panel in place of the div, or adding the runat=sever attribute to the div you have

            TextBox txt = new TextBox();
            div.Controls.Add(txt);

            Since it is being added dynamically you must recreate the control during postback but then you could access it's value.


            I know the language. I've read a book. - _Madmatt

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups