I'm trying to add an ImageUrl property to an asp.net server control. So, I'm wonder how can I enable the option that the user can Pick Up the URL for this propery (For example, Image and ImageButton Web Controls).
In addition, I want to add a default image to the server control, so in case no image Url is pickeed up, the default image will be displayed.
Please, I need your help in order to imlement the above requirements.
It is very appreciated if you can send me a sample code which demonstrates the requirements.
Plz add this ImgeUrl as a string property, and then use Response.Write or Render control, plz write "<img src='"+ImageUrl+"/>".
public class ImageControl
{
………………
public string ImageUrl
{
get{return ViewState["url"]==null?String.Empty:ViewState["url"].ToString();}
set{ViewState["url"]=value;}
}
}
If you want to show the default image, in case of no image. here is the code (just updating the TimoYang code).
public class ImageControl
{
private string _defaultImageUrl = "~/Image/default.jpg"; // put your own default image.
………………
public string ImageUrl
{
get{return ViewState["url"]==null?defaultImageUrl:ViewState["url"].ToString();}
set{ViewState["url"]=value;}
}
}
If this post answered your question or solved your problem, please Mark it as Answer.
public class ImageControl { private string _defaultImageUrl = "~/Image/default.jpg"; // put your own default image. ……………… public string ImageUrl { get{return ViewState["url"]==null?defaultImageUrl:ViewState["url"].ToString();} set{ViewState["url"]=value;} } }
I did the required steps, but I see that I have an error with your code (An object reference is required for the non-static field, method, or property "System.Web.UI.Page.ClientScript.get):
I did the required steps, but I see that I have an error with your code (An object reference is required for the non-static field, method, or property "System.Web.UI.Page.ClientScript.get):
Regarding "YourProjectsNameSpace.Images.jpg", can you explain it?
My project is devided to several namespaces, but the control is related to a namespace called CodeHelper.UI and in my case I embedded an image called Pass.png to the project. Is that correct to change "YourProjectsNameSpace.Images.jpg" to "CodeHelper.UI.Pass.png"?
If so, the image is still not displayed, why?
Bader
Member
10 Points
84 Posts
Images within asp.net server control
Jul 31, 2012 05:24 AM|LINK
Hi,
I'm trying to add an ImageUrl property to an asp.net server control. So, I'm wonder how can I enable the option that the user can Pick Up the URL for this propery (For example, Image and ImageButton Web Controls).
In addition, I want to add a default image to the server control, so in case no image Url is pickeed up, the default image will be displayed.
Please, I need your help in order to imlement the above requirements.
It is very appreciated if you can send me a sample code which demonstrates the requirements.
Regards,
Bader
TimoYang
Contributor
3732 Points
1275 Posts
Re: Images within asp.net server control
Jul 31, 2012 05:27 AM|LINK
Plz add this ImgeUrl as a string property, and then use Response.Write or Render control, plz write "<img src='"+ImageUrl+"/>".
public class ImageControl { ……………… public string ImageUrl { get{return ViewState["url"]==null?String.Empty:ViewState["url"].ToString();} set{ViewState["url"]=value;} } }Ramesh Chand...
Star
12922 Points
2672 Posts
Re: Images within asp.net server control
Jul 31, 2012 05:34 AM|LINK
If you want to show the default image, in case of no image. here is the code (just updating the TimoYang code).
public class ImageControl { private string _defaultImageUrl = "~/Image/default.jpg"; // put your own default image. ……………… public string ImageUrl { get{return ViewState["url"]==null?defaultImageUrl:ViewState["url"].ToString();} set{ViewState["url"]=value;} } }TimoYang
Contributor
3732 Points
1275 Posts
Re: Images within asp.net server control
Jul 31, 2012 05:35 AM|LINK
Bader
Member
10 Points
84 Posts
Re: Images within asp.net server control
Jul 31, 2012 06:21 AM|LINK
Hi,
Thank you both, TimoYang and Ramesh Chandra.
The default image is placed only within the server control project and not within the application.
So how can I make reference to an image which is placed within the server control project when no image is selected?
Please, I need your help,
Regards,
Bader
Ramesh Chand...
Star
12922 Points
2672 Posts
Re: Images within asp.net server control
Jul 31, 2012 06:31 AM|LINK
you have to perform the following steps.
[assembly: System.Web.UI.WebResource("YourProjectsNameSpace.Images.jpg", "img/jpg")]4. Update your code as given below.
public class ImageControl { private string _defaultImageUrl = Page.ClientScript.GetWebResourceUrl(this.GetType(), "YourProjectsNameSpace.Images.jpg"); ……………… public string ImageUrl { get{return ViewState["url"]==null?defaultImageUrl:ViewState["url"].ToString();} set{ViewState["url"]=value;} } }Bader
Member
10 Points
84 Posts
Re: Images within asp.net server control
Jul 31, 2012 07:07 AM|LINK
Hi,
Thank you for the reply,
I did the required steps, but I see that I have an error with your code (An object reference is required for the non-static field, method, or property "System.Web.UI.Page.ClientScript.get):
http://www.tmonavot.com/imageurl.png
Can you please help me solve this issue?
By the way, how should I modify the assemblyinfo.cs when I have move than one image of diffeent types, such as png, gif...?
Regards,
Bader
Ramesh Chand...
Star
12922 Points
2672 Posts
Re: Images within asp.net server control
Jul 31, 2012 08:23 AM|LINK
you have to update your code as given below
public class ImageControl { private string _defaultImageUrl = string.Empty; protected void Page_Load(object sender, EventArgs e) { _defaultImageUrl = Page.ClientScript.GetWebResourceUrl(this.GetType(), "YourProjectsNameSpace.Images.jpg"); } ……………… public string ImageUrl { get{return ViewState["url"]==null?defaultImageUrl:ViewState["url"].ToString();} set{ViewState["url"]=value;} } }you have to write one line for each image file.
shivanand G ...
Participant
1763 Points
534 Posts
Re: Images within asp.net server control
Jul 31, 2012 08:30 AM|LINK
get the path of the image by imgaebutton id,
if(imagebuttonid.imagepath=="server.mappath("~/gevethepath of the image"))
{
///////////////////////////
}
else
{
//////////////////////////////
}
good luck
shivanand.G.N (shivu.betta@gmail.com)
Bader
Member
10 Points
84 Posts
Re: Images within asp.net server control
Jul 31, 2012 12:01 PM|LINK
Hi,
Thank you for the modified code.
Regarding "YourProjectsNameSpace.Images.jpg", can you explain it?
My project is devided to several namespaces, but the control is related to a namespace called CodeHelper.UI and in my case I embedded an image called Pass.png to the project. Is that correct to change "YourProjectsNameSpace.Images.jpg" to "CodeHelper.UI.Pass.png"? If so, the image is still not displayed, why?
Regards,
Bader