简体   繁体   English

在ASP.NET中刷新页面后如何将用户添加的项目带入下拉列表?

[英]How to bring user-added item to dropdownlist after page refresh in asp.net?

I have a dropdownlist in my aspx page: ddlProgramList , which is loaded in page_load() event from Sql Server via the query: Select Name from Programs; 我的aspx页面中有一个dropdownlist: ddlProgramList ,它是通过查询从Sql Server的page_load()事件中加载的: Select Name from Programs; The default value of this DDL is: Select Program 此DDL的默认值为: Select Program

In the same page, I have a button in aspx like below, where a user can add a new Program if it does not exist in the current dropdownlist options: 在同一页面中,我在aspx中有一个如下所示的按钮,如果当前的下拉列表选项中不存在新的程序,用户可以在其中添加:

<asp:TextBox ID="txtPrgLabel" runat="server" style="width:295px"></asp:TextBox>
<asp:Button ID="btnSaveProgram" runat="server" Text ="Save Program" OnClick="btnSaveProgram_Click" />

btnSaveProgram_Click function basically does an insertion to the related SQL table with the content of txtPrgLabel . btnSaveProgram_Click函数基本上是使用txtPrgLabel的内容插入相关的SQL表。 After insertion completes, Response.Redirect(Request.RawUrl); 插入完成后, Response.Redirect(Request.RawUrl); is used to refresh the page and update the content of the DDL. 用于刷新页面和更新DDL的内容。 This process works successfully. 此过程成功进行。

What I want is: After a user adds a program, when page is refreshed, the DDL should automatically bring the recently added program instead of the default "Select Program" content. 我想要的是:用户添加程序后,刷新页面时,DDL应该自动带入最近添加的程序,而不是默认的“选择程序”内容。 I could not find a way to manage this. 我找不到解决此问题的方法。 Any help or advice would be appreciated. 任何帮助或建议,将不胜感激。

Here are the steps to manage this demand: 以下是管理此需求的步骤:

1- I've defined a global variable: 1-我定义了一个全局变量:

public int maxProgramID = 0;

2- Found MaxID in page_load: 2-在page_load中找到了MaxID:

string maxID = programsDataTable.AsEnumerable()
.Max(row => row["ProgramId"])
.ToString();

3- Added it to my session in btnSaveProgram_Click: 3-将其添加到我在btnSaveProgram_Click中的会话中:

Session["AddedProgram"] = new DDLProgram { Name = programName, Value = maxProgramID + 1 };

4- Select the value automatically in page_load: 4-在page_load中自动选择值:

if(Session["AddedProgram"] != null) ddl.SelectedValue = Session["AddedProgram"];

Thanks @penleychan to help me out by commenting under my question. 感谢@penleychan通过在我的问题下发表评论来帮助我。

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

相关问题 从ASP.NET C#中的另一个下拉列表中选择项目后如何将项目加载到下拉列表中 - How to load items into dropdownlist after selecting item from another dropdownlist in asp.net c# 刷新页面后,下拉列表中的项目丢失,asp.net的下拉列表中只有一个项目 - Items in dropdownlist is lost , only one item is coming in dropdown list in asp.net after page is refreshed ASP.NET 控件不会在页面刷新时动态添加到面板 - ASP.NET Controls are not dynamically added to the panel on page refresh 如何根据ASP.NET中另一个下拉列表中的用户选择禁用下拉列表项? - How do I disable a dropdownlist item based on a user selection on another drop down in ASP.NET? 如何更新下拉列表中的选定项asp.net c# - how to update a selected item in a dropdownlist asp.net c# asp.net:如何从下拉列表中删除项目? - asp.net: How can I remove an item from a dropdownlist? 如何在ASP.NET的Dropdownlist上获取当前项目 - How to get current item on Dropdownlist for ASP.NET 将数据从ASP.NET/C#中的SQL带入DropDownList? - Bring data into DropDownList from SQL in ASP.NET / C#? 我如何检查一个DropDownList包含asp.net中的项目 - How can I check a DropDownList for containing item in asp.net 如何在asp.net中将图表标题显示为下拉列表所选项目 - how to display chart title as dropdownlist selected item in asp.net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM