0

I have some problems identifying an asp.net control in javascript. Here's a simple variant of my code, maybe you can see an error.

This function creates a set of button + label, it can be called multiple times:

    private void AddRow(Panel pnl)
    {
        var myButton = new Button();
        myButton.Text = "+";
        myButton.Attributes.Add("onclick", "return MyJavascript();");
        myButton.Attributes.Add("somevariable", "FOOBAR!");

        var lblAnzahl = new Label();

        pnl.Controls.Add(myButton);
        pnl.Controls.Add(lblAnzahl);
    }

This is the JS function the button calls:

            function MyJavascript(e) {
                var sender = event.target || event.srcElement;
                var somevar = sender.getAttribute("somevariable");
                alert('somevar: ' + somevar);
                alert('ID: ' + sender.id);
            }

The output is:

somevar: FOOBAR!

ID:

So I guess I get the correct sender, but for some reason the ID is always empty. The easiest way for me would be, if I could somehow set the ID of button and label myself, so that I can give it an unique ID every time AddRow() gets called, so I can easily identify the button and of course the label that belongs to the button.

What am I doing wrong?

4

2 Answers 2

2

You can use myButton.ClientID or add name attribute myButton.Attributes.Add("name", "FOOBAR"); with check to be sure that name is unique and retrieve in Javascript by getElementByName

1
  • myButton.ClientID is readyonly and doesn't return anything either. I've also tried var mybutton = document.getElementsByName("FOOBAR")[0], but then mybutton is undefined.
    – Core_F
    Apr 14, 2014 at 13:17
1

You're not setting an ID in your AddRow function.

Change your function to this:

private void AddRow(Panel pnl, int index)
{
    var myButton = new Button();
    myButton.ID = "button_" + index; // add this line (the index will guarantee a distinct ID)
    myButton.Text = "+";
    myButton.Attributes.Add("onclick", "return MyJavascript();");
    myButton.Attributes.Add("somevariable", "FOOBAR!");

    var lblAnzahl = new Label();

    pnl.Controls.Add(myButton);
    pnl.Controls.Add(lblAnzahl);
}

You can invoke the code like this:

AddRow(pnl, 0);
AddRow(pnl, 1);
AddRow(pnl, 2);
2
  • Ah, ok... ID in asp.net = ClientID in javascript. Thanks, that helped. :)
    – Core_F
    Apr 14, 2014 at 13:20
  • Hum, not quite... Javascript, as a standalone language, does not know about ClientID's. The ClientID terminology is only used in asp.net. The thing is, in asp.net you can't set the ClientID to a control, you have to set the ID value instead. This in turn will make the asp.net framework generate a proper ClientID, which you can then reference from javascript. Apr 14, 2014 at 13:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.