Anatoli
Anatoli

Reputation: 932

How do I access the ASP control within span through span ID?

I use custom control which creates a span and 2 asp controls with int.

<abc:edf runat="server" ID="testing" ... />

Now my questions is how do I access the asp control within span through span ID in javascript?

I can get to it by

var test = $get('<%=testing.ClientID %>');
alert(test.all.[hard coded client ID of inner asp control].value)

but obviously I don't want to hard code the client ID so is there any better way to handle this?

Upvotes: 0

Views: 215

Answers (1)

jdavies
jdavies

Reputation: 12894

You could expose the controls ClientID as a public property like so:

public class TestControl : CompositeControl
{
    private TextBox textBox;

    public string TextBoxClientID
    {
        get
        {
            return textBox.ClientID;
        }
    }

    protected override void CreateChildControls()
    {
        textBox = new TextBox();
        textBox.ID = "textBox1";
        Controls.Add(textBox);
    }
}

That way you can access it in your code blocks:

<%= TestControl1.TextBoxClientID %>

Upvotes: 3

Related Questions