• Home
  • Privacy Policy
  • Terms & Conditions
  • Contact
  • Advertise
Videos, Photos, Wallpapers, Free Download, Movies, Songs, Sports , Live TV, Entertainment
 
  • Home
  • Bollywood
  • Hollywood
  • Box Office
  • Beauty
  • Fashion
  • Celebrity
  • Business
    • Social Media
    • Money Online
    • Category
  • Entertainment
    • Bollywood
  • Video
    • Youtube
    • Video
  • Technology
    • Asp.net
    • C#
    • SQL
    • .Net
Showing posts with label User Control. Show all posts
Rather than use the registering a User control in each page in which you need to use it by using the <%@ Register %> directive, you can register a User control once for an entire application. You can register a User control in an application’s web configuration file.

Web.Config

<configuration>
<system.web>
<pages>
<controls>
<add tagPrefix="ASPXIMAGE" tagName="WebImage" src="~/UserControls/CtrlImage.ascx"/>
</controls>
</pages>
</system.web>
</configuration>

After you register a User control in the web configuration file, you can simply declare the User control in any page, but it does not include the <%@ Register %> directive.

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Show Application Register</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<ASPXIMAGE:WebImage ID="WebImage1" Runat="Server" />
</div>
</form>
</body>
</html>
You can dynamically load a User control at runtime and display it in a page.

You load a User control at runtime with the Page.LoadControl() method. This method returns an instance of the Control class that you can add to a page. Typically, you add the User control to a PlaceHolder control that you have declared on the page.

The PlaceHolder control was designed to do absolutely nothing. It simply acts as a placeholder on the page where you can add other controls.

<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<script runat="server">

const string CtrlFolder = "~/WebRoot";

protected void Page_Load(object sender, EventArgs e)
{
string WebRootPath = GetRandomWebPath();
Control LoadCtrl = Page.LoadControl(WebRootPath);
PHLoadControl.Controls.Add(LoadCtrl);
}

private string GetRandomWebPath()
{
Random rnd = new Random();
string[] files = Directory.GetFiles(MapPath(CtrlFolder), "*.ascx");
string WebRootPath = Path.GetFileName(files[rnd.Next(files.Length)]);
return Path.Combine(CtrlFolder, WebRootPath);
}

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Load a User control</title>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:PlaceHolder id="PHLoadControl" Runat="server" />

</div>
</form>
</body>
</html>
Older Posts Home

ASP.NET Examples

 
2012 24x7 Magazine. All rights reserved.
Designed by 24x7 Magazine