【问题标题】:Current date insertion in edit mode of Grideview in asp.net frameworkasp.net框架中Gridview编辑模式下的当前数据插入
【发布时间】:2011-12-06 19:47:04
【问题描述】:

先生,我是 asp.net 的新学习者,正在尝试在 asp.net 中开发一些小型数据驱动的网页,这些网页早期是在经典的 asp 中开发的,到目前为止运行良好。但是更改我们的服务器我无法在经典 asp 中发布我的旧网页。先生,在经典的 asp 中,当前日期可以很容易地插入到更新页面中,但我无法在 asp.net 中这样做。这是我的代码,我想将当前日期自动放入 Gridview 编辑模式的 Updation_date 字段中,同时客户更新其他字段中的数据。先生请帮忙。

<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>

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    处理 RowEditing 事件。您将有权访问正在编辑的行的字段。

    【讨论】:

      【解决方案2】:

      先生,请从 GridView 的列中删除 BoundField for updation_date。先生,之后订阅AccessDataSource1Updating 事件并将以下代码添加到该事件处理程序:e.Command.Parameters["updation_date"].Value = DateTime.Now;。先生,请对 Inserting 事件做同样的事情。先生,您可以使用相同的方法来处理这两个事件,先生。

      补充:如果要在网格中显示updation_date,请将其从BoundField更改为TemplateField,如下所示:

      <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>
      

      【讨论】:

      • 发现此错误 - 编译器错误消息:CS1061:“System.Web.UI.WebControls.GridViewUpdatedEventArgs”不包含“Command”的定义,并且没有扩展方法“Command”接受第一个参数可以找到类型“System.Web.UI.WebControls.GridViewUpdatedEventArgs”(您是否缺少 using 指令或程序集引用?)
      • 你看我的回答了吗?为什么要订阅 GridView 的 Updated 事件,虽然我建议订阅 SqlDataSourceUpdating 事件?
      • 先生,如果可能,请为 SqlDataSource 的更新事件编写完整的代码。我不熟悉事件处理程序。我会非常感谢你。
      猜你喜欢
      • 1970-01-01
      • 2014-10-04
      • 1970-01-01
      • 1970-01-01
      • 2018-03-02
      • 2021-09-22
      • 2013-07-08
      • 1970-01-01
      • 2011-08-31
      相关资源
      最近更新 更多