web 2.0

ASP.NET(vb.net) & DetailsView - DataBound

ASP.NET(vb.net) & DetailsView - DataBound Example scripts how to use DetailsView control in asp.net , The DataBound Occurs when an item is data bound to the DetailsView control

ShotDev Focus:
- ASP.NET(vb.net) & DetailsView - DataBound



<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.OleDb"%>
<%@ Page Language="VB" %>
<script runat="server">
Dim objConn As OleDbConnection
Dim objCmd As OleDbCommand
Dim strCusID As String = "C001" '*** Request.QueryString("CusID") ***'

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)

End Sub

Sub BindData()
Dim strSQL As String
strSQL = "SELECT * FROM customer WHERE CustomerID = '"& strCusID &"' "

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

'*** BindData to DetailsView ***'
myDetailsView.DataSource = dtReader

dtReader = Nothing

End Sub

Sub Page_UnLoad()
objConn = Nothing
End Sub

Sub myDetailsView_DataBound(ByVal sender As Object, ByVal e As System.EventArgs)
'*** CustomerID ***'
Dim lblCustomerID As Label = CType(myDetailsView.FindControl("lblCustomerID"),Label)
IF Not IsNothing(lblCustomerID) Then
lblCustomerID.Text = myDetailsView.DataItem("CustomerID")
End IF

'*** Name ***'
Dim lblName As Label = CType(myDetailsView.FindControl("lblName"),Label)
IF Not IsNothing(lblName) Then
lblName.Text = myDetailsView.DataItem("Name")
End IF

'*** Email ***'
Dim lblEmail As Label = CType(myDetailsView.FindControl("lblEmail"),Label)
IF Not IsNothing(lblEmail) Then
lblEmail.Text = myDetailsView.DataItem("Email")
End IF

'*** CountryCode ***'
Dim lblCountryCode As Label = CType(myDetailsView.FindControl("lblCountryCode"),Label)
IF Not IsNothing(lblCountryCode) Then
lblCountryCode.Text = myDetailsView.DataItem("CountryCode")
End IF

'*** Budget ***'
Dim lblBudget As Label = CType(myDetailsView.FindControl("lblBudget"),Label)
IF Not IsNothing(lblBudget) Then
lblBudget.Text = FormatNumber(myDetailsView.DataItem("Budget"),2)
End IF

'*** Used ***'
Dim lblUsed As Label = CType(myDetailsView.FindControl("lblUsed"),Label)
IF Not IsNothing(lblUsed) Then
lblUsed.Text = FormatNumber(myDetailsView.DataItem("Used"),2)
End IF
End Sub

<title>ShotDev.Com Tutorial</title>
<form id="form1" runat="server">
<asp:DetailsView id="myDetailsView" runat="server" AutoGenerateRows="False" OnDataBound="myDetailsView_DataBound">

<asp:TemplateField HeaderText="CustomerID">
<asp:Label id="lblCustomerID" runat="server"></asp:Label>

<asp:TemplateField HeaderText="Name">
<asp:Label id="lblName" runat="server"></asp:Label>

<asp:TemplateField HeaderText="Email">
<asp:Label id="lblEmail" runat="server"></asp:Label>

<asp:TemplateField HeaderText="CountryCode">
<asp:Label id="lblCountryCode" runat="server"></asp:Label>

<asp:TemplateField HeaderText="Budget">
<asp:Label id="lblBudget" runat="server"></asp:Label>

<asp:TemplateField HeaderText="Used">
<asp:Label id="lblUsed" runat="server"></asp:Label>



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



ASP.NET(vb.net) & DetailsView - DataBound

Download this script.

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

Leave a Reply

You must be logged in to post a comment.