FindControl("id"); does not work - another alternative?
-
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 :)
-
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 :)
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.
-
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 :)
A page on the server is a transient object recreated whenever the browser requests the page. That's true for postbacks and callbacks too
-
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 :)
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.
-
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 :)
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