Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use javascript to set image URL of image control in asp.net

I have a Image, FileUpload and a Button controls. I want to save the image to the server from the local path obtained from FileUpload control. I implemented this functionality on Button Click in C#..

Now i want to set the image URL of Image control OnClientClick of the same button on which server side code is implemented.

Image URL will defer everytime depending on file selected in FileUpload control. Can anyone help me to understand how javascript can be used to set image URL based on thre file selected in File Upload Control?

like image 412
Ishan Avatar asked Nov 18 '25 19:11

Ishan


2 Answers

First of all, understand that JavaScript doesn't understand, care, or even know about C# and its fancy "controls". It just deals with HTML. Period. That said, you can use JavaScript's setAttribute function to set the image URL of an img tag (not control). Like this:

document.getElementById('my-image').setAttribute('src', 'http://ecx.images-amazon.com/images/I/41%2BjAZ4dUGL._SS500_.jpg');

Demo here: http://jsfiddle.net/je9Gx/

like image 180
Ayman Safadi Avatar answered Nov 20 '25 11:11

Ayman Safadi


You can use this code to find the image control, where imgid is ID of image control;

$("[id$='imgid']").attr("src",pathfromfileuploader);

//pathfromfileuploader=it is a variable which stores the path taken from file uploader;

Hope it will help :)

like image 29
Pranav Avatar answered Nov 20 '25 11:11

Pranav



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!