简体   繁体   English

插入更新删除后,如何从页面加载刷新下拉菜单?

[英]How to refresh a dropdown menu from page load after insert update delete?

Hi I am new in c# for first assignment. 嗨,我是第一次使用C#的新手。 I had successfully load data from dropdownlist into my textfield. 我已经成功地将数据从下拉列表加载到文本字段中。 Also manage to insert, update and delete the records from database table. 还可以从数据库表中插入,更新和删除记录。 Now, I am facing this problem. 现在,我正面临着这个问题。 Each new record insert into db table, update or delete, my dropdownlist which load from Page_Load doesn't not refresh my dropdownlist. 每个新记录插入db表,更新或删除时,从Page_Load加载的我的下拉列表不会刷新我的下拉列表。

I do some research online, instead of using ajax. 我在网上进行了一些研究,而不是使用Ajax。 I may use another trick code which is DropDownList2.DataBind(); 我可能会使用另一个技巧代码DropDownList2.DataBind();。

Anyhow,it still doesn't work to refresh my Dropdown list. 无论如何,刷新我的下拉列表仍然无效。 Any expert can give me some advise. 任何专家都可以给我一些建议。 Did I place the line of code wrongly? 我把代码行放错了吗?

Below is my code for delete function. 下面是我的删除功能代码。

 connection.ConnectionString = 
 ConfigurationManager.ConnectionStrings["constr"].ToString();
 connection.Open();
 NpgsqlCommand cmd = new NpgsqlCommand();
 cmd.Connection = connection;
 cmd.CommandText = "Delete from student_db where id=@id";
 cmd.CommandType = CommandType.Text;
 cmd.Parameters.Add(new NpgsqlParameter("@id", 
 Convert.ToInt32(DropDownList2.SelectedItem.Value)));
 cmd.ExecuteNonQuery();
 cmd.Dispose();
 DropDownList2.DataBind();
 connection.Close();

The database will not fire an event to your code base when data has changed. 数据更改后,数据库不会向您的代码库触发事件。 You need to use a wrapper class like an Observable around an in-memory collection. 您需要围绕内存集合使用类似Observable的包装器类。

https://docs.microsoft.com/en-us/dotnet/api/system.collections.objectmodel.observablecollection-1?redirectedfrom=MSDN&view=netframework-4.8 https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.objectmodel.observablecollection-1?redirectedfrom=MSDN&view=netframework-4.8

If you need to be updated when other client applications update data then you'll either need to use a Message broker like RabbitMQ or present an external API for receiving notification like MVC WebAPI. 如果您需要在其他客户端应用程序更新数据时进行更新,那么您要么需要使用RabbitMQ之类的消息代理,要么需要提供一个外部API来接收诸如MVC WebAPI之类的通知。

Last option would be a re-query after an action is performed, but that could heavily impact your network traffic. 最后一个选择是在执行操作后重新查询,但这可能会严重影响您的网络流量。

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

相关问题 Windows窗体-从外部插入,删除或更新后刷新数据网格 - Windows Forms - Refresh datagrid after insert, delete or update from outside 插入并加载后如何刷新datagridview? - How to refresh a datagridview after insert and on load? 如何从左侧菜单刷新页面 - How to refresh page from left menu 页面刷新后从下拉框中保留所选项目 - Keeping selected item from dropdown box after page refresh 在页面加载事件上单击按钮(发布)后刷新页面时,下拉列表中的值不重复 - Dropdown list values not duplicating on refresh the page after button click (post) on page load event 如何从数据集中执行插入、更新、删除 - How to perform a Insert , Update, Delete from a Dataset 页面加载后刷新标签 - refresh label after page load 成功发布后如何在不刷新页面的情况下更新页面内容 - How to update page content after success post without page refresh 插入或更新或删除后刷新datagridview而不选择新的sql查询 - refresh datagridview after insert or update or delete without selecting new sql query 存储更新,插入或删除语句在context.refresh()之后影响了意外的行数(0) - Store update, insert, or delete statement affected an unexpected number of rows (0) after context.refresh()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM