I am developing a web page in ASP.NET.
It consists of a master page which has the following line:
<div id="content">
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
This so that each created page shares the information of menus and other things, and I use this ContentPlaceHolder to add the respective information of each page.
I have a page where I am using a DropDownList, here is the design code:
<%@ Page Title="" Language="C#" MasterPageFile="~/Pages/Master/MasterPage.master" AutoEventWireup="true" CodeFile="Users-Search.aspx.cs" Inherits="Pages_Users_Users_Search" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div>
<h2>Seleccione una opción</h2>
<asp:DropDownList ID="CmbUsers" runat="server" AutoPostBack="True" OnSelectedIndexChanged="CmbUsers_SelectedIndexChanged" ViewStateMode="Enabled"></asp:DropDownList>
</div>
<div>
<asp:Button ID="BtnSearch" runat="server" Text="Busar" OnClick="BtnSearch_Click" />
</div>
</asp:Content>
And the code in C # is as follows:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Pages_Users_Users_Search : System.Web.UI.Page
{
public string user =
ConfigurationManager.AppSettings.Get("User");
public string search = ConfigurationManager.AppSettings.Get("User2Search");
public ListItem Coords = new ListItem("Coordinadores", "0");
public ListItem Admins = new ListItem("Administradores", "1");
public ListItem Users = new ListItem("Usuarios", "2");
protected void Page_Load(object sender, EventArgs e)
{
switch (user)
{
case "Coordinator":
CmbUsers.Items.Clear();
CmbUsers.Items.Add(Coords);
CmbUsers.Items.Add(Admins);
CmbUsers.Items.Add(Users);
break;
case "Administrator":
CmbUsers.Items.Clear();
CmbUsers.Items.Add(Users);
break;
}
if (!IsPostBack)
{
}
else
{
search = ConfigurationManager.AppSettings.Get("User2Search");
switch (search)
{
case "Coordinator":
CmbUsers.SelectedIndex = 0;
break;
case "Administrator":
CmbUsers.SelectedIndex = 1;
break;
case "User":
CmbUsers.SelectedIndex = 2;
break;
}
}
}
protected void BtnSearch_Click(object sender, EventArgs e)
{
string CodeUserSearch = TxtCode.Text.Trim(), TypeUserSearch = ConfigurationManager.AppSettings.Get("User2Search");
}
protected void CmbUsers_SelectedIndexChanged(object sender, EventArgs e)
{
int index = CmbUsers.SelectedIndex;
if (index == 0) { ConfigurationManager.AppSettings.Set("User2Search", "Coordinator"); }
else if (index == 1) { ConfigurationManager.AppSettings.Set("User2Search", "Administrator"); }
else if (index == 2) { ConfigurationManager.AppSettings.Set("User2Search", "User"); }
}
}
Basically what it does is, according to a data from a previous page, load the data in the list as appropriate.
Then there is a button that allows you to search a DB according to the value selected in the list. For that is the variable "TypeUserSearch".
Initially I had matched that variable to the value selected in the list, but if I did not do PostBack when changing the value of the list the selected value was always the default.
Something like:
Load values (V1, V2, V3, V4)
Initial value: V1
Select value V2
Selected value V1
I was always going to take the first value if the PostBack was not done. So I activated the option and it remained the same, so what I did was that after the PostBack I saved the selected value from the list in a variable in the Web.Config, the variable "User2Search", and then in the PageLoad if a PostBack happened, loaded the index of the list as needed (for purely aesthetic purposes, that by reloading the page the user would continue to see the value he had selected) and then pressing the button used the variable stored in the Web.Config. Everything worked correctly and in all the cases that I tried it worked correctly.
But after changing another page completely different this stopped working (without modifying anything to this page) and now whenever it comes back from the PostBack it returns to the default value and never changes.
Any way to preserve the value that the user selected after doing the PostBack or to be able to take the value without having to do a PostBack ???