passing clientid t javascript from aspx declaration

A

Aamir Ghanchi

Hi, I am wondering why the <%= % is not working when I use it to pass
the clientid to a javascrip function from the aspx declaration code.

Say, I have a listbox webcontrol and from its client-side javascript
eventhandler attribute, I try to pass on the client id as follows

<asp:ListBox ID="myListBox"
runat="server"
onchange="lst_onchange('<
%=myListBox.ClientID%>')" >
</asp:ListBox>

In the javascript as follows it will not show the actual clientid, but
will pass on the whole paramater without evaluating it simply as <
%=myListBox.ClientID%>

function lst_onchange(lstBoxID){
alert(listboxID)
}
 
A

Aamir Ghanchi

Hi, I am wondering why the <%= % is not working when I use it to pass
the clientid to a javascrip function from the aspx declaration code.

Say, I have a listbox webcontrol and from its client-side javascript
eventhandler attribute, I try to pass on the client id as follows

<asp:ListBox ID="myListBox"
runat="server"
onchange="lst_onchange('<
%=myListBox.ClientID%>')" >
</asp:ListBox>

In the javascript as follows it will not show the actual clientid, but
will pass on the whole paramater without evaluating it simply as <
%=myListBox.ClientID%>

function lst_onchange(lstBoxID){
alert(listboxID)



}- Hide quoted text -

- Show quoted text -

me again.
Never mind. I have done it in code behind pageload event handler by
adding the clientside ebenthandler to the listbox's attributes
collection and its working fine. But still, I would like to hear why
is it not possible to use <%=%> or <% Response.write()%> in the
clientside event handler attribute of a webcontrol.

thanks.
 
B

bruce barker

you can not use <%= %> with server control properties, only databinding
expressions (<%# %>), but you must databind.

in your case you need neither, just use:

<asp:ListBox
ID="myListBox"
runat="server"
onchange="lst_onchange(this);" >
</asp:ListBox>

function lst_onchange(e){
alert(e.id);
}

-- bruce (sqlwork.com)
 
A

Aamir Ghanchi

you can not use <%= %> with server control properties, only databinding
expressions (<%# %>), but you must databind.

in your case you need neither, just use:

<asp:ListBox
ID="myListBox"
runat="server"
onchange="lst_onchange(this);" >
</asp:ListBox>

function lst_onchange(e){
alert(e.id);
}

-- bruce (sqlwork.com)









- Show quoted text -

Thanks Bruce. I guess I oversimplified my example for this newspost.
I was aware of the "this" object in javascript which can be to
retrieve the reference to the element from which the eventhandler is
being called. But, I had another parameter in there which was an id
property of another webcontrol on the form.

Any ways, thanks very much for confirming that <%= %> cannot be used
from within the webcontrol attribute value.
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top