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;
The default value of this DDL is: 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:
<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
. After insertion completes, Response.Redirect(Request.RawUrl);
is used to refresh the page and update the content of the 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. 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:
public int maxProgramID = 0;
2- Found MaxID in page_load:
string maxID = programsDataTable.AsEnumerable()
.Max(row => row["ProgramId"])
.ToString();
3- Added it to my session in btnSaveProgram_Click:
Session["AddedProgram"] = new DDLProgram { Name = programName, Value = maxProgramID + 1 };
4- Select the value automatically in page_load:
if(Session["AddedProgram"] != null) ddl.SelectedValue = Session["AddedProgram"];
Thanks @penleychan to help me out by commenting under my question.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.