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
Example
DetailsViewDataBound.aspx
<%@ 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) objConn.Open() BindData() 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 myDetailsView.DataBind() dtReader.Close() dtReader = Nothing End Sub Sub Page_UnLoad() objConn.Close() 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 </script> <html> <head> <title>ShotDev.Com Tutorial</title> </head> <body> <form id="form1" runat="server"> <asp:DetailsView id="myDetailsView" runat="server" AutoGenerateRows="False" OnDataBound="myDetailsView_DataBound"> <Fields> <asp:TemplateField HeaderText="CustomerID"> <ItemTemplate> <asp:Label id="lblCustomerID" runat="server"></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Name"> <ItemTemplate> <asp:Label id="lblName" runat="server"></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Email"> <ItemTemplate> <asp:Label id="lblEmail" runat="server"></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="CountryCode"> <ItemTemplate> <asp:Label id="lblCountryCode" runat="server"></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Budget"> <ItemTemplate> <asp:Label id="lblBudget" runat="server"></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Used"> <ItemTemplate> <asp:Label id="lblUsed" runat="server"></asp:Label> </ItemTemplate> </asp:TemplateField> </Fields> </asp:DetailsView> </form> </body> </html>
Create a asp.net file and save to path root-path/dotnet/
Run
http://localhost/dotnet/DetailsViewDataBound.aspx
Screenshot