DetailsView - template code

I've recently started using the DetailsView control in .NET. This article is a basic template for how to use the controls (so I don't forget)

There are two technologies combined here, we're using a Telerik RadWindow for a popup control, and the DetailsView control

Related Articles

... and you 'll find more on the NET Development Menu

The asps page

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="InventoryStocking3.aspx.vb" Inherits="utilities_InventoryStocking3" %>
<!DOCTYPE html>
<head runat="server">
    <title></title>
</head>
<body>
 
    <form id="form1" runat="server">
    <div>
        <script type="text/javascript">
            function CloseAndRebind(args)
            {
                GetRadWindow().BrowserWindow.refreshGrid(args);
                GetRadWindow().close();
            }
  
            function GetRadWindow()
            {
                var oWindow = null;
                if (window.radWindow) oWindow = window.radWindow; //Will work in Moz in all cases, including clasic dialog
                else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow; //IE (and Moz as well)
  
                return oWindow;
            }
  
            function CancelEdit()
            {
                GetRadWindow().close();
            }
        </script>
  
        <asp:ScriptManager ID="ScriptManager2" runat="server" />
  
        <telerik:RadFormDecorator RenderMode="Lightweight" ID="RadFormDecorator1" runat="server" Skin="Vista" DecoratedControls="All" />
        <br />
        <br />
        <asp:DetailsView ID="DetailsView1" DataKeyNames="itemnmbr,locncode,UofM" runat="server" AutoGenerateRows="False"
            GridLines="None" Height="50px" Width="100%"
            BorderWidth="0" CellPadding="0" CellSpacing="7">
            <Fields>
                 
                <asp:BoundField DataField="itemnmbr" HeaderText="Item Number"  ReadOnly="true" />
                <asp:BoundField DataField="locncode" HeaderText="Yard" ReadOnly="true"  />
                <asp:BoundField DataField="UofM" HeaderText="UofM" ReadOnly="true"  />
                <asp:TemplateField HeaderText="Qty Requested" >
                    <EditItemTemplate>
                        <telerik:RadNumericTextBox ID="ntbQtyRequested" runat="server"  NumberFormat-DecimalDigits='<%#Convert.ToDecimal(Eval("decplqty")) %>' Value='<%#Convert.ToDecimal(Eval("QtyRequested")) %>'></telerik:RadNumericTextBox>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:CommandField ShowEditButton="True" ButtonType="Button" />
                <asp:CommandField ShowInsertButton="True" ButtonType="Button" />
            </Fields>
        </asp:DetailsView>
 
    </div>
    </form>
</body>
</html>

The code behind

Imports System.Data
Imports Telerik.Web.UI
 
Partial Class utilities_InventoryStocking3
 
    Inherits InheritedPage
    Dim mstrItemNumber As String
    Dim mstrLocationCode As String
    Dim decQtyRequested As Decimal
    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        'force the DetailsView control into Edit mode
        DetailsView1.DefaultMode = DetailsViewMode.Edit
 
    End Sub
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
        Me.Page.Title = "Editing record"
 
        'we expect these values to be passed in the query string
        mstrItemNumber = Request("itemnmbr")
        mstrLocationCode = Request("locncode")
        decQtyRequested = Request("QtyRequested")
 
        If Not IsPostBack Then
            'data access code to populate the DetailsView control
            Me.DetailsView1.DataSource = DynData.SPs.FP_InventoryStocking3(mstrItemNumber, mstrLocationCode, decQtyRequested, AppUser.Database).getTable
            Me.DetailsView1.DataBind()
        End If
 
    End Sub
    Protected Sub DetailsView1_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewCommandEventArgs) Handles DetailsView1.ItemCommand
        'in Edit mode, there are two buttons
        Select Case e.CommandName
            Case "Update"
                ClientScript.RegisterStartupScript(Page.GetType(), "mykey", "CloseAndRebind();", True)
            Case "Cancel"
                ClientScript.RegisterStartupScript(Page.GetType(), "mykey", "CancelEdit();", True)
        End Select
 
    End Sub
    Protected Sub DetailsView1_ItemUpdating(sender As Object, e As System.Web.UI.WebControls.DetailsViewUpdateEventArgs) Handles DetailsView1.ItemUpdating
        'data access code to save the data entered
 
        'retreive data in a templated field
        Dim decQtyRequested As Decimal = CType(Me.DetailsView1.FindControl("ntbQtyRequested"), RadNumericTextBox).Value
        If decQtyRequested <= 0 Then
            Exit Sub
        End If
 
        'retrieve data in the DataKeyNames collection
        Dim keyList As IOrderedDictionary = Me.DetailsView1.DataKey.Values
        Dim strItemNumber As String = keyList(0)
        Dim strLocationCode As String = keyList(1)
        Dim strUofM As String = keyList(2)
 
        'data access code to retrieve a record number from the database
        Dim oDT As DataTable = DynData.SPs.FP_FPStockRequest_NextNumber(AppUser.Database).getTable
        If oDT.Rows.Count = 0 Then Exit Sub
 
        Dim strStockRequest As String = oDT.Rows(0)("StockRequest")
 
        'data access code to create a Stock Request
        Dim oFPStockRequest As New DynData.FPStockRequest
        oFPStockRequest.StockRequest = strStockRequest
        oFPStockRequest.ITEMNMBR = strItemNumber
        oFPStockRequest.LOCNCODE = strLocationCode
        oFPStockRequest.QUANTITY = decQtyRequested
        oFPStockRequest.UOFM = strUofM
        oFPStockRequest.Save(AppUser.Database)
 
        'close the form
        ClientScript.RegisterStartupScript(Page.GetType(), "mykey", "CloseAndRebind();", True)
    End Sub
 
End Class

 

 

 

 

 


RealWorldCode gives developers practical, real‑world solutions with clean, working code — no fluff, no theory, just answers.
Links
Home
Knowledge Areas
Sitemap
Contact
Et cetera
Privacy Policy
Terms and Conditions
Cookie Preferences