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