当前日期插入在Grideview的编辑模式在asp.net框架 [英] Current date insertion in edit mode of Grideview in asp.net framework

查看:73
本文介绍了当前日期插入在Grideview的编辑模式在asp.net框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先生,我是asp.net的一个新的学习和努力发展驱动在asp.net中的网页本文早先在传统的ASP NAD工作正常,至今开发了一些小的数据。但是,我们的服务器的变化我不能公布我的老网页中传统的ASP。主席先生,在传统的ASP,当前日期可能被easliy在更新页面插入,但我无法在asp.net这样做。这里是我的code和我想要自动把当前日期Updation_date领域的GridView编辑模式,而更新的客户在其他领域的数据。先生请帮助。

 <表ID =form1的=服务器>
    < ASP:GridView控件=服务器ID =GridView1的AutoGenerateColumns =FALSE的DataSourceID =AccessDataSource1的DataKeyNames =IDAllowSorting =真>
        <柱体和GT;
            < ASP:CommandField中的ShowDeleteButton =FALSEShowEditButton =真ShowSelectButton =FALSE>
            < / ASP:CommandField中>
            < ASP:BoundField的数据字段=ID的HeaderText =ID只读=真InsertVisible =FALSESORTEX pression =ID可见=假>
            < / ASP:BoundField的>
            < ASP:BoundField的数据字段=acc_dateDataFormatString ={0:D}的HeaderText = SORTEX pression =acc_date&GT事故日期;
            < / ASP:BoundField的>
            < ASP:BoundField的数据字段=RLY的HeaderText =RLYSORTEX pression =RLY>
            < / ASP:BoundField的>
            < ASP:BoundField的数据字段=师的HeaderText =司SORTEX pression =司>
            < / ASP:BoundField的>
            < ASP:BoundField的数据字段=涉及的HeaderText =涉及SORTEX pression =涉及>
            < / ASP:BoundField的>
            < ASP:BoundField的数据字段=调查结果的HeaderText =发现SORTEX pression =发现>
            < / ASP:BoundField的>
            < ASP:BoundField的数据字段=责任的HeaderText =责任SORTEX pression =责任>
            < / ASP:BoundField的>
            < ASP:BoundField的数据字段=行动的HeaderText =行动SORTEX pression =行动>
            < / ASP:BoundField的>
            < ASP:BoundField的数据字段=updation_date的HeaderText =updation_dateSORTEX pression =updation_date>
            < / ASP:BoundField的>
            < ASP:BoundField的数据字段=类的HeaderText =类SORTEX pression =类可见=假>
            < / ASP:BoundField的>
            < ASP:BoundField的数据字段=年的HeaderText =年SORTEX pression =年可见=假>
            < / ASP:BoundField的>
        < /专栏>
    < / ASP:GridView的>
    < ASP:的AccessDataSource =服务器ID =AccessDataSource1的DeleteCommand =删除[事故] WHERE [ID] =?将InsertCommand =INSERT INTO [事故]([acc_date],[RLY],[师],[涉及],[结果],[责任],[动作],[updation_date],[等级],[年])VALUES (?,?,?,?,?,?,?,?,?,?)的UpdateCommand =UPDATE [事故] SET [acc_date] =?[RLY] =?[师] =?[涉及] =?[结果] =?[责任] =?,[动作] = [updation_date] = [类] = [年] =?WHERE [ID] =?数据文件=unusual.mdb的SelectCommand =SELECT * FROM [事故] WHERE([RLY] =?)ORDER BY [acc_date] DESC>
        < D​​eleteParameters>
            < ASP:参数名称=ID类型=的Int32/>
        < / DeleteParameters>
        < SelectParameters>
        < ASP:参数默认值=CRNAME =RLY类型=字符串/>
    < / SelectParameters>
        < UpdateParameters>
            < ASP:参数名称=acc_date类型=日期时间/>
            < ASP:参数名称=RLY类型=字符串/>
            < ASP:参数名称=科类型=字符串/>
            < ASP:参数名称=涉及类型=字符串/>
            < ASP:参数名称=发现类型=字符串/>
            < ASP:参数名称=责任类型=字符串/>
            < ASP:参数名称=行动类型=字符串/>
            < ASP:参数名称=updation_date类型=日期时间/>
            < ASP:参数名称=类类型=字符串/>
            < ASP:参数名称=年类型=字符串/>
            < ASP:参数名称=ID类型=的Int32/>
        < / UpdateParameters>
        < InsertParameters>
            < ASP:参数名称=acc_date类型=日期时间/>
            < ASP:参数名称=RLY类型=字符串/>
            < ASP:参数名称=科类型=字符串/>
            < ASP:参数名称=涉及类型=字符串/>
            < ASP:参数名称=发现类型=字符串/>
            < ASP:参数名称=责任类型=字符串/>
            < ASP:参数名称=行动类型=字符串/>
            < ASP:参数名称=updation_date类型=日期时间/>
            < ASP:参数名称=类类型=字符串/>
            < ASP:参数名称=年类型=字符串/>
        < / InsertParameters>
    < / ASP:&的AccessDataSource GT;


解决方案

爵士,删除的BoundField updation_date 从GridView的列。爵士此后认购 AccessDataSource1 更新事件并添加以下code到事件处理程序: e.Command.Parameters [updation_date]值= DateTime.Now; 。爵士做的插入事件一样。先生,你可以使用处理这两个事件,先生同样的方法。

补充:如果你想在一个表格显示 updation_date ,从的BoundField 更改为模板列如下:

 < ASP:的TemplateField的HeaderText =更新时间SORTEX pression =updation_date>
     <&ItemTemplate中GT;
          <%#的eval(updation_date)%>
     < / ItemTemplate中>
     <&EditItemTemplate的GT;
          <%#DateTime.Now.ToString()%GT;
     < / EditItemTemplate中>
     <&InsertTemplate则GT;
          <%#DateTime.Now.ToString()%GT;
     < / InsertTemplate则>
< / ASP:的TemplateField>

Sir, I am a new learner of asp.net and trying to develop some small data driven webpages in asp.net which were earlier developed in classic asp nad working fine till now. But change of our server I am unable to publish my old webpages in classic asp. Sir, in classic asp, current date could be inserted easliy in update page but I am unable to do so in asp.net. Here is my code and I want to put current date automatically in Updation_date field in Gridview editmode while customer update the data in other fields. Sir please help.

<form id="form1" runat="server">
    <asp:GridView runat="server" id="GridView1" AutoGenerateColumns="False" DataSourceID="AccessDataSource1" DataKeyNames="ID" AllowSorting="True">
        <Columns>
            <asp:commandfield ShowDeleteButton="False" ShowEditButton="True" ShowSelectButton="False">
            </asp:commandfield>
            <asp:boundfield DataField="ID" HeaderText="ID" ReadOnly="True" InsertVisible="False" SortExpression="ID" Visible="False">
            </asp:boundfield>
            <asp:boundfield DataField="acc_date" DataFormatString="{0:d}" HeaderText="Date of Accident" SortExpression="acc_date">
            </asp:boundfield>
            <asp:boundfield DataField="Rly" HeaderText="Rly" SortExpression="Rly">
            </asp:boundfield>
            <asp:boundfield DataField="Division" HeaderText="Division" SortExpression="Division">
            </asp:boundfield>
            <asp:boundfield DataField="Involving" HeaderText="Involving" SortExpression="Involving">
            </asp:boundfield>
            <asp:boundfield DataField="Findings" HeaderText="Findings" SortExpression="Findings">
            </asp:boundfield>
            <asp:boundfield DataField="Responsibility" HeaderText="Responsibility" SortExpression="Responsibility">
            </asp:boundfield>
            <asp:boundfield DataField="Action" HeaderText="Action" SortExpression="Action">
            </asp:boundfield>
            <asp:boundfield DataField="updation_date" HeaderText="updation_date" SortExpression="updation_date">
            </asp:boundfield>
            <asp:boundfield DataField="Class" HeaderText="Class" SortExpression="Class" Visible="false">
            </asp:boundfield>
            <asp:boundfield DataField="Year" HeaderText="Year" SortExpression="Year" Visible="false">
            </asp:boundfield>
        </Columns>
    </asp:GridView>
    <asp:AccessDataSource runat="server" ID="AccessDataSource1" DeleteCommand="DELETE FROM [accident] WHERE [ID] = ?" InsertCommand="INSERT INTO [accident] ([acc_date], [Rly], [Division], [Involving], [Findings], [Responsibility], [Action], [updation_date], [Class], [Year]) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" UpdateCommand="UPDATE [accident] SET [acc_date] = ?, [Rly] = ?, [Division] = ?, [Involving] = ?, [Findings] = ?, [Responsibility] = ?, [Action] = ?, [updation_date] = ?, [Class] = ?, [Year] = ? WHERE [ID] = ?" DataFile="unusual.mdb" SelectCommand="SELECT * FROM [accident]WHERE ([Rly] = ?) ORDER BY [acc_date] DESC">
        <DeleteParameters>
            <asp:parameter Name="ID" Type="Int32" />
        </DeleteParameters>
        <SelectParameters>
        <asp:parameter DefaultValue="CR" Name="Rly" Type="String" />
    </SelectParameters>
        <UpdateParameters>
            <asp:parameter Name="acc_date" Type="DateTime" />
            <asp:parameter Name="Rly" Type="String" />
            <asp:parameter Name="Division" Type="String" />
            <asp:parameter Name="Involving" Type="String" />
            <asp:parameter Name="Findings" Type="String" />
            <asp:parameter Name="Responsibility" Type="String" />
            <asp:parameter Name="Action" Type="String" />
            <asp:parameter Name="updation_date" Type="DateTime" />
            <asp:parameter Name="Class" Type="String" />
            <asp:parameter Name="Year" Type="String" />
            <asp:parameter Name="ID" Type="Int32" />
        </UpdateParameters>
        <InsertParameters>
            <asp:parameter Name="acc_date" Type="DateTime" />
            <asp:parameter Name="Rly" Type="String" />
            <asp:parameter Name="Division" Type="String" />
            <asp:parameter Name="Involving" Type="String" />
            <asp:parameter Name="Findings" Type="String" />
            <asp:parameter Name="Responsibility" Type="String" />
            <asp:parameter Name="Action" Type="String" />
            <asp:parameter Name="updation_date" Type="DateTime" />
            <asp:parameter Name="Class" Type="String" />
            <asp:parameter Name="Year" Type="String" />
        </InsertParameters>
    </asp:AccessDataSource>

解决方案

Sir, remove the BoundField for updation_date from GridView's columns. Sir, after that subscribe for AccessDataSource1 Updating event and add following code to that event handler: e.Command.Parameters["updation_date"].Value = DateTime.Now;. Sir, do the same with the Inserting event. Sir, you may use the same method for handling both events, sir.

Added: if you want to show updation_date in a grid, change it from BoundField to TemplateField as below:

<asp:TemplateField HeaderText="Updated Date" SortExpression="updation_date" >
     <ItemTemplate>
          <%# Eval("updation_date") %>
     </ItemTemplate>
     <EditItemTemplate>
          <%# DateTime.Now.ToString() %>
     </EditItemTemplate>
     <InsertItemTemplate>
          <%# DateTime.Now.ToString() %>
     </InsertItemTemplate>
</asp:TemplateField>

这篇关于当前日期插入在Grideview的编辑模式在asp.net框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆