If you are using ASP .NET DetailsView control and use a TemplateField containing a textbox for displaying the fields text and a LinkButton to update the textbox on postback, you should use the DetailsView.ItemCommand event and the DetailsView.FindControl method to set the text of the textbox or label, depending on the DetailsView.DefaultMode.

It uses the ADO .NET Entitiy Framework 4.0, declarative binding and the Microsoft SQL Server 2008 R2 RTM sample database AdventureWorks. The page is the “detail” part of a Master – Detail view, so you must supply the ProductCategory to show in the request querystring, like http://localhost:52109/ProductCategoryDetails.aspx?Id=4

Code

protected void PageDetailsView_ItemCommand(object sender, DetailsViewCommandEventArgs e)
        {
            if (this.PageDetailsView.DefaultMode == DetailsViewMode.ReadOnly)
            {
                if (e.CommandName.Equals("GenerateModifiedDate", StringComparison.CurrentCultureIgnoreCase))
                {
                    Label modifiedDateLabel = this.PageDetailsView.FindControl("ModifiedDateLabel") as Label;
                    modifiedDateLabel.Text = DateTime.Now.ToString();
                }
            }
            else
            {
                if (e.CommandName.Equals("GenerateModifiedDate", StringComparison.CurrentCultureIgnoreCase))
                {
                    TextBox modifiedDateTextbox = this.PageDetailsView.FindControl("ModifiedDateTextbox") as TextBox;
                    modifiedDateTextbox.Text = DateTime.Now.ToString();
                }
            }
        }

 

Screendump

image

By clicking on the Generate LinkButton in the DetailsView the ModifiedDate will be updated.

 

ProductCategoryDetails.aspx

 

<%@ Page MasterPageFile="~/Site.master" Language="C#" AutoEventWireup="true" CodeBehind="ProductCategoryDetails.aspx.cs" Inherits="Demo.WebApplication.ProductCategoryDetails" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
    <link href="Styles/ProductCategoryDetails.css" rel="stylesheet" type="text/css" />
</asp:Content>
<asp:Content ID="PageContent" ContentPlaceHolderID="MainContent" runat="server">
    <asp:DetailsView 
        ID="PageDetailsView" 
        runat="server"
        DataSourceID="PageDataSource"
        AllowPaging="false"
        AutoGenerateDeleteButton="true"
        AutoGenerateEditButton="true"
        AutoGenerateInsertButton="true"
        AutoGenerateRows="false"
        EmptyDataText="No record found!"
        DefaultMode="Edit"
        DataKeyNames="ProductCategoryID" 
        onitemcommand="PageDetailsView_ItemCommand">
        <Fields>
            <asp:BoundField
                DataField="ProductCategoryID"
                HeaderText="ProductCategoryID">
            </asp:BoundField>
            <asp:BoundField
                DataField="Name"
                HeaderText="Name">
            </asp:BoundField>
            <asp:BoundField
                DataField="rowguid"
                HeaderText="rowguid">
            </asp:BoundField>
            <asp:TemplateField>
                <HeaderTemplate>
                    <asp:Label ID="ModifiedDateHeaderLabel" runat="server" Text="ModifiedDate"></asp:Label>
                </HeaderTemplate>
                    <ItemTemplate>
                    <div>
                    <asp:Label ID="ModifiedDateLabel" runat="server" Text='<%# Bind("ModifiedDate") %>'></asp:Label>
                    <asp:LinkButton ID="ModifiedDateLinkButton" runat="server" CssClass="generateLinkButton" CommandName="GenerateModifiedDate">Generate</asp:LinkButton>
                    </div>
                </ItemTemplate>
                <EditItemTemplate>
                    <div>
                    <asp:TextBox ID="ModifiedDateTextBox" runat="server" Text='<%# Bind("ModifiedDate") %>'></asp:TextBox>
                    <asp:LinkButton ID="ModifiedDateLinkButton" runat="server" CssClass="generateLinkButton" CommandName="GenerateModifiedDate">Generate</asp:LinkButton>
                    </div>
                </EditItemTemplate>
            </asp:TemplateField>
        </Fields>
    </asp:DetailsView>
    <asp:EntityDataSource 
        ID="PageDataSource" 
        runat="server" 
        EnableDelete="true" 
        EnableInsert="true" 
        EnableUpdate="true"
        ConnectionString="name=AdventureWorksEntities"
        DefaultContainerName="AdventureWorksEntities"
        EntitySetName="ProductCategory"
        Where="it.[ProductCategoryID]=@ProductCategoryID">
        <WhereParameters> 
            <asp:QueryStringParameter DbType="Int32" Name="ProductCategoryID" QueryStringField="Id" /> 
        </WhereParameters> 
    </asp:EntityDataSource>
</asp:Content>



 

This is part of a very simple ASP .NET demo web application I created, see https://www.roelvanlisdonk.nl/?p=1521

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.