什么是customcontrol和用户控制 [英] what is customcontrol and user control
本文介绍了什么是customcontrol和用户控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
什么是自定义控件?
有用的目的是什么?
Hi,
What is custom control?
For which purpose is it useful?
Could you please explain to me with a proper example?
推荐答案
尝试使用此链接
http://ondotnet.com/pub/a/dotnet/excerpt/progaspdotnet_14/index1.html [^ ]
http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=164 [ ^ ]
try to this Link
http://ondotnet.com/pub/a/dotnet/excerpt/progaspdotnet_14/index1.html[^]
http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=164[^]
查看此链接
http://support.microsoft.com/kb/893667 [
see this link
http://support.microsoft.com/kb/893667[^]
User controls are custom, reusable controls. They are more or less like the Web forms, with similar programming model like a Web page. user controls in asp.net ends with an ".ascx" extension. Whereas Custom control are compiled class library program. They are .dll files.
You can create a .ascx file and can use it across your website.
for eg:
1. Create a "Test.ascx" file in your project and write the foll. code in your ascx file
<script language="C#" runat="server">
public void button1_Click(object sender, EventArgs e)
{
label1.Text = "Hello World!!!";
}
</script>
<asp:Label id="label1" runat="server"/>
<br><br>
<asp:button id="button1" text="Hit" OnClick="button1_Click" runat="server" />
2. Now you can use this Test.ascx in a web page or an other .ascx file
a.) First you need to register the user control in the page
<%@ Register TagPrefix="UC" TagName="TestControl" Src="test.ascx" %>
b.) you can now use the usercontrol as :
<html>
<body>
<form runat="server">
<UC:TestControl id="Test1" runat="server"/>
</form>
</body>
</html>
-----------------------------------------------------------------------------
Custom controls are compiled code components that execute on the server, expose the object model, and render markup text, such as HTML or XML, as a normal Web Form or user control does.
To write a custom control, you should directly or indirectly derive the new class from the System.Web.UI.Control class or from the System.Web.UI.WebControls.WebControl class.
You create a custom control if u want to change or extend the default functionality of
existing controls
Controls provided by Telerik are the best examples of custom controls
for eg:
1.)
using System
using System.Collections
using System.ComponentModel
using System.Data
using System.Web
using System.Web.UI
using System.Web.UI.WebControls
public class SimpleServerControl : Control
protected override void Render(HtmlTextWriter writer)
{
writer.Write("Hello World from custom control");
}
2.) Now u need to add the reference for the generated dll in your project and then the register the custom controls in the page or .ascx file just like
usercontrols
<%@ Register TagPrefix="CC " Namespace=" CustomServerControlsLib " Assembly="CustomServerControlsLib " %>
3.)
<form id="Form1" method="post" runat="server">
<CC:SimpleServerControl id="ctlSimpleControl" runat="server">
</CC:SimpleServerControl >
</form>
这篇关于什么是customcontrol和用户控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文