Is it possible to access a dynamically created textbox with a dynamically added javascript? I noticed when you view the html on a page where there are dynamically created controls the dynamic controls don't show up in the html view... even though they show
just fine on the page in normal view. Are the dynamic controls in some other hidden page or encrypted somewhere?
I'm guessing that's related to why I'm having trouble accessing the dynamic controls from my javascript function.
I can see that I did have some bugs in that code. I tried the changes you mentioned but I'm still having the same issue. It just seems that I'm missing something when it comes to accessing dynamic controls. The fact that I can't see the dynamic controls
when I view the html source makes me think I need to do something drastically different to access them with javascript.
If you use IEs View Source you won't see them, but if you use something FireFox's FireBug, or IEs IE Developer Toolbar, you will see the dynamic changes in real time.
Remember: mark posts that helped you as the answer to aid future readers
gbrot
Member
7 Points
17 Posts
dynamic controls accessed by javascript?
Mar 27, 2008 08:39 PM|LINK
Is it possible to access a dynamically created textbox with a dynamically added javascript? I noticed when you view the html on a page where there are dynamically created controls the dynamic controls don't show up in the html view... even though they show just fine on the page in normal view. Are the dynamic controls in some other hidden page or encrypted somewhere?
I'm guessing that's related to why I'm having trouble accessing the dynamic controls from my javascript function.
Here's a snippet of what I'm doing:
TextBox txtDim = new TextBox(); txtDim.ID = "txtDim" + i.ToString(); txtDim.Attributes.Add("onBlur", "DecimalToFeet('" + txtDim.ID + "');"); pnlDims.Controls.Add(txtDim);The javascript function is something like this:
function DecimalToFeet(InputBoxID) { var InputControl = document.getElementById(InputBoxID); psDim = InputControl.value; ..bla bla bla... InputControl.value = psDim; }The javascript works fine when its called by a regular textbox control, but again, it's not doing anything from the dynamic control.Thanks!
javascript ASP.NET 2.0 dynamic controls
whatispunk
Contributor
4074 Points
876 Posts
Re: dynamic controls accessed by javascript?
Mar 27, 2008 09:53 PM|LINK
There are a couple subtle mistakes in your code.
First: You must reference the ClientID property (or UniqueID) of your control in the Javascript.
Second: You must add txtDim to the pnlDims.Controls collection in order for this ClientID to be created.
1 TextBox txtDim = new TextBox();
2 txtDim.ID = "txtDim" + i.ToString();
4 pnlDims.Controls.Add(txtDim);
3 txtDim.Attributes.Add("onBlur", "DecimalToFeet('" + txtDim.ClientID + "');");
Why UpdatePanels Are Dangerous
Why You Should Not Place Your Whole Site In An UpdatePanel
gbrot
Member
7 Points
17 Posts
Re: dynamic controls accessed by javascript?
Mar 28, 2008 02:48 PM|LINK
Thanks for the response,
I can see that I did have some bugs in that code. I tried the changes you mentioned but I'm still having the same issue. It just seems that I'm missing something when it comes to accessing dynamic controls. The fact that I can't see the dynamic controls when I view the html source makes me think I need to do something drastically different to access them with javascript.
whatispunk
Contributor
4074 Points
876 Posts
Re: dynamic controls accessed by javascript?
Mar 28, 2008 04:42 PM|LINK
If you use IEs View Source you won't see them, but if you use something FireFox's FireBug, or IEs IE Developer Toolbar, you will see the dynamic changes in real time.
Why UpdatePanels Are Dangerous
Why You Should Not Place Your Whole Site In An UpdatePanel
whatispunk
Contributor
4074 Points
876 Posts
Re: dynamic controls accessed by javascript?
Mar 28, 2008 04:44 PM|LINK
Try using UniqueID instead of ClientID
Why UpdatePanels Are Dangerous
Why You Should Not Place Your Whole Site In An UpdatePanel
gbrot
Member
7 Points
17 Posts
Re: dynamic controls accessed by javascript?
Mar 28, 2008 05:50 PM|LINK
Using UniqueID fixed it! Thanks, I would have never figured that out.
NC01
All-Star
82559 Points
15430 Posts
MVP
Re: dynamic controls accessed by javascript?
Mar 28, 2008 06:03 PM|LINK
Change to this:
txtDim.Attributes.Add("onBlur", "DecimalToFeet(this);");
And the JavaScript to:
function DecimalToFeet(InputControl)
{
// var InputControl = document.getElementById(InputBoxID);
// Rest of your code...
NC...
gbrot
Member
7 Points
17 Posts
Re: dynamic controls accessed by javascript?
Mar 28, 2008 06:26 PM|LINK
actually that looks more like the right way to do this. It works too! Thanks for helping this javascript noob
NC01
All-Star
82559 Points
15430 Posts
MVP
Re: dynamic controls accessed by javascript?
Mar 31, 2008 12:42 PM|LINK
No problem.
NC...