Thursday 23 February 2012

A SIMPLE USERCONTROLL EXAMPLE

1. This is the source code of " ucImageViewer.ascx"


<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ucImageViewer.ascx.cs" Inherits="ucImageViewer" %>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td align="center">
<asp:Image ID="Image1" runat="server" 
        Height="20px" Width="51px" />
</td>
</tr>
<tr>
<td align="center"> 
<asp:Button ID="Button1" runat="server" Text="Original View"
OnClick="Button1_Click" />
</td>
</tr>
</table>


2. This is the code of  "ucImageViewer.ascx.cs"





using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ucImageViewer : System.Web.UI.UserControl
{
   public string m_ImagePath, m_ImageName,m_ImageUrl;
   public int m_ImageHeight, m_ImageWidth;
    protected void Page_Load(object sender, EventArgs e)
    {
   
        Image1.ImageUrl = "~/"+ImagePath;

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (ImagePath == string.Empty)
            return;
        else
            Response.Redirect(ImagePath);
    }
    public string ImagePath
    {
        get
        {
            return m_ImagePath;
        }
        set
        {
            m_ImagePath = value;
        }
    }
    public string ImageUrl
    {
        get
        {
            return m_ImageUrl;
        }
        set
        {
            m_ImageUrl = value;
        }
    }
    public string ImageName
    {
        get
        {
            return m_ImageName;
        }
        set
        {
            m_ImageName = value;
        }
    }

    public int ImageHeight
    {
        get
        {
            return m_ImageHeight;
        }
        set
        {
            m_ImageHeight = value;
        }
    }

    public int ImageWidth
    {
        get
        {
            return m_ImageWidth;
        }
        set
        {
            m_ImageWidth = value;
        }
    }

}

Now your user controll  to see original  image viewer is ready. You can put it in your project by drag and droping the controll.

4.Here is the code to access the user controll in a sample default page.

5. Here is the source code of  " Default2.aspx"


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>


<%@ Register src="ucImageViewer.ascx" tagname="ucImageViewer" tagprefix="uc1" %>


<!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>
    
        <uc1:ucImageViewer  ID="ucImageViewer1" runat="server"  />
    
    </div>
    </form>
</body>
</html>


6. Here is the code behind of  "Default2.aspx.cs"


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ucImageViewer1.ImagePath = "Image/shriganesh1.jpg";
        ucImageViewer1.ImageName = "Shri Ganesh 1st Pic";
        ucImageViewer1.ImageHeight = 200;
        ucImageViewer1.ImageWidth = 200;
    }
}

No comments:

Post a Comment