0

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?

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Anatoli
  • 922
  • 2
  • 11
  • 25
  • 1
    http://stackoverflow.com/questions/5666011/retrieve-id-of-server-control-using-jquery has some ideas like using a feature in ASP.Net 4.0 – JB King Sep 16 '11 at 22:53

1 Answers1

3

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 %>
jdavies
  • 12,784
  • 3
  • 33
  • 33