Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > jscript can't reference textbox in user control

Reply
Thread Tools

jscript can't reference textbox in user control

 
 
moondaddy
Guest
Posts: n/a
 
      01-21-2004
I'm running winXP and vb.net 1.1: I want some JavaScript to add a value to
a textbox server control in a web user control. The below code works in an
aspx page, but on in an ascx page. can anyone please advise?

Thanks in advance!

<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="zzTestTextBox.aspx.vb" Inherits="CharmPix.zzTestTextBox"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>zzTestTextBox</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
<script type="text/javascript">

function TestFuncInUC(){
alert('TestFuncInUC')
//txtID.value='123' //this didnt work in the UC either.
var txt;
txt = document.getElementById("txtID");
txt.value='123';
}
</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<INPUT style="Z-INDEX: 101; LEFT: 48px; WIDTH: 130px; POSITION: absolute;
TOP: 34px; HEIGHT: 44px"
type="button" value="Button" onclick="TestFuncInUC()">

<asp:textbox id="txtID" runat="server"></asp:textbox>

</form>
</body>
</HTML>





--
http://www.velocityreviews.com/forums/(E-Mail Removed)


 
Reply With Quote
 
 
 
 
Victor Garcia Aprea [MVP]
Guest
Posts: n/a
 
      01-22-2004
Hi,

The problem is that you've hardcoded the client id:

>>> txt = document.getElementById("txtID");


This will work on page but not when added to a control that is a naming
container as the client id will change to ParentControlID_ChildControlID.
You can use the ClientID property to get the exact ID that will be rendered
to the client instead of hardcoding it.

--
Victor Garcia Aprea
Microsoft MVP | ASP.NET
Looking for insights on ASP.NET? Read my blog:
http://obies.com/vga/blog.aspx
To contact me remove 'NOSPAM'. Please post all questions to the newsgroup

"moondaddy" <(E-Mail Removed)> wrote in message
news:%23%(E-Mail Removed)...
> I'm running winXP and vb.net 1.1: I want some JavaScript to add a value

to
> a textbox server control in a web user control. The below code works in

an
> aspx page, but on in an ascx page. can anyone please advise?
>
> Thanks in advance!
>
> <%@ Page Language="vb" AutoEventWireup="false"
> Codebehind="zzTestTextBox.aspx.vb" Inherits="CharmPix.zzTestTextBox"%>
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
> <HTML>
> <HEAD>
> <title>zzTestTextBox</title>
> <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
> <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
> <meta name="vs_defaultClientScript" content="JavaScript">
> <meta name="vs_targetSchema"
> content="http://schemas.microsoft.com/intellisense/ie5">
> <script type="text/javascript">
>
> function TestFuncInUC(){
> alert('TestFuncInUC')
> //txtID.value='123' //this didnt work in the UC either.
> var txt;
> txt = document.getElementById("txtID");
> txt.value='123';
> }
> </script>
> </HEAD>
> <body MS_POSITIONING="GridLayout">
> <form id="Form1" method="post" runat="server">
> <INPUT style="Z-INDEX: 101; LEFT: 48px; WIDTH: 130px; POSITION:

absolute;
> TOP: 34px; HEIGHT: 44px"
> type="button" value="Button" onclick="TestFuncInUC()">
>
> <asp:textbox id="txtID" runat="server"></asp:textbox>
>
> </form>
> </body>
> </HTML>
>
>
>
>
>
> --
> (E-Mail Removed)
>
>



 
Reply With Quote
 
 
 
 
moondaddy
Guest
Posts: n/a
 
      01-23-2004
Thanks. Your recommendation led to this solution:

In the Main aspx page's Page_Load event I input the code below. This page
has 2 user controls and the first one needs to send an ID value to the
second one (named ItemGrid_Standard1). the control ItemGrid_Standard1 has
a server control textbox named txtID and a server control button named
btnPostBack. By generating this jscript on the server for the client I was
able to reference the controls in the 2nd UC from the first one.

In the first UC I have a table with this html element and onclick event to
pass the ID value:
<TD><A id="lnkMenu_Planets" href="#"
onclick="GetMenuID('201')">TestMe</A></TD>


'Code in 2nd UC (receiving the param)
Dim uc As ItemGrid_Standard1 = Page.FindControl("ItemGrid_Standard1")
Dim txt As TextBox = uc.FindControl("txtID")
Dim btn As HtmlInputButton = uc.FindControl("btnPostBack")

Dim scriptString As String
scriptString = vbTab & "<script language=jscript type=text/Jscript>" &
vbNewLine
scriptString += vbTab & vbTab & "function GetMenuID(value)" & vbNewLine
scriptString += vbTab & vbTab & vbTab & "{" & vbNewLine
scriptString += vbTab & vbTab & vbTab & txt.ClientID & " =value;" &
vbNewLine
scriptString += vbTab & vbTab & vbTab & "document.all('" & txt.ClientID &
"').value =value;" & vbNewLine
scriptString += vbTab & vbTab & vbTab & "document.all('" & btn.ClientID &
"').click();" & vbNewLine
scriptString += vbTab & vbTab & vbTab & "}" & vbNewLine
scriptString += vbTab & vbTab & "</script>"

'RegisterClientScriptBlock("clientScript", scriptString)
If (Not Page.IsClientScriptBlockRegistered("clientScript") ) Then
Page.RegisterClientScriptBlock("clientScript", scriptString)
End If












"Victor Garcia Aprea [MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi,
>
> The problem is that you've hardcoded the client id:
>
> >>> txt = document.getElementById("txtID");

>
> This will work on page but not when added to a control that is a naming
> container as the client id will change to ParentControlID_ChildControlID.
> You can use the ClientID property to get the exact ID that will be

rendered
> to the client instead of hardcoding it.
>
> --
> Victor Garcia Aprea
> Microsoft MVP | ASP.NET
> Looking for insights on ASP.NET? Read my blog:
> http://obies.com/vga/blog.aspx
> To contact me remove 'NOSPAM'. Please post all questions to the newsgroup
>
> "moondaddy" <(E-Mail Removed)> wrote in message
> news:%23%(E-Mail Removed)...
> > I'm running winXP and vb.net 1.1: I want some JavaScript to add a value

> to
> > a textbox server control in a web user control. The below code works in

> an
> > aspx page, but on in an ascx page. can anyone please advise?
> >
> > Thanks in advance!
> >
> > <%@ Page Language="vb" AutoEventWireup="false"
> > Codebehind="zzTestTextBox.aspx.vb" Inherits="CharmPix.zzTestTextBox"%>
> > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
> > <HTML>
> > <HEAD>
> > <title>zzTestTextBox</title>
> > <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
> > <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
> > <meta name="vs_defaultClientScript" content="JavaScript">
> > <meta name="vs_targetSchema"
> > content="http://schemas.microsoft.com/intellisense/ie5">
> > <script type="text/javascript">
> >
> > function TestFuncInUC(){
> > alert('TestFuncInUC')
> > //txtID.value='123' //this didnt work in the UC either.
> > var txt;
> > txt = document.getElementById("txtID");
> > txt.value='123';
> > }
> > </script>
> > </HEAD>
> > <body MS_POSITIONING="GridLayout">
> > <form id="Form1" method="post" runat="server">
> > <INPUT style="Z-INDEX: 101; LEFT: 48px; WIDTH: 130px; POSITION:

> absolute;
> > TOP: 34px; HEIGHT: 44px"
> > type="button" value="Button" onclick="TestFuncInUC()">
> >
> > <asp:textbox id="txtID" runat="server"></asp:textbox>
> >
> > </form>
> > </body>
> > </HTML>
> >
> >
> >
> >
> >
> > --
> > (E-Mail Removed)
> >
> >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is Server-Side Jscript (not Jscript.NET)? Maxwell2006 ASP .Net 5 03-07-2006 05:28 AM
JScript.Net (JScript 7.0) is official documented language VK Javascript 4 01-12-2006 06:48 PM
Porting from JScript to JScript.Net - compiler error Jon Maz ASP .Net 4 09-09-2004 10:24 AM
Which to Learn: Javascript, Jscript, JScript.NET, ECMA 262 (3rd ed)? John Bentley Javascript 14 02-05-2004 06:14 PM
Re: VERY STRANGE BUG? Adding a textbox control causes other textbox control to fail??? S. Justin Gengo ASP .Net 0 07-16-2003 06:51 PM



Advertisments