getelementbyid cant find runtime id

B

Beemer Biker

I am adding at bindtime an htmlbutton and an html dropdownlist. The idea is
to select the item in the list, hit the button and my callback code uses the
ID.selectedindex to act on the item that was selected. I tested it all out
by dropping a button and a DDL on a form. Worked ok. Then I wrote the code
that automatically added the the button and the DDL to a templated column.
The button callback works just fine, but document.getelementbyid returns
null. I know how to make this work if I replace the client side objects
with server side. I just thought I could do it on the client side and avoid
postbacks. I guess I could store the value of the DDL during an onchange
somewhere and then the button would get that value (or 0 if nothing was
changed???)

Is there a better way to handle this? Seems there should be some way the
button callback could get the value of the index that was selected. Maybe I
am doing something wrong?

...TIA..
 
E

Eliyahu Goldin

If you view the html source of your page, you will see that the ddl's id is
not what you think It is made out of parts. You may want to correct the id
you are passing to getElementById to include all the parts.
 
B

Beemer Biker

Eliyahu Goldin said:
If you view the html source of your page, you will see that the ddl's id
is not what you think It is made out of parts. You may want to correct the
id you are passing to getElementById to include all the parts.

Thanks Eliyahu, shortly after posting this topic, like about 30 seconds
later, I read the same response to a similar post by another individual
working with radio buttons (instead of drop down list)
I didnt realize the the actual id's were different than the ones I assigned.
I had coded something like "onclick="ProcessSelected('ddl_id')" but I see
now that ddl_id would never have have worked because at a minimum there are
more than one of them in the grid plus the names are assigned as you say.

What if I passed "this" instead of the ddl_id. I am at home and not at work
where I got VS8 so I cant run a quick test. Would this.id maybe get the
correct id?
There is another discussion here FWIW (e-mail address removed)

...thanks..
 
B

Beemer Biker

going to answer my own question:
I had coded something like "onclick="ProcessSelected('ddl_id')" but I see
now that ddl_id would never have have worked because at a minimum there
are more than one of them in the grid plus the names are assigned as you
say.

What if I passed "this" instead of the ddl_id. I am at home and not at
work where I got VS8 so I cant run a quick test. Would this.id maybe get
the correct id?

"this" would be the button object, not the ddl object so this.id would not
get me the value of the selected index
 
M

Mark Rae

Do it this way, and you'll never be bothered by this problem again:

<asp:Button ID="MyButton" runat="server" Text="Test"
OnClientClick="alert('<%=MyButton.ClientID%>'); />
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top