本文介绍了设置当前日期的FormView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要把当前的日期在我FormView控件dateadded但是当我在浏览器中加载了不显示。我使用的是落后code,但我怎么能与日期和时间显示呢?
下面是一个我的code的后面。
使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;
使用System.Web.UI程序;
使用System.Web.UI.WebControls;
命名空间LibrarySystem.AdminPage
{
公共部分类ManageBooks:System.Web.UI.Page
{
保护无效的Page_Load(对象发件人,EventArgs的发送)
{
如果(FormView1.CurrentMode == FormViewMode.Insert)
{
文本框dateadded = FormView1.FindControl(dateaddedTextBox)的文本框;
dateadded.Text = DateTime.Now.ToString(D);
}
}
}
}
下面是我的完整的设计
<%@页标题=LANGUAGE =C#的MasterPageFile =〜/ Site1.MasterAutoEventWireup =真codeBehind =Lending.aspx。 CS继承=LibrarySystem.Test%GT;
< ASP:内容ID =内容1ContentPlaceHolderID =头=服务器>
< / ASP:内容>
< ASP:内容ID =内容2ContentPlaceHolderID =ContentPlaceHolder1=服务器>
< H3>
图书与LT的贷款; / H3 GT&;
&所述p为H.;
< ASP:FormView控件ID =FormView1=服务器的DataSourceID =lendDataSourceDefaultMode =插入OnDataBound =FormView1_DataBound>
<&EditItemTemplate的GT; 图书ID / ISBN:
< ASP:文本框ID =bookidTextBox=服务器文本='<%#绑定(BOOKID)%>' />
< BR /> 书名:
< ASP:文本框ID =booktitleTextBox=服务器
文字='<%#绑定(BOOKTITLE)%>' />
< BR /> 员工ID:
< ASP:文本框ID =employeeidTextBox=服务器
文字='<%#绑定(雇员)%>' />
< BR /> 部:
< ASP:文本框ID =departmentTextBox=服务器
文字='<%#绑定(部)%GT;' />
< BR /> 借用日期:
< ASP:文本框ID =dateborrowedTextBox=服务器
文字='<%#绑定(dateborrowed)%>' />
< BR /> < ASP:LinkButton的ID =UpdateButton=服务器的CausesValidation =真
的CommandName =更新文本=更新/>
&安培; NBSP;< ASP:LinkButton的ID =UpdateCancelButton=服务器
的CausesValidation =FALSE的CommandName =取消文本=取消/>
< / EditItemTemplate中> <&InsertTemplate则GT; 书:
< ASP:DropDownList的ID =DropDownList1=服务器
的DataSourceID =booktitleDataSourceDataTextField =BOOKTITLE
DataValueField =BOOKID的SelectedValue ='<%#绑定(BOOKID,{0})%GT;'>
< / ASP:DropDownList的> < ASP:SqlDataSource的ID =booktitleDataSource=服务器
的ConnectionString =下;%$的ConnectionStrings:LibrarySystemConnectionString%>中
的SelectCommand =SELECT [BOOKID],[BOOKTITLE] FROM [TblBooks]>
< / ASP:SqlDataSource的>
< BR /> 员工ID:
< ASP:文本框ID =employeeidTextBox=服务器
文字='<%#绑定(雇员)%>' />
< ASP:的RequiredFieldValidator ID =RequiredFieldValidator1=服务器的ErrorMessage =*必填的ControlToValidate =employeeidTextBox的ValidationGroup =lendbook>
< / ASP:&的RequiredFieldValidator GT;
< BR /> 部:
< ASP:文本框ID =departmentTextBox=服务器
文字='<%#绑定(部)%GT;' />
< ASP:的RequiredFieldValidator ID =RequiredFieldValidator2=服务器的ErrorMessage =*必填的ControlToValidate =departmentTextBox的ValidationGroup =lendbook>
< / ASP:&的RequiredFieldValidator GT;
< BR /> 借用日期:
< ASP:文本框ID =dateborrowedTextBox=服务器
文字='<%#绑定(dateborrowed)%>' />
< ASP:的RequiredFieldValidator ID =RequiredFieldValidator3=服务器的ErrorMessage =*必填的ControlToValidate =dateborrowedTextBox的ValidationGroup =lendbook>
< / ASP:&的RequiredFieldValidator GT;
< BR /> < ASP:LinkButton的ID =InsertButton=服务器的CausesValidation =真的ValidationGroup =lendbook
的CommandName =插入文本=插入/>
&安培; NBSP;< ASP:LinkButton的ID =InsertCancelButton=服务器的CausesValidation =FALSE
的CommandName =取消文本=取消/>
< / InsertTemplate则> <&ItemTemplate中GT;
< ASP:LinkButton的ID =NewButton=服务器的CausesValidation =FALSE
的CommandName =新文本=新建/>
< / ItemTemplate中> < EmptyDataTemplate>
< ASP:LinkButton的ID =NewButton=服务器的CausesValidation =FALSE
的CommandName =新文本=新建/>
< / EmptyDataTemplate>
< / ASP:FormView控件>
&所述; / P>
&所述p为H.;
< ASP:SqlDataSource的ID =lendDataSource=服务器
的ConnectionString =下;%$的ConnectionStrings:LibrarySystemConnectionString%>中
DeleteCommand会=删除[LendTable],其中[lenid] = @lenid
将InsertCommand =INSERT INTO [LendTable]([BOOKID],[雇员],[部门],[dateborrowed],[datereturned])VALUES(@bookid,@EmployeeID,@department,@dateborrowed,@datereturned)
的SelectCommand =SELECT dbo.LendTable.bookid,dbo.TblBooks.booktitle,dbo.LendTable.employeeid,dbo.LendTable.department,dbo.LendTable.dateborrowed FROM dbo.LendTable INNER JOIN dbo.TblBooks ON dbo.LendTable.bookid = DBO .TblBooks.bookid 更新命令=UPDATE [LendTable] SET [BOOKID] = @bookid,[雇员] = @EmployeeID,[部门] = @department,[dateborrowed] = @dateborrowed,[datereturned] = @datereturned WHERE [lenid] = @lenid >
< DeleteParameters>
< ASP:参数名称=lenidTYPE =的Int32/>
< / DeleteParameters>
< UpdateParameters>
< ASP:参数名称=BOOKIDTYPE =的Int64/>
< ASP:参数名称=雇员类型=字符串/>
< ASP:参数名称=部门类型=字符串/>
< ASP:参数名称=dateborrowed类型=日期时间/>
< ASP:参数名称=datereturned类型=日期时间/>
< ASP:参数名称=lenidTYPE =的Int32/>
< / UpdateParameters>
< InsertParameters>
< ASP:参数名称=BOOKIDTYPE =的Int64/>
< ASP:参数名称=雇员类型=字符串/>
< ASP:参数名称=部门类型=字符串/>
< ASP:参数名称=dateborrowed类型=日期时间/>
< ASP:参数名称=datereturned类型=日期时间/>
< / InsertParameters>
< / ASP:SqlDataSource的>
&所述; / P>
&所述p为H.;
< ASP:GridView控件ID =GridView1=服务器AllowPaging =真
AllowSorting =真的AutoGenerateColumns =FALSE的cellpadding =4
的DataKeyNames =lenid的DataSourceID =lendgridviewDataSource前景色=#333333
网格线=无>
< RowStyle的BackColor =#EFF3FB/>
<柱体和GT;
< ASP:CommandField中的ShowDeleteButton =真ShowEditButton =真/>
< ASP:BoundField的数据字段=BOOKID的HeaderText =书ID / ISBN
SORTEX pression =BOOKID/>
< ASP:BoundField的数据字段=BOOKTITLE的HeaderText =书名
SORTEX pression =BOOKTITLE/>
< ASP:BoundField的数据字段=雇员的HeaderText =员工ID
SORTEX pression =雇员/>
< ASP:BoundField的数据字段=部门的HeaderText =部门
SORTEX pression =部门/>
< ASP:BoundField的数据字段=dateborrowed的HeaderText =借来的时间
SORTEX pression =dateborrowed/>
< ASP:BoundField的数据字段=datereturned的HeaderText =返回的日期
NullDisplayText = - 尚未恢复 - SORTEX pression =datereturned/>
< /专栏>
< FooterStyle背景色=#507CD1FONT-粗体=真前景色=白/>
< PagerStyle背景色=#2461BF前景色=白HorizontalAlign =中心/>
< SelectedRowStyle背景色=#D1DDF1FONT-粗体=真前景色=#333333/>
< HeaderStyle背景色=#507CD1FONT-粗体=真前景色=白/>
< EditRowStyle背景色=#2461BF/>
< AlternatingRowStyle背景色=白/>
< / ASP:GridView的>
< ASP:SqlDataSource的ID =lendgridviewDataSource=服务器
的ConnectionString =下;%$的ConnectionStrings:LibrarySystemConnectionString%>中
DeleteCommand会=删除[LendTable],其中[lenid] = @lenid
将InsertCommand =INSERT INTO [LendTable]([BOOKID],[部门],[雇员],[dateborrowed],[datereturned])VALUES(@bookid,@department,@EmployeeID,@dateborrowed,@datereturned)
的SelectCommand =SELECT dbo.LendTable.lenid,dbo.LendTable.bookid,dbo.LendTable.department,dbo.LendTable.EmployeeID,dbo.LendTable.dateborrowed,dbo.LendTable.datereturned,dbo.TblBooks.booktitle FROM dbo.LendTable INNER JOIN dbo.TblBooks ON dbo.LendTable.bookid = dbo.TblBooks.bookid
更新命令=UPDATE [LendTable] SET [BOOKID] = @bookid,[部门] = @department,[雇员] = @EmployeeID,[dateborrowed] = @dateborrowed,[datereturned] = @datereturned WHERE [lenid] = @lenid >
< DeleteParameters>
< ASP:参数名称=lenidTYPE =的Int32/>
< / DeleteParameters>
< UpdateParameters>
< ASP:参数名称=BOOKIDTYPE =的Int64/>
< ASP:参数名称=部门类型=字符串/>
< ASP:参数名称=雇员类型=字符串/>
< ASP:参数名称=dateborrowed类型=日期时间/>
< ASP:参数名称=datereturned类型=日期时间/>
< ASP:参数名称=lenidTYPE =的Int32/>
< / UpdateParameters>
< InsertParameters>
< ASP:参数名称=BOOKIDTYPE =的Int64/>
< ASP:参数名称=部门类型=字符串/>
< ASP:参数名称=雇员类型=字符串/>
< ASP:参数名称=dateborrowed类型=日期时间/>
< ASP:参数名称=datereturned类型=日期时间/>
< / InsertParameters>
< / ASP:SqlDataSource的>
&所述; / P>
&所述p为H.;
< ASP:超链接ID =HyperLink4=服务器
NavigateUrl =〜/行政/ Returning.aspx>返回< / ASP:超链接>
&所述; / P>
< ASP:超链接ID =HyperLink5=服务器
NavigateUrl =〜/行政/ AdminPage.aspx>返回管理页面< / ASP:超链接>
&所述p为H.;
&放大器; NBSP;&下; / P> < / ASP:内容>
解决方案
您必须使用数据绑定事件
为...
保护无效FormView1_DataBound(对象发件人,EventArgs的发送)
{
如果(FormView1.CurrentMode == FormViewMode.Insert)
{
文本框dateadded = FormView1.FindControl(dateaddedTextBox)的文本框;
dateadded.Text = DateTime.Now.ToString(D);
}
}
编辑:您需要通过你的日期列在DetailsView的插入方法
保护无效DetailsView1_ItemInserting(对象发件人,DetailsViewInsertEventArgs E)
{
e.Values [DateColumnName] =((文本框)DetailsView1.FindControl(dateaddedTextBox))文本。
}
I want to put the current date in my formview 'dateadded' but its not showing when I load it in the browser. I'm using code behind but how can I display it with date and time?
Here's a my code behind.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace LibrarySystem.AdminPage
{
public partial class ManageBooks : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(FormView1.CurrentMode == FormViewMode.Insert)
{
TextBox dateadded = FormView1.FindControl("dateaddedTextBox") as TextBox;
dateadded.Text = DateTime.Now.ToString("d");
}
}
}
}
Here's my complete design
<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="Lending.aspx.cs" Inherits="LibrarySystem.Test" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<h3>
Lending of Books</h3>
<p>
<asp:FormView ID="FormView1" runat="server" DataSourceID="lendDataSource" DefaultMode="Insert" OnDataBound="FormView1_DataBound">
<EditItemTemplate>
Book ID/ISBN:
<asp:TextBox ID="bookidTextBox" runat="server" Text='<%# Bind("bookid") %>' />
<br />
Book Title:
<asp:TextBox ID="booktitleTextBox" runat="server"
Text='<%# Bind("booktitle") %>' />
<br />
Employee ID:
<asp:TextBox ID="employeeidTextBox" runat="server"
Text='<%# Bind("employeeid") %>' />
<br />
Department:
<asp:TextBox ID="departmentTextBox" runat="server"
Text='<%# Bind("department") %>' />
<br />
Date borrowed:
<asp:TextBox ID="dateborrowedTextBox" runat="server"
Text='<%# Bind("dateborrowed") %>' />
<br />
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True"
CommandName="Update" Text="Update" />
<asp:LinkButton ID="UpdateCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
<InsertItemTemplate>
Book:
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="booktitleDataSource" DataTextField="booktitle"
DataValueField="bookid" SelectedValue='<%# Bind("bookid", "{0}") %>'>
</asp:DropDownList>
<asp:SqlDataSource ID="booktitleDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:LibrarySystemConnectionString %>"
SelectCommand="SELECT [bookid], [booktitle] FROM [TblBooks]">
</asp:SqlDataSource>
<br />
Employee ID:
<asp:TextBox ID="employeeidTextBox" runat="server"
Text='<%# Bind("employeeid") %>' />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="* Required" ControlToValidate="employeeidTextBox" ValidationGroup="lendbook">
</asp:RequiredFieldValidator>
<br />
Department:
<asp:TextBox ID="departmentTextBox" runat="server"
Text='<%# Bind("department") %>' />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="* Required" ControlToValidate="departmentTextBox" ValidationGroup="lendbook">
</asp:RequiredFieldValidator>
<br />
Date borrowed:
<asp:TextBox ID="dateborrowedTextBox" runat="server"
Text='<%# Bind("dateborrowed") %>' />
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="* Required" ControlToValidate="dateborrowedTextBox" ValidationGroup="lendbook">
</asp:RequiredFieldValidator>
<br />
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" ValidationGroup="lendbook"
CommandName="Insert" Text="Insert" />
<asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False"
CommandName="Cancel" Text="Cancel" />
</InsertItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False"
CommandName="New" Text="New" />
</ItemTemplate>
<EmptyDataTemplate>
<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False"
CommandName="New" Text="New" />
</EmptyDataTemplate>
</asp:FormView>
</p>
<p>
<asp:SqlDataSource ID="lendDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:LibrarySystemConnectionString %>"
DeleteCommand="DELETE FROM [LendTable] WHERE [lenid] = @lenid"
InsertCommand="INSERT INTO [LendTable] ([bookid], [employeeid], [department], [dateborrowed], [datereturned]) VALUES (@bookid, @employeeid, @department, @dateborrowed, @datereturned)"
SelectCommand="SELECT dbo.LendTable.bookid, dbo.TblBooks.booktitle, dbo.LendTable.employeeid, dbo.LendTable.department, dbo.LendTable.dateborrowed FROM dbo.LendTable INNER JOIN dbo.TblBooks ON dbo.LendTable.bookid = dbo.TblBooks.bookid"
UpdateCommand="UPDATE [LendTable] SET [bookid] = @bookid, [employeeid] = @employeeid, [department] = @department, [dateborrowed] = @dateborrowed, [datereturned] = @datereturned WHERE [lenid] = @lenid">
<DeleteParameters>
<asp:Parameter Name="lenid" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="bookid" Type="Int64" />
<asp:Parameter Name="employeeid" Type="string" />
<asp:Parameter Name="department" Type="String" />
<asp:Parameter Name="dateborrowed" Type="DateTime" />
<asp:Parameter Name="datereturned" Type="DateTime" />
<asp:Parameter Name="lenid" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="bookid" Type="Int64" />
<asp:Parameter Name="employeeid" Type="string" />
<asp:Parameter Name="department" Type="String" />
<asp:Parameter Name="dateborrowed" Type="DateTime" />
<asp:Parameter Name="datereturned" Type="DateTime" />
</InsertParameters>
</asp:SqlDataSource>
</p>
<p>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" CellPadding="4"
DataKeyNames="lenid" DataSourceID="lendgridviewDataSource" ForeColor="#333333"
GridLines="None">
<RowStyle BackColor="#EFF3FB" />
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="bookid" HeaderText="Book ID/ISBN"
SortExpression="bookid" />
<asp:BoundField DataField="booktitle" HeaderText="Book Title"
SortExpression="booktitle" />
<asp:BoundField DataField="EmployeeID" HeaderText="Employee ID"
SortExpression="EmployeeID" />
<asp:BoundField DataField="department" HeaderText="Department"
SortExpression="department" />
<asp:BoundField DataField="dateborrowed" HeaderText="Date Borrowed"
SortExpression="dateborrowed" />
<asp:BoundField DataField="datereturned" HeaderText="Date Returned"
NullDisplayText="-- not yet returned --" SortExpression="datereturned" />
</Columns>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
<asp:SqlDataSource ID="lendgridviewDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:LibrarySystemConnectionString %>"
DeleteCommand="DELETE FROM [LendTable] WHERE [lenid] = @lenid"
InsertCommand="INSERT INTO [LendTable] ([bookid], [department], [EmployeeID], [dateborrowed], [datereturned]) VALUES (@bookid, @department, @EmployeeID, @dateborrowed, @datereturned)"
SelectCommand="SELECT dbo.LendTable.lenid, dbo.LendTable.bookid, dbo.LendTable.department, dbo.LendTable.EmployeeID, dbo.LendTable.dateborrowed, dbo.LendTable.datereturned, dbo.TblBooks.booktitle FROM dbo.LendTable INNER JOIN dbo.TblBooks ON dbo.LendTable.bookid = dbo.TblBooks.bookid"
UpdateCommand="UPDATE [LendTable] SET [bookid] = @bookid, [department] = @department, [EmployeeID] = @EmployeeID, [dateborrowed] = @dateborrowed, [datereturned] = @datereturned WHERE [lenid] = @lenid">
<DeleteParameters>
<asp:Parameter Name="lenid" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="bookid" Type="Int64" />
<asp:Parameter Name="department" Type="String" />
<asp:Parameter Name="EmployeeID" Type="String" />
<asp:Parameter Name="dateborrowed" Type="DateTime" />
<asp:Parameter Name="datereturned" Type="DateTime" />
<asp:Parameter Name="lenid" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="bookid" Type="Int64" />
<asp:Parameter Name="department" Type="String" />
<asp:Parameter Name="EmployeeID" Type="String" />
<asp:Parameter Name="dateborrowed" Type="DateTime" />
<asp:Parameter Name="datereturned" Type="DateTime" />
</InsertParameters>
</asp:SqlDataSource>
</p>
<p>
<asp:HyperLink ID="HyperLink4" runat="server"
NavigateUrl="~/Admin/Returning.aspx">Returning</asp:HyperLink>
</p>
<asp:HyperLink ID="HyperLink5" runat="server"
NavigateUrl="~/Admin/AdminPage.aspx">Back to Admin Page</asp:HyperLink>
<p>
</p>
</asp:Content>
解决方案
You have to use the Databound event
for that...
protected void FormView1_DataBound(object sender, EventArgs e)
{
if (FormView1.CurrentMode == FormViewMode.Insert)
{
TextBox dateadded = FormView1.FindControl("dateaddedTextBox") as TextBox;
dateadded.Text = DateTime.Now.ToString("d");
}
}
Edit: You need to pass your Date Column in the inserting method of the DetailsView
protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
e.Values["DateColumnName"] = ((TextBox)DetailsView1.FindControl("dateaddedTextBox")).Text;
}
这篇关于设置当前日期的FormView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!