Sometimes our requirement is to use same asp controls in a number of web pages. To avoid lot of effort , we can create user controls that can be automatically replicated in different web pages.
There are some applications which require changes in individual asp controls of user control. For example - One of my application most webpages require requiredfield validation on two textboxes but some webpages require requiredfield validation on one textbox only and everything else is the same. So now there is a question " How to do this by making only one user control?"This can be easily done by creating property of user control in its .cs file,by using that property we can enable or disable some or all controls depending upon the requirement of webpage.
1. 1.Add web user control to your project(WebUserControl1.ascx)
2. Design your user control using toolbox.
3. Now you can drag WebUserControl1.ascx (from solution explorer) to your aspx page that will create the same user control on your webpage.
We have successfully created the user control and even embedded it on our web page but still to use it ,we need to define its properties such as enable,disable, load data(depending upon our need) in WebUserControl1.ascx.
In the following example it is shown that how to create different properties of user control in WebUserControl1.ascx.cs and how to use them in WebForm1.aspx.cs.
WebUserControl1.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs"Inherits="WebApplication1.controls.WebUserControl1" %>
<asp:Label ID="Label1" runat="server" Text="Label">asp:Label>
<asp:TextBox ID="TextBox1" runat="server">asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="TextBox1"ErrorMessage="RequiredFieldValidator">asp:RequiredFieldValidator>
<br />
<asp:Label ID="Label2" runat="server" Text="Label">asp:Label>
<asp:TextBox ID="TextBox2" runat="server">asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="TextBox2"ErrorMessage="RequiredFieldValidator">asp:RequiredFieldValidator>
<br />
WebUserControl1.ascx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1.controls
{
public partial class WebUserControl1 : System.Web.UI.UserControl
{
bool regular;
protected void Page_Load(object sender, EventArgs e)
{
}
public bool requiredexp
{
get
{
return regular;
}
set
{
regular = value;
RequiredFieldValidator2.Enabled = value;
}
}
public string text1
{
get
{
return TextBox1.Text.ToString();
}
set
{
TextBox1.Text = value;
}
}
}
}
WebForm1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"Inherits="WebApplication1.WebForm1" %>
<%@ Register src="controls/WebUserControl1.ascx" tagname="WebUserControl1" 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:WebUserControl1 ID="WebUserControl11" runat="server" />
<br />
<asp:TextBox ID="TextBox1" runat="server" ReadOnly="True">asp:TextBox>
<br />
<asp:TextBox ID="TextBox2" runat="server" ReadOnly="True">asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
div>
form>
body>
html>
WebForm1.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
WebUserControl11.requiredexp = false;
}
protected void Button1_Click(object sender, EventArgs e)
{
TextBox1.Text = WebUserControl11.text1;
}
}
}