0

Quick question. I am dynamically creating some buttons and basically want to add a background image to them in the code behind. Can someone tell me how its supposed to look like? I want to use the Button.Style.Add().

I tried Button.Style.Add("background-image","url(~/Uploads/Steak.jpg)"); but it did not work.

2 Answers 2

1

please use following code:

Button.Style.Add("background-image","url('~/Uploads/Steak.jpg')");

Add single quotes to the url

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

2 Comments

I had tried this previously, tried it again, just as you wrote it and my button is still with its default appearance.
Try using- Button.Attributes.Add("style", "background-image:url('~/Uploads/Steak.jpg');");
0
string strimgpath = "Uploads/Steak.jpg";
String strImgLocation = "url('" + strimgpath + "')";
idimage.Style.Add("background-image", strImgLocation);

1 Comment

Thank you, I found the answer before your post but yours is exactly correct. The "~/" that I added was making the problem. I have done it as you have written it and it is working without a problem.

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.