简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM