How to change html src image in c #

<img src ="~/UserControls/Vote/Images/Arrow Up.png" id = "vote-up-off" runat = "server" alt ="vote up" class="voteupImage" style="height: 45px; width: 44px"/> 

here i want to change src images for a specific state like

 if ( a==4) { src url shuld be ...... } else { src url should be... } 
+4
source share
5 answers

First you need to specify the id name, which can be used as a variable:

 <img src="~/UserControls/Vote/Images/Arrow Up.png" id="VoteUpOff" runat="server" alt ="vote up" class="voteupImage" style="height: 45px; width: 44px" /> 

And in your code behind you can use this variable:

 if (someCondition) { VoteUpOff.Attributes["src"] = ResolveUrl("~/UserControls/foo.png"); } 
+4
source

You want to change id to something without a hyphen, but then it will be

 voteUpOff.Attributes["src"] = "myImage.png"; 
+2
source

HTML

 <img src="~/UserControls/Vote/Images/Arrow Up.png" id="VoteUpOff" runat="server" alt ="vote up" class="voteupImage" style="height: 45px; width: 44px" /> 

server side

 if (someCondition) { VoteUpOff.Attributes["src"] = ResolveUrl("~/UserControls/foo.png"); } 

Keep in mind to see the changes you should put "img" in the UpdatePanel. After making changes. Update UpdatePanel if its UpdateMode = Conditional, otherwise it will be automatically updated if its ChildAsTriggers = True property

+1
source

Try it, its work for me

 <img src="_images/<%= AssignImageURL() %>" alt="Logo" /> 

C # method

 protected string AssignImageURL() { String ID = Convert.ToString(Session["id"]); ds = sq.SelectQuery("Select LogoURl from table where ID='"+ ID+"' "); if (Convert.ToString(ds.Tables[0].Rows[0][0]).Trim() != "") return Convert.ToString(ds.Tables[0].Rows[0][0]).Trim(); else return "Logo.jpg"; } 
0
source
 if (someCondition) { vote-up-off.Attributes["src"] = ResolveUrl("~/UserControls/pic.png"); } 
-2
source

All Articles