jscript can't reference textbox in user control

Discussion in 'ASP .Net Web Controls' started by moondaddy, Jan 21, 2004.

  1. moondaddy

    moondaddy Guest

    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>





    --
     
    moondaddy, Jan 21, 2004
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:%23%...
    > 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>
    >
    >
    >
    >
    >
    > --
    >
    >
    >
     
    Victor Garcia Aprea [MVP], Jan 22, 2004
    #2
    1. Advertising

  3. moondaddy

    moondaddy Guest

    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]" <> wrote in message
    news:...
    > 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" <> wrote in message
    > news:%23%...
    > > 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>
    > >
    > >
    > >
    > >
    > >
    > > --
    > >
    > >
    > >

    >
    >
     
    moondaddy, Jan 23, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Henke
    Replies:
    4
    Views:
    3,530
    Henke
    Aug 30, 2004
  2. Jon Maz
    Replies:
    4
    Views:
    1,315
    Jon Maz
    Sep 9, 2004
  3. Maxwell2006
    Replies:
    5
    Views:
    2,434
    Steven Cheng[MSFT]
    Mar 7, 2006
  4. John Bentley
    Replies:
    14
    Views:
    385
    Jim Ley
    Feb 5, 2004
  5. VK
    Replies:
    4
    Views:
    376
    Thomas 'PointedEars' Lahn
    Jan 12, 2006
Loading...

Share This Page