|
-
Aug 2nd, 2012, 01:10 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Dynamically created controls not accessible with client javascript
Hi,
I´ve tried creating checkboxes, literalcontrols and textboxes.
I need to generate checkboxes dynamically, depending on how many types of <whatever> the user has put in. I know how to do this (I think), but the problem is the controls don´t work as they should.
If I create server controls, then for some reason I can´t change the value with javascript. If I create client controls, then something appears to happen with the id so the server can´t fetch the value (it doesn´t recognize the id, allthough it´s there in the html)
I can´t use a regular checkbox with autopostback because there is to much happening at page load.
On other pages on the site, this works, but on those pages the controls are not created dynamically.
Thanks.
Fuga.
Visual Studio 2010 xpress, Visual Studio 2008 pro, SQL Server, SQL Server management studio 2008 r2, MSAccess
-
Aug 3rd, 2012, 06:56 AM
#2
Re: Dynamically created controls not accessible with client javascript
Hello Fuga,
In order to best help you with this, we are really going to need to see the code that you are currently using.
Is it possible that you can share this with us?
Gary
-
Aug 3rd, 2012, 11:33 AM
#3
Fanatic Member
Re: Dynamically created controls not accessible with client javascript
Look at the Id values on the rendered HTML page. Are they what you expect them to be?
And then post the offending code, as Gary suggested.
-
Aug 3rd, 2012, 04:49 PM
#4
Thread Starter
Hyperactive Member
Re: Dynamically created controls not accessible with client javascript
Hi guys,
Thanks for answering!
yes of course
Look at the Id values on the rendered HTML page. Are they what you expect them to be?
yes, in the html. But in the runtime asp code (below) they turn up as nothing, which means I can´t get to them in the code.
The creation of controls, this is page load:
Code:
Me.comCalc.Controls.Add(New LiteralControl("<input type=""checkbox"" onclick=""cl('" & pivrw.Item("SalID") & "')"" id=""" & pivrw.Item("SalID") & """ /><label id=""lb" & i & """ title=""" & pivrw.Item("SalID") & """ class=""comlabel"" type=""text"" style=""width:100px;color:black""/>" & Trim(pivrw.Item("SalTitle")) & "</label><div style=""clear:both""></div>"))
Me.comCalc.Controls.Add(New TextBox() With {.ID = "txb" & pivrw.Item("SalID"), .Text = Trim(pivrw.Item("SalID")), .CssClass = "cbtext"})
Me.comCalc.Controls.Add(New LiteralControl("<input id=""cp" & pivrw.Item("SalID") & """ type=""text"" value=""1"" runat=""server"" />"))
and then the fetching. it´s a button event (this gives id=nothing. it finds the controls and recognises them as literalcontrols):
Code:
For Each ctl As Control In Me.comCalc.Controls
If TypeOf ctl Is LiteralControl Then
If Left(ctl.ID, 2) = "cp" Then
Dim icp As New HtmlInputText
icp = ctl
Checkboxes(ctli) = CInt(icp.Value)
ctli = ctli + 1
End If
ctlindex = ctlindex + 1
End If
Next
and the javascript (this works, in that it changes the value from 1 to 0 and vice versa, allthough not in the html, but I guess that´s not surprising)
Code:
function cl(salid) {
if (document.getElementById("cp" + salid).value == "0") {
document.getElementById("cp" + salid).value = "1"
}
else {
document.getElementById("cp" + salid).value = "0"
}
}
edit: it´s the "cp"-control I need to use the value of when I press the button.
Thanks again
Fuga.
Visual Studio 2010 xpress, Visual Studio 2008 pro, SQL Server, SQL Server management studio 2008 r2, MSAccess
-
Aug 8th, 2012, 01:35 AM
#5
Re: Dynamically created controls not accessible with client javascript
Hello,
The problem here is that you are not accessing the controls that had been created when the page first loads, you are accessing "new" controls when the page is posted back.
Using dynamically created controls can be tricky, and you need to make sure that you use the right event handlers for accessing existing controls.
I would highly recommend that you read through this article:
http://www.4guysfromrolla.com/articles/092904-1.aspx
Hope that helps!
Gary
-
Aug 13th, 2012, 01:28 PM
#6
Re: Dynamically created controls not accessible with client javascript
You can get the "real" ID of a dynamicly created control using control.ClientID.
-
Aug 20th, 2012, 05:00 AM
#7
Thread Starter
Hyperactive Member
Re: Dynamically created controls not accessible with client javascript
Hi again,
and thanks guys for answering.
Gary, thanks for the tip on the article. I´ve read it, and then put the code in the init event handler instead of the load event handler, and so far it works. Thanks!
I´m marking it resolved.
Fuga
Visual Studio 2010 xpress, Visual Studio 2008 pro, SQL Server, SQL Server management studio 2008 r2, MSAccess
-
Aug 20th, 2012, 06:32 AM
#8
Re: [RESOLVED] Dynamically created controls not accessible with client javascript
Great, glad to hear that you got it working!
Gary
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|