Image With Asp.Net Web Application - asp.net

I have a problem with Images I have a image which is at root directory like “C:\Mh\mahesh1” and tried to upload by following ways but won’t works.
Defaults.aspx.cs
namespace UITI
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string img = MapPath("~Mh/mahesh1.jpg");
myimages.ImageUrl = img;
}
}
}
Defaults.aspx
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="UITI._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Image ID="myimages" runat="server" BorderStyle="Double" />
</div>
</form>
</body>
</html>
I have also tried on Default.aspx.cs page like:
namespace UITI
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string img = ResolveClientUrl ("~/Mh/mahesh1.jpg");
myimages.ImageUrl = img;
}
}
}
How to do?.

try it
string img = Server.MapPath("~/Mh/mahesh1.jpg");
myimages.ImageUrl = img;
or
Edit:
string img = ResolveUrl("~/Mh/mahesh1.jpg");
myimages.ImageUrl = img;
If it not worked,please check your folder path (~/Mh/mahesh1.jpg). Your path is non correct. My above code is working to me
New Edit
string img = **ResolveUrl("mahesh1.jpg");**
myimages.ImageUrl = img;
See
first just try this for put correct url straightly
**myimages.ImageUrl = #"~/Mh/mahesh1";**
Problem is here:
Your mahesh1 image is inside of any other folder(for project folder or any other drive or desktop ) .So you got this problem . You can use any other image. or rename of your mahesh1 to mahes123 (if you like name) image then check.

Related

How do I have a usercontrol add a Html String to Head of a MasterPage

I know this is easy when you know what you are adding. For example, with a UI control its
Page.Header.Controls.Add([control])
However, I am pulling HTML code from a CMS database. In other words, I need to add a string to the Head section of the master page.
You can add a Literal control to the Header by using code similar to the one you show in your question:
public partial class MyUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
var lit = new LiteralControl();
lit.Text = "<link href=\"test.css\" rel=\"stylesheet\" />";
Page.Header.Controls.Add(lit);
}
}
If you're using a master page, you could:
Master page markup:
<html>
<head runat="server">
<asp:ContentPlaceHolder ID="MyHeadContentPlaceHolder" runat="server"></asp:ContentPlaceHolder>
</head>
</html>
Content page markup:
<asp:Content ID="MyContent" ContentPlaceHolderID="MyHeadContentPlaceHolder" runat="server">
<asp:Literal ID="MyLiteralForCmsContent" runat="server"></asp:Literal>
</asp:Content>
Content page code-behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Load the literal with content from the CMS.
this.MyLiteralForCmsContent.Text = ""; //Hook up the call to the CMS here.
}
}

Access a content page variable through the master page

I know in ASP.net, we can access a master page variable through content page, but is there anyway we can access a content page variable through the master page?
Yes you can. You have to implement a base class and content class should be derived from that base class.
EDIT : Posted markup and changed code for a clearer example
I have created a base page inherited the System.Web.UI.Page, then made the content page inherit it. My basepage:
namespace WebApplication2
{
public class BasePage : System.Web.UI.Page
{
public BasePage() { }
public virtual string TextValue()
{
return "";
}
}
}
Here's my content page markup:
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication2._Default" %>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
<asp:Label ID="lblContentText" Text="Contentpage TextValue:" runat="server"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</asp:Content>
Content page code:
namespace WebApplication2
{
public partial class _Default : BasePage
{
protected void Page_Load(object sender, EventArgs e)
{
}
public override string TextValue()
{
return TextBox1.Text;
}
}
}
My Masterpage markup:
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="WebApplication2.SiteMaster" %>
<!DOCTYPE html>
<html lang="en">
<head runat="server">
<meta charset="utf-8" />
<title><%: Page.Title %> - My ASP.NET Application</title>
<asp:ContentPlaceHolder runat="server" ID="HeadContent" />
</head>
<body>
<form runat="server">
<header> </header>
<div id="body">
<asp:Label ID="lblText" runat ="server" Text="Masterpage Text :" />
<asp:TextBox ID="txtMaster" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Click to read content page TextValue " OnClick="Button1_Click" />
<asp:ContentPlaceHolder runat="server" ID="MainContent" />
</div>
<footer>
</footer>
</form>
</body>
</html>
And the implementation in masterpage code behind:
namespace WebApplication2
{
public partial class SiteMaster : MasterPage
{
BasePage Currentpage = null;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Currentpage = this.Page as BasePage;
if (Currentpage != null)
{
txtMaster.Text = Currentpage.TextValue();
}
}
}
}
If you see any error like BasePase is not recognized, Please make sure that either it is using same namespace(i.e. WebApplication2), or the namespace is added to the implementation page (i.e. using WebApplication2;).
Hope it helps!

Click event on div in asp.net

I have a div in default.aspx with img inside as follow
<div id="sTab" class="tabs firsttab" runat="server">
<asp:Image ID="sTabImg" src="images/home.png" runat="server" />
Activities
</div>
I want to perform some action on click of the div in ASP.NET (not in javascript).
I tried the following
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
sTabImg.Attributes["onclick"] = ClientScript.GetPostBackEventReference(this, "ClickDiv");
}
protected void ClickDiv()
{
Label2.Text = "helo got it";
}
The page is getting refreshed and when i debugged the issue, its not entering the ClickDiv function..What wrong here..
From Here
public partial class _Default : System.Web.UI.Page, IPostBackEventHandler
{
protected void Page_Load(object sender, EventArgs e)
{
div1.Attributes["onclick"]=ClientScript.GetPostBackEventReference(this,"ClickDiv");
}
protected void Div1_Click()
{
// Do something
}
#region IPostBackEventHandler Members
public void RaisePostBackEvent(string eventArgument)
{
if (!string.IsNullOrEmpty(eventArgument))
{
if (eventArgument == "ClickDiv")
{
Div1_Click();
}
}
}
#endregion
}
You have to implement the IPostBackEventHandler Interface.
Why not use ImageButton?
<%# Page Language="C#" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>ImageButton Sample</title>
<script language="C#" runat="server">
void ImageButton_Click(object sender, ImageClickEventArgs e)
{
Label1.Text = "You clicked the ImageButton control at the coordinates: (" +
e.X.ToString() + ", " + e.Y.ToString() + ")";
}
</script>
</head>
<body>
<form id="form1" runat="server">
<h3>ImageButton Sample</h3>
Click anywhere on the image.<br /><br />
<asp:ImageButton id="imagebutton1" runat="server"
AlternateText="ImageButton 1"
ImageAlign="left"
ImageUrl="images/pict.jpg"
OnClick="ImageButton_Click"/>
<br /><br />
<asp:label id="Label1" runat="server"/>
</form>
</body>
</html>
And you can see some examples here.

How to use control state in asp.net

Below is my simple code to use control state in a custom control,
[DefaultProperty("Text")]
[ToolboxData("<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>")]
public class WebCustomControl1 : WebControl
{
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string Text
{
get { return text; }
set { text = value; }
}
private string text;
protected override void RenderContents(HtmlTextWriter output)
{
output.Write(Text);
}
protected override void OnInit(System.EventArgs e)
{
base.OnInit(e);
Page.RequiresControlState(this);
}
protected override object SaveControlState()
{
object baseSate = base.SaveControlState();
return new Pair(baseSate, Text);
}
protected override void LoadControlState(object savedState)
{
Pair value = savedState as Pair;
text = value.Second;
}
}
But it doesn't seem to work.. The SaveControlState and LoadControlState are not firing. can someone help me..?
Below is the aspx Code. Here is where i use the custom control.
<%# Page Language="C#" AutoEventWireup="true" EnableViewState="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<%# Register Assembly="WebApplication1" Namespace="WebApplication1" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>`enter code here`
<body>
<form id="form1" runat="server">
<div>
<cc1:WebCustomControl1 ID="WebCustomControl1_1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Button" /></div>
</form>
</body>
</html>
You've called RequiresControlState
Determines whether the specified Control object is registered to participate in control state management.`
But you should call RegisterRequiresControlState
Registers a control as one whose control state must be persisted.

changing themes with buttons, how?

I have 2 themes that i want to use on site, and they are settled in Themes Folder-> theme1 and theme2 , in theme1 and theme2 i have one .css file.
So how will I connect these .css files to two buttons and when i click on button1 i want to theme1.css activates and for theme2.
If you need more informations about problem ask.
thanks
In the Page_PreInit or before insert the code
{
this.Theme = "myTheme"
}
I have created a sample application in asp.net check out this,and let me know if there is some doubt.
Updated
<%# Page Language="C#" AutoEventWireup="true" CodeFile="ProfilePage.aspx.cs" Inherits="ProfilePage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="test" Text="Stackoverflow" runat="server"></asp:Label>
<asp:Button ID="Button1" runat="server" Text="Theme1" OnClick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="Theme2" OnClick="Button2_Click" />
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class ProfilePage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Page_PreInit(object sender, EventArgs e)
{
switch (Request.QueryString["theme"])
{
case "Blue":
Page.Theme = "Blue";
break;
case "Red":
Page.Theme = "Red";
break;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("ProfilePage.aspx?theme=Red");
}
protected void Button2_Click(object sender, EventArgs e)
{
Response.Redirect("ProfilePage.aspx?theme=Blue");
}
}

Resources