jscript can't reference textbox in user control

M

moondaddy

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>
 
V

Victor Garcia Aprea [MVP]

Hi,

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

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
 
M

moondaddy

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 said:
Hi,

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,231
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top