Asp.net slide
Trang 1Session 15
Custom Controls in
ASP.NET
Trang 2 Data binding is the process of linking data retrieved from a
database to the control, which will display the data
Repeater control is a container control, which can be used to
display a list of data This control has no form of its own.
DataList control can be used for displaying data, and it enables to
specify the flow of data.
Values to be passed to the database for insertion are attached to
the command object with the help of the Parameters collection.
Assigning the index of the row to be updated to the EditItemIndex
property makes that row editable.
The System.IO namespace has to be imported to read XML data.
XML data can be read only into a DataSet, and not into a
DataReader
Trang 3controls
Trang 4 Just as classes are built for code re-usability, it
also becomes necessary to re-use existing user
ASP.NET controls are mostly used for the web,
generating HTML or WML, which is displayed on the client’s browser
Trang 5Controls Contd…
Assfffghfioi Jhjkhjkhjkhj Uyuuiyuiyui ghghghjggg
Assfffghfioi Jhjkhjkhjkhj Uyuuiyuiyui ghghghjggg
Assfffghfioi Jhjkhjkhjkhj Uyuuiyuiyui ghghghjggg
Code for a dataview
Code for a radiobutton
Code for a checkbox
Code for a button
Code for a datagrid
Code for a Label
Code for a textbox
Trang 6Custom Controls
Created in two ways
ASP.NET like Page
Pagelets
C#
Trang 7Custom Control using Label
Control
MyLabel.ascx
<% @Control Language="C#" Debug="True" %>
<ASP:Label id="lblOne" runat="server" style="color:Red;
font-family: arial; font-size=40pt"></asp:Label>
<script language="C#" runat="server">
public string MyText
Trang 8Custom Control using Label Control
<MyFirstControl:MyLbl runat="Server" id ="AllMine"
MyText="My first custom control !!!" />
Trang 9Custom Controls Using C#
This control is completely programmed using C# and without using any existing
Trang 10Custom Controls Using C#
for (intCount=1; intCount<=3; intCount++)
The code is saved with a cs extension since it is a C# file, which will be compiled
into an assembly This is done by typing the following command at the DOS
prompt
csc /t:library /r:System.dll,System.Web.Dll Repeater.cs
The command creates Repeater.dll, which is placed in the bin directory of the
application
Trang 11Custom Controls Using C#
The custom control created using C# can be used in an aspx page as follows:
<% @Register TagPrefix="Mine" Namespace="MyOwnControls"
Trang 12Properties of Custom Controls
In order to enable a better reuse of the control it is possible to
create properties for the control
These properties can be modified dynamically, thus allowing the
user to use the control in most flexible manner
The code Repeater.cs in the previous example can be modified to
specify the text to be printed and the number of times it should be
Trang 13Properties of Custom Controls
public string myText;
public int TimesToRepeat
else timesToRepeat = value;
}
}
Trang 14Properties of Custom Controls
public string MyText
else myText = value;
}
}
Trang 15
Properties of Custom Controls
protected override void Render(HtmlTextWriter writer)
Trang 16Composite Controls
Simple controls can be clubbed together to form more complex controls
Complex controls, which comprise one or more
simple controls, are known as Composite
controls
Composite controls can comprise custom
controls, as well as windows controls
Every ASP.NET page which includes at least one control, can be referred (the ASP.NET page
itself) to as a composite control
Trang 17Composite Controls - Example
this.EnsureChildControls();
return Int32.Parse (((TextBox) Controls[1]) Text); }
Trang 18Composite Controls - Example
set { this.EnsureChildControls();
((TextBox)Controls[1]).Text = value.ToString();
} }
protected override void CreateChildControls()
{
this.Controls.Add(new LiteralControl("<h3>Value: "));TextBox box = new TextBox();
Trang 19Composite Controls - Example
This composite control created can be used within a aspx page, just like any
other custom control that has been created
<script language="C#" runat="server">
private void AddBtn_Click(Object sender, EventArgs e)
Trang 20Composite Controls - Example
<asp:button text="Subtract" OnClick="SubtractBtn_
Click" runat="server" ID="btnSubtract" />
</form>
</body>
</HTML>
Trang 21Composite Controls - Output
Trang 22 Web Controls using C#
Pagelets are custom controls that look like an asp page, and have the extension ascx
Web Controls render HTML automatically.
Custom Controls, which compose of other controls as well, are known as composite controls.
The System.Web.UI.INamingContainer interface has
no methods; it is used by ASP.NET to create unique IDs