Accessing Server Control Value from Client-Side
Messages   Related Types
This message was discovered on ASPFriends.com 'aspngcontrolscs' list.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.

Strauss, Jon (VIP)
Hi,

I've created a composite server control that includes a textbox and a validator. However, I can't figure out how to access the textbox value from client-side javascript. The problem seems to be that the client id that get's generated doesn't match the id that I use when I declare my server control in my aspx page. So, when I try to access the control in javascript, I get "object is null or not an object". I also tried just deriving my control from the System.Web.UI.WebControls.TextBox, but I couldn't figure out a way to add a validator (without manually adding the equivalent javascript which seemed to defeat the purpose of using server controls).

I guess my question boils down to this:

*** Is there any way to access the value of one of the controls in a composite server control on the client-side? ***

Since I can do this with a TextBox server control (<asp:TextBox />), I figure there must be a way to do it with a composite control that contains a textbox. I just can't figure out how.

Any help would be greatly appreciated!

Thanks,
Jon

Here's some sample code of what I'm essentially trying to do...

.aspx page:

<%@ Register TagPrefix="Util" Namespace="MyControlLib" Assembly="MyControlLib" %>
<html>
<head>
<script language=javascript>
function TestAccess()
{
        //this get's called, but displays "MyForm.cCompCtrl.value is null or not an object"
        alert(MyForm.cCompCtrl.value);
}
</script>
</head>
<body MS_POSITIONING="GridLayout">
    
<form id="MyForm" method="post" runat="server">
    
    <Util:TextCompControl runat=server id=cCompCtrl
        onchange="javascript:TestAccess();" />

    </form>
    
</body>
</html>

And here's my control:

using System;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MyControlLib
{
    /// <summary>
    /// Summary description for TextCompControl.
    /// </summary>
    [ValidationPropertyAttribute("Text")]
    public class TextCompControl: WebControl, INamingContainer
    {
        private TextBox m_TextBox = new TextBox();
        private string m_OnChangeFunc;

        public TextCompControl()
        {
            m_OnChangeFunc = null;
        }

        public string onchange
        {
            set
            {
                m_OnChangeFunc = value;
            }
        }

        public string Text
        {
            get
            {
                return m_TextBox.Text;
            }
            set
            {
                m_TextBox.Text = value;
            }
        }

        protected override void CreateChildControls()
        {
            m_TextBox.ID = this.ClientID;

            if (m_OnChangeFunc != null)
                m_TextBox.Attributes["onchange"] = m_OnChangeFunc;

            this.Controls.Add(m_TextBox);

            CustomValidator cv = new CustomValidator();

            cv.ClientValidationFunction = "CheckVal";
            cv.ControlToValidate = m_TextBox.ID;
            cv.Display = ValidatorDisplay.Static;

            cv.ErrorMessage = "Invalid value";

            this.Controls.Add(cv);
        
        }
    }
}

Reply to this message...
 
    
Andy Smith
The id of an inner control will concatenated with the id of the =
containing control.
This is so you won't get naming conflicts when multiple composite =
controls are placed on the form.

If you give your inner Textbox an ID, then the clientside id attribute =
will be CompositeControlID:TextBoxControlID. However, the name attribute =
will be CompositeControlID_TextBoxControlID.
( I may have those backwards, whatever, you'll see )

many times, control designers don't give the inner control ids, so the =
framework just makes one up.
If you give your control an ID, I don't think it'll be too hard to =
access it from client script.

__
Andy Smith
Chief Code Monkey

-----Original Message-----
From: Strauss, Jon [mailto:Click here to reveal e-mail address]
Sent: Friday, May 31, 2002 10:32 AM
To: aspngcontrolscs
Subject: [aspngcontrolscs] Accessing Server Control Value from
Client-Side

Hi,

I've created a composite server control that includes a textbox and a =
validator. However, I can't figure out how to access the textbox value =
from client-side javascript. The problem seems to be that the client id =
that get's generated doesn't match the id that I use when I declare my =
server control in my aspx page. So, when I try to access the control in =
javascript, I get "object is null or not an object". I also tried just =
deriving my control from the System.Web.UI.WebControls.TextBox, but I =
couldn't figure out a way to add a validator (without manually adding =
the equivalent javascript which seemed to defeat the purpose of using =
server controls). =20

I guess my question boils down to this: =20

*** Is there any way to access the value of one of the controls in a =
composite server control on the client-side? ***

Since I can do this with a TextBox server control (<asp:TextBox />), I =
figure there must be a way to do it with a composite control that =
contains a textbox. I just can't figure out how.

Any help would be greatly appreciated!

Thanks,
Jon

Here's some sample code of what I'm essentially trying to do...

.aspx page:

<%@ Register TagPrefix=3D"Util" Namespace=3D"MyControlLib" =
Assembly=3D"MyControlLib" %>
<html>
<head>
<script language=3Djavascript>
function TestAccess()
{
        //this get's called, but displays "MyForm.cCompCtrl.value is null or =
not an object"
        alert(MyForm.cCompCtrl.value);
}
</script>
</head>
<body MS_POSITIONING=3D"GridLayout">
=09
<form id=3D"MyForm" method=3D"post" runat=3D"server">
=09
    <Util:TextCompControl runat=3Dserver id=3DcCompCtrl=20
        onchange=3D"javascript:TestAccess();" />

    </form>
=09
</body>
</html>

And here's my control:

using System;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MyControlLib
{
    /// <summary>
    /// Summary description for TextCompControl.
    /// </summary>
    [ValidationPropertyAttribute("Text")]
    public class TextCompControl: WebControl, INamingContainer
    {
        private TextBox m_TextBox =3D new TextBox();
        private string m_OnChangeFunc;

        public TextCompControl()
        {
            m_OnChangeFunc =3D null;
        }

        public string onchange
        {
            set
            {
                m_OnChangeFunc =3D value;
            }
        }

        public string Text
        {
            get
            {
                return m_TextBox.Text;
            }
            set
            {
                m_TextBox.Text =3D value;
            }
        }

        protected override void CreateChildControls()=20
        {
            m_TextBox.ID =3D this.ClientID;

            if (m_OnChangeFunc !=3D null)
                m_TextBox.Attributes["onchange"] =3D m_OnChangeFunc;

            this.Controls.Add(m_TextBox);

            CustomValidator cv =3D new CustomValidator();

            cv.ClientValidationFunction =3D "CheckVal";
            cv.ControlToValidate =3D m_TextBox.ID;
            cv.Display =3D ValidatorDisplay.Static;

            cv.ErrorMessage =3D "Invalid value";

            this.Controls.Add(cv);
    =09
        }
    }
}

| [aspngcontrolscs] member Click here to reveal e-mail address =3D YOUR ID
| http://www.asplists.com/asplists/aspngcontrolscs.asp =3D JOIN/QUIT
| http://www.asplists.com/search =3D SEARCH Archives

Reply to this message...
 
 
System.Web.UI.INamingContainer
System.Web.UI.MobileControls.CustomValidator
System.Web.UI.MobileControls.TextBox
System.Web.UI.ValidationPropertyAttribute
System.Web.UI.WebControls.CustomValidator
System.Web.UI.WebControls.TextBox
System.Web.UI.WebControls.ValidatorDisplay
System.Web.UI.WebControls.WebControl
System.Windows.Forms.TextBox




Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
 
 Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us - Terms of Use - Privacy Policy - www.dotnet247.com