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
  • check sender.getAttribute("id") Commented Apr 14, 2014 at 13:06
  • @RandRandom alert(sender.clientid) returns an 'undefined' Commented Apr 14, 2014 at 13:06
  • 1
    Need to create the client ID in a predictable way. MSDN Video Commented Apr 14, 2014 at 13:08
  • Check clientid spelled correctly :) msdn.microsoft.com/de-de/library/… Commented Apr 14, 2014 at 13:12

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

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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 Comments

Ah, ok... ID in asp.net = ClientID in javascript. Thanks, that helped. :)
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.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.