gpt4 book ai didi

C# DropDownList 和 session [""]

转载 作者:太空宇宙 更新时间:2023-11-03 20:33:22 24 4
gpt4 key购买 nike

每个人。

我在从下拉列表中选择值时遇到一点问题。当用户输入他/她的凭据并选择数据源名称时,它们都存储在 session 中。当用户稍后返回登录页面时,我希望他/她看到最后选择的数据源名称。它是第一次工作,但是当我尝试更改数据源名称时,它不会更改,但会出现之前选择的值。你能帮我解决这个问题吗?谢谢!

默认.aspx

public partial class _Default : System.Web.UI.Page 
{
private dbConnection connection = new dbConnection();
private string[,] errorMessage =
{ {"Expired", "Unsuccessful", "Incorrect"},
{"Your Logon Session Has Expired", "Invalid Username or Password",
"Selected Data Source is not supported within this application"} };

protected void Page_Load(object sender, EventArgs e)
{

ddlDatabase.DataTextField = "Value";
ddlDatabase.DataValueField = "Key";
ddlDatabase.DataSource = dbList.GetDbList();
ddlDatabase.DataBind();

Response.Buffer = true;

if (Session["Error"] != null)
ErrorMessage();

if (Session["Datasource"] != null)
ddlDatabase.SelectedValue = Session["Datasource"].ToString();
}

protected void btnLogin_Click(object sender, EventArgs e)
{
string dataSource = ddlDatabase.SelectedValue;
string userID = txtUserID.Text;
string password = txtPassword.Text;

connection = new dbConnection(dataSource, userID, password);

if (connection.IsValid())
{
try
{
connection.GetConnection().Open();
Session["Username"] = userID;
Session["Password"] = password;
Session["Datasource"] = ddlDatabase.SelectedValue;
Response.Clear();
Response.Redirect("main.aspx", false);
}
catch (Exception ex)
{
lblSessionInfo.Text = ex.Message;
Session["Username"] = null;
Session["Password"] = null;
Session["Error"] = "Unsuccessful";
Response.Clear();
Response.Redirect("default.aspx");
}
}
else
{
Session["Username"] = null;
Session["Password"] = null;
Session["Datasource"] = null;
Session["Error"] = "Incorrect";
Response.Clear();
Response.Redirect(Request.Url.ToString(), true);
}
}

protected void ErrorMessage()
{
int length = errorMessage.Length / 2;

for (int i = 0; i < length; i++)
if (Session["Error"].ToString() == errorMessage[0, i])
lblSessionInfo.Text = errorMessage[1, i];
}
}

main.aspx(注销)

public partial class main : System.Web.UI.Page
{
dbConnection connection = new dbConnection();

protected void Page_Load(object sender, EventArgs e)
{
Response.Buffer = true;
if (Session["Username"] == null)
{
Session["Error"] = "Expired";
Response.Clear();
Response.Redirect("default.aspx");
}
else
{
Session["Error"] = null;
}

Label1.Text = Session["Datasource"].ToString();
}
protected void btnLogout_Click(object sender, EventArgs e)
{
connection.GetConnection().Close();
Session["Username"] = null;
Session["Password"] = null;
Response.Clear();
Response.Redirect(Request.Url.ToString(), true);
}
}

数据库列表.cs

public class dbList
{
public static Dictionary<int, string> GetDbList()
{
//List<string> dataSources = new List<string>();
var dataSources = new Dictionary<int, string>();

RegistryKey reg = (Registry.LocalMachine).OpenSubKey("Software");
reg = reg.OpenSubKey("ODBC");
reg = reg.OpenSubKey("ODBC.INI");
reg = reg.OpenSubKey("ODBC Data Sources");

if (reg != null)
{
string[] items = reg.GetValueNames();
Array.Sort(items);
int i = 0;
foreach (string item in items)
{
dataSources.Add(i, item);
i++;
}
}

reg.Close();

return dataSources;
}
}

数据库连接.cs

public class dbConnection
{
private string[] dataSources = { "name1", "name2", "name3" };

public dbConnection() {}

public dbConnection(string dataSource, string userID, string password)
{
DataSource = dataSource;
UserID = userID;
Password = password;
}

public string DataSource { get; set; }
private string UserID { get; set; }
private string Password { get; set; }

public OracleConnection GetConnection()
{
OracleConnection connection = null;

string connectionString =
@"Data Source="+ DataSource +
";User ID=" + UserID +
";Password=" + Password +
";Unicode=True";

try
{
connection = new OracleConnection(connectionString);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error");
}

return connection;
}

public bool IsValid()
{
int trueCount = 0;

foreach (string dataSource in dataSources)
if (DataSource == dataSource) trueCount++;

if (trueCount > 0) return true;
else return false;
}

}

最佳答案

更新:2011 年 6 月 13 日

你能尝试创建一个 Dictionary 吗?而不是 List<string>在你的手中 GetConnection()方法?

var dictionary = new Dictionary<int, string>();
dictionary.Add(1, "Connection1");
dictionary.Add(2, "Connection2");
dictionary.Add(3, "Connection3");
dictionary.Add(4, "Connection4");

// Binding the dictionary to the DropDownList:
dropDown.DataTextField = "Value";
dropDown.DataValueField = "Key";
dropDown.DataSource = dictionary; //Dictionary<int, string>
dropDown.DataBind();

您需要先绑定(bind)“ddlDatabase”,然后设置 SelectedValue。否则,只会在您的下拉列表中创建 1 个项目(具有 SelectedValue),并且您没有任何项目可供选择,除了一个项目。

//Code to databind ddlDatabase
BindDatabaseDropdown();
if (Session["Datasource"] != null)
ddlDatabase.SelectedValue = Session["Datasource"].ToString();

关于C# DropDownList 和 session [""],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6300964/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com