[英]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.