简体   繁体   English

在asp.net上读写

[英]Reading & Writing in asp.net

I have a basic page which has fields that I read from the database and write to it. 我有一个基本页面,其中包含我从数据库读取并写入数据库的字段。 I would like to know where I should call the insert and read commands. 我想知道应该在哪里调用插入和读取命令。 I know about age_load but heard about page_loadcomplete. 我知道age_load,但听说过page_loadcomplete。

I noted that when the user refreshes the page, for some reason the sqldatasource loads the old data(before the write) even though new data has been loaded on the form. 我注意到,当用户刷新页面时,由于某种原因,即使新数据已加载到表单上,sqldatasource也会(在写入之前)加载旧数据。

thanks, 谢谢,

Jim 吉姆

Let me segregate first your inquiries per paragraph and give some comments. 首先让我分开您对每个段落的询问并提出一些意见。

  1. For your question where to insert or read commands, I would usually call my insert/read commands in another project/class file. 对于您在哪里插入或读取命令的问题,我通常会在另一个项目/类文件中调用我的插入/读取命令。 However, these commands are triggered in my user interface. 但是,这些命令是在我的用户界面中触发的。 That means, if I have to retrieve my records to view them, I would setup my read commands in my Data Access Project/Class then trigger them in my user interface once it loads my page. 这意味着,如果必须检索记录以查看它们,则可以在数据访问项目/类中设置读取命令,然后在加载页面后在用户界面中触发它们。 Or, if I will insert new records, I will set a method to insert up in my class project/class file then call it in my user interface. 或者,如果我要插入新记录,则将设置一个方法插入我的班级项目/班级文件中,然后在用户界面中调用它。

  2. How often does this happen? 这种情况多久发生一次? I haven't encountered this since. 从那以后我再也没有遇到过。 It's usually that once you inserted/updated a data, the datasource refreshes itself. 通常,一旦您插入/更新了数据,数据源就会刷新自身。

It's important that you understand the ASP.NET page life cycle (assuming that you're using web forms). 了解ASP.NET页面生命周期非常重要(假设您正在使用Web表单)。 With this you'll know the correct page events to use and their particular purpose. 通过此操作,您将了解要使用的正确页面事件及其特定用途。 Page_Load and Page_OnLoadComplete are not used to save changes to data, but rather retrieval and rendering of that data. Page_Load和Page_OnLoadComplete并不用于保存对数据的更改,而是用于检索和呈现该数据。

I would have the generic database calls inside a database class. 我将在数据库类内部进行通用数据库调用。 The fields in I would place into a separate class, and this class would have an associated mapping class containing the particular CRUD methods you need (eg the names of Stored Procedures to call, Parameters etc). 我中的字段将放入一个单独的类中,并且该类将具有一个关联的映射类,其中包含您需要的特定CRUD方法(例如,要调用的存储过程的名称,Parameters等)。 I'd pass this to an instance of the database class to perform the functions I want, triggered by an event from the page (eg a Button_Click event). 我将其传递给数据库类的实例以执行所需的功能,该功能由页面上的事件(例如Button_Click事件)触发。

On your second point, refreshing the page calls a HTTP GET, so your page will be reloaded. 第二点,刷新页面将调用HTTP GET,因此将重新加载页面。 This to me implies that your write is not actually occurring, check the database to ensure that it is. 对我来说,这意味着您的写操作实际上并未在进行,请检查数据库以确保确实如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM