In: Computer Science
Create an ASP.NET page and add ScriptManager and UpdatePanel controls. Next, add a SqlDataSource and DetailsView control within the UpdatePanel and configure them so that the visitor can.
add new records to the Books table. add two Label Web controls to this page—one outside the UpdatePanel and the other within the UpdatePanel. Have the Label Text properties assigned to the current date and time on each page load. When testing the page, note that when the page is first visited, the two Labels' values match, but when a new record is added through the DetailsView control, the Label within the UpdatePanel is updated, but one outside the UpdatePanel is no
Dear Student ,
As per the requirement submitted above , kindly find the below solution.
Here a new asp.net web application with C# is created using Visual Studio 2017 with name "Demo_BooksApplication".This application contains a web page with name "AddBook.aspx".Below are the files associated with AddBook.aspx
1.AddBook.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AddBook.aspx.cs" Inherits="Demo_BooksApplication.AddBook" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<%-- title for web page --%>
<title>Book Demo</title>
</head>
<body>
<form id="form1" runat="server">
<%--ScriptManager control --%>
<asp:ScriptManager ID="ScriptManager1"
runat="server"></asp:ScriptManager>
<div>
<%-- UpdatePanel --%>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<%-- label to display date and time --%>
<asp:Label ID="lblWithinUpdatePanel"
runat="server"></asp:Label>
<%-- details view --%>
<asp:DetailsView ID="DetailsView1" runat="server"
AllowPaging="True" AutoGenerateInsertButton="True"
AutoGenerateRows="False" DataKeyNames="BookID"
DataSourceID="SqlDataSource1" Height="50px" Width="125px">
<Fields>
<asp:BoundField DataField="BookID" HeaderText="BookID"
InsertVisible="False" ReadOnly="True" SortExpression="BookID"
/>
<asp:BoundField DataField="Title" HeaderText="Title"
SortExpression="Title" />
<asp:BoundField DataField="Author" HeaderText="Author"
SortExpression="Author" />
<asp:BoundField DataField="YearPublished"
HeaderText="YearPublished" SortExpression="YearPublished"
/>
<asp:BoundField DataField="Price" HeaderText="Price"
SortExpression="Price" />
<asp:BoundField DataField="LastReadOn" HeaderText="LastReadOn"
SortExpression="LastReadOn" />
<asp:BoundField DataField="PageCount" HeaderText="PageCount"
SortExpression="PageCount" />
</Fields>
</asp:DetailsView>
<%-- sql data source --%>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:BookDBConnectionString
%>" DeleteCommand="DELETE FROM [Books] WHERE [BookID] = @BookID"
InsertCommand="INSERT INTO [Books] ([Title], [Author],
[YearPublished], [Price], [LastReadOn], [PageCount]) VALUES
(@Title, @Author, @YearPublished, @Price, @LastReadOn, @PageCount)"
SelectCommand="SELECT * FROM [Books]" UpdateCommand="UPDATE [Books]
SET [Title] = @Title, [Author] = @Author, [YearPublished] =
@YearPublished, [Price] = @Price, [LastReadOn] = @LastReadOn,
[PageCount] = @PageCount WHERE [BookID] = @BookID">
<DeleteParameters>
<asp:Parameter Name="BookID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Title" Type="String" />
<asp:Parameter Name="Author" Type="String" />
<asp:Parameter Name="YearPublished" Type="Int32" />
<asp:Parameter Name="Price" Type="Decimal" />
<asp:Parameter Name="LastReadOn" Type="DateTime" />
<asp:Parameter Name="PageCount" Type="Int32" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="Title" Type="String" />
<asp:Parameter Name="Author" Type="String" />
<asp:Parameter Name="YearPublished" Type="Int32" />
<asp:Parameter Name="Price" Type="Decimal" />
<asp:Parameter Name="LastReadOn" Type="DateTime" />
<asp:Parameter Name="PageCount" Type="Int32" />
<asp:Parameter Name="BookID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
<br />
</ContentTemplate>
</asp:UpdatePanel>
</div>
<br />
<br />
<%-- label to display date and time --%>
<asp:Label ID="lbloutsideUpdatePanel"
runat="server"></asp:Label>
</form>
</body>
</html>
2.AddBook.aspx.cs
//namespace
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
//application namespace
namespace Demo_BooksApplication
{
public partial class AddBook : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//display date and time on label that is inside update panel
lblWithinUpdatePanel.Text ="Inside Update Panel : "+
DateTime.Now.ToLongTimeString();
//display date and time on label that is outside update panel
lbloutsideUpdatePanel.Text = "Outside Update Panel :
"+DateTime.Now.ToLongTimeString();
}
}
}
======================================================
Output : Run application using F5 and will get the screen as shown below
Screen 1 :AddBook.aspx
Screen 2 :Click on New and will get the screen as shown below
Screen 3 :Screen when timing change in the label inside update panel
NOTE : PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.