web 2.0

ASP.NET(vb.net) & DataList - FindControl

ASP.NET(vb.net) & DataList - FindControl Example scripts how to use DataList control in asp.net , The DataList.FindControl is searches the current naming container for the specified server control.

ShotDev Focus:
- ASP.NET(vb.net) & DataList - FindControl

Example

DataListDataBind.aspx

<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<%@ Page Language="VB" %>
<script runat="server">

Dim objConn As OleDbConnection
Dim objCmd As OleDbCommand

Sub Page_Load(sender As Object, e As EventArgs)
Dim strConnString As String
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="& _
Server.MapPath("database/mydatabase.mdb")&";"
objConn = New OleDbConnection(strConnString)
objConn.Open()

IF Not Page.IsPostBack() Then
BindData()
End IF
End Sub

Sub BindData()
Dim strSQL As String
strSQL = "SELECT * FROM category"

Dim dtReader As OleDbDataReader
objCmd = New OleDbCommand(strSQL, objConn)
dtReader = objCmd.ExecuteReader()

'*** BindData to DataList ***'
myDataList.DataSource = dtReader
myDataList.DataBind()

dtReader.Close()
dtReader = Nothing

End Sub

Sub Page_UnLoad()
objConn.Close()
objConn = Nothing
End Sub

Private Sub myDataList_ItemDataBound(sender As Object, e As DataListItemEventArgs)
'*** Image ***'
Dim img As Image = CType(e.Item.FindControl("imgPicture"),Image)
IF Not IsNothing(img) Then
img.ImageURL = "images/" & e.Item.DataItem("Picture")
'img.Attributes.Add("OnClick","window.location='http://www.shotdev.com?Cateid="&e.Item.DataItem("CategoryID")&"'")
'img.Style.Add("cursor","hand")
End IF

'*** lblCateID ***'
Dim lblID As Label = CType(e.Item.FindControl("lblCateID"),Label)
IF Not IsNothing(lblID) Then
lblID.Text = e.Item.DataItem("CategoryID")
lblID.Visible = False '*** Hide ***'
End IF

'*** HyperLink ***'
Dim hplCate As Hyperlink = CType(e.Item.FindControl("hplCategory"),Hyperlink)
IF Not IsNothing(hplCate) Then
hplCate.Text = e.Item.DataItem("CategoryName")
hplCate.ToolTip = e.Item.DataItem("CategoryName")
hplCate.Navigateurl = "http://www.shotdev.com?Cateid="&e.Item.DataItem("CategoryID")
End IF
End Sub

Sub Button1_Click(sender As Object, e As EventArgs)
Dim chkCate As CheckBox
Dim lblID As Label
Dim i As Integer
lblText.Text = ""
For i = 0 To myDataList.Items.Count - 1
chkCate = myDataList.Items(i).FindControl("chkCateID")
lblID = myDataList.Items(i).FindControl("lblCateID")
IF chkCate.Checked = True Then
'*** Have lblID.Text ***'
Me.lblText.Text = Me.lblText.Text & "<br>" & lblID.Text
End IF
Next
End Sub

</script>
<html>
<head>
<title>ShotDev.Com Tutorial</title>
</head>
<body>
<form id="form1" runat="server">
<asp:DataList id="myDataList" runat="server" borderstyle="inset" cellspacing="2" cellpadding="2" RepeatColumns="2"
onItemDataBound="myDataList_ItemDataBound">
<HeaderTemplate>
<b>My Category</b>  <br />
</HeaderTemplate>
<ItemTemplate>
<div style="width:100px" align="center">
<asp:Image id="imgPicture" runat="server"></asp:Image>
<br />
<asp:Label id="lblCateID" runat="server"></asp:Label>
<asp:CheckBox id="chkCateID" runat="server"></asp:CheckBox>
<asp:HyperLink id="hplCategory" runat="server"></asp:HyperLink>
</div>
</ItemTemplate>
</asp:DataList>
<br />
<asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Submit"></asp:Button>
<hr />
<asp:Label id="lblText" runat="server"></asp:Label>
</form>
</body>
</html>

Create a asp.net file and save to path root-path/dotnet/

Run
http://localhost/dotnet/DataListFindControl.aspx

Screenshot

ASP.NET(vb.net) & DataList - FindControl
.
.
.

Download this script.
Download

1 Star2 Stars3 Stars4 Stars5 Stars6 Stars7 Stars8 Stars9 Stars10 Stars (No Ratings Yet)
Loading ... Loading ...

One Response to “ASP.NET(vb.net) & DataList - FindControl”

  1. 2volunteers…

Leave a Reply

You must be logged in to post a comment.