Has anyone out there accessed form controls from a RegisterStartupScript? I'm registering the script on my Page Load event. I have the alert box working but cannot access any data values from controls like the FileUpload, TextBox, Label... I'm trying to
take the contents of the FileUpload TextBox and assign it to another TextBox(txtDummy) that dynamically changes size with regards to file path. This is what I've tried...
Dim onloadScript As New System.Text.StringBuilder()
onloadScript.Append(" var y = (document.getElementById('<%=((TextBox)this.FormView1.FindControl(""txtDummy.ClienID""));%>")
...at least now my alert box displays NULL. When I browse to a file in the FileUpload, the onLoadCall fires and I'm thinking I should see the file path from that control, BUT I don't. Any ideas?
recordc
Member
33 Points
47 Posts
JavaScript -- RegisterStartupScript -- FormView Controls
Mar 18, 2009 04:57 PM|LINK
Has anyone out there accessed form controls from a RegisterStartupScript? I'm registering the script on my Page Load event. I have the alert box working but cannot access any data values from controls like the FileUpload, TextBox, Label... I'm trying to take the contents of the FileUpload TextBox and assign it to another TextBox(txtDummy) that dynamically changes size with regards to file path. This is what I've tried...
Dim onloadScript As New System.Text.StringBuilder()
onloadScript.Append(" var y = (document.getElementById('<%=((TextBox)this.FormView1.FindControl(""txtDummy.ClienID""));%>")
onloadScript.Append(vbCrLf)
onloadScript.Append("alert(y);")
Me.ClientScript.RegisterStartupScript(Me.GetType(), "onLoadCall", onloadScript.ToString())
...any way I code the getElementById function, I get errors like Object not referenced or 'y' is null.
arunkumar200...
Participant
786 Points
118 Posts
Re: JavaScript -- RegisterStartupScript -- FormView Controls
Mar 18, 2009 06:56 PM|LINK
Try this,
onloadScript.Append("var y = document.getElementById('");
onloadScript.Append(((TextBox)this.FormView1.FindControl("txtDummy")).ClientID + "'); alert(y);");
this.ClientScript.RegisterStartupScript(this.GetType(), "onLoadCall", onloadScript.ToString(), true);
Let me know if you have any issues.
Thanks,
Arunkumar
MARK AS ANSWER IF IT IS USEFUL
recordc
Member
33 Points
47 Posts
Re: JavaScript -- RegisterStartupScript -- FormView Controls
Mar 18, 2009 07:59 PM|LINK
Thanks arunkumar2K3. Well, I converted the above code to VB and then worked the syntax to come up with...
onloadScript.Append("var y = document.getElementById('");
onloadScript.Append("DirectCast(Me.FormView1.FindControl(""txtDummy""), TextBox).ClientID" & "'); alert(y);")
Me.ClientScript.RegisterStartupScript(Me.GetType(), "onLoadCall", onloadScript.ToString(), true);
...at least now my alert box displays NULL. When I browse to a file in the FileUpload, the onLoadCall fires and I'm thinking I should see the file path from that control, BUT I don't. Any ideas?
arunkumar200...
Participant
786 Points
118 Posts
Re: JavaScript -- RegisterStartupScript -- FormView Controls
Mar 18, 2009 08:11 PM|LINK
FindControl(""txtDummy"") will not work I think.
Please try FindControl("txtDummy").
Let me know if you have any issues.
Thanks,
ARUNKUMAR
Mark as ANSWER if it is useful
recordc
Member
33 Points
47 Posts
Re: JavaScript -- RegisterStartupScript -- FormView Controls
Mar 18, 2009 08:25 PM|LINK
FindControl("txtDummy") messes with the syntax. Also, when I do a "View Source" on the browser rendered web page, I see...
var y = document.getElementById('DirectCast(Me.FormView1.FindControl("txtDummy"), TextBox).ClientID'); alert(y);
...looks good to me. Right?
Instead of the FileUpload, I set the txtDummy text property to "Yo!". I would like to get that value in the alert box for starters.
arunkumar200...
Participant
786 Points
118 Posts
Re: JavaScript -- RegisterStartupScript -- FormView Controls
Mar 18, 2009 08:30 PM|LINK
DirectCast(Me.FormView1.FindControl("txtDummy"), TextBox).ClientID - this should give the ClientID of the txtDummy.
You should not see this syntax in the pagesource. Because should be evaluvated and replaced with the ClientID.
Thanks,
Arunkumar
recordc
Member
33 Points
47 Posts
Re: JavaScript -- RegisterStartupScript -- FormView Controls
Mar 18, 2009 08:39 PM|LINK
When I remove one set of quotes, I get the blue squiggly line under txtDummy. When I mouse over txtDummy, It states:
Comma, ')', or a valid expression continuation expected.
...Won't compile.
arunkumar200...
Participant
786 Points
118 Posts
Re: JavaScript -- RegisterStartupScript -- FormView Controls
Mar 18, 2009 08:58 PM|LINK
onloadScript.Append(DirectCast(Me.FormView1.FindControl("txtDummy"), TextBox).ClientID & "'); alert(y);")
Use the above line and let me know if you have any issues.
Thanks,
Arunkumar
recordc
Member
33 Points
47 Posts
Re: JavaScript -- RegisterStartupScript -- FormView Controls
Mar 18, 2009 09:04 PM|LINK
No squiggly lines but, now I get an "Object reference not set to an instance of an object" error.
arunkumar200...
Participant
786 Points
118 Posts
Re: JavaScript -- RegisterStartupScript -- FormView Controls
Mar 18, 2009 09:23 PM|LINK
Please send your formview markup. If possible codebehind.
Thanks,
Arunkumar