- First Step is Open solution explorer then right click on project -> add New Item -> Web User Control ->
- Assign Name to Page.ascx
- Add asp.net server controls there and create your own server control and write code of click event or any events code in .ascx.cs file
- Open solution explorer then right click on add New Item->Create name form.aspx
- Then drag and drop that page from solution explorer to .aspx Design Page
- Run the .aspx page.
the ascx file source
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
<br />
<asp:TextBox ID="txt_show" runat="server"></asp:TextBox>
<br />
<br />
<asp:Button ID="btn_show" runat="server" Font-Bold="True"
onclick="btn_show_Click" Text="Show Message" />
Design ascx file
the
.ascx.cs file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class WebUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn_show_Click(object sender, EventArgs e)
{
txt_show.Text = "User Control Example";
}
}
The .aspx file
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UserControlexample.aspx.cs" Inherits="UserControlexample" %>
<%@ Register src="WebUserControl.ascx" tagname="WebUserControl" 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:WebUserControl ID="WebUserControl1" runat="server" />
<br />
<br />
</div>
</form>
</body>
</html>
.aspx.cs File Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class UserControlexample : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
TextBox t = (TextBox)WebUserControl1.FindControl("txt_show");
t.Text = "Hi,Hitesh";
}
}
No comments:
Post a Comment