[RESOLVED]how to access server side control id in javascript?


I have 5 Button in my web page for save image that user uploaded On server.(note that : every Save Button have it’s File Upload Control)

I want (in javaScript code) get id of Button that clicked, and according to it’s id checked that user uploaded image for this Save button?

but I don’t know how to access Button and  file Upload id (that are server side control) in javaScript code?

I thanks any body that guide me! ;)

I think there are few ways you could do. The Button control has an OnClientClick event which you could use to call some js-code for all buttons.


<asp:Button id="button1" OnClientClick="test(button1)" …

function test(b)

or set id using this.id property

<asp:Button id="button1" OnClientClick="test(this.id)" …
function test(b)

To get value of FileUpload

var fu1 = document.getElementById("<%= FileUpload1.ClientID %>");
alert("You selected " + fu1.value);

where FileUpload1 is your asp.net id of control.

Thanks for your guide. I wonder!!! why this code in javaScript don’t work?

var elementId=$(this).attr("id");

Or this:

var elementId=$(this).id;

$(this) and this aren’t the same. The first represents a jQuery object wrapped around your element. The second is just your element. The id property exists on the element, but not the jQuery object. Maybe you could do

$(function () {
    $("#button2").click(function (e) {

where #button2 is your button.

Demo: http://jsbin.com/wutofapola/1/ 

Read more: http://stackoverflow.com/questions/10578566/jquery-this-id-return-undefined 

That’s right.

Thanks alot smirnov. you educate me a tip. :)

Leave a Reply