- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
每个人。
我在从下拉列表中选择值时遇到一点问题。当用户输入他/她的凭据并选择数据源名称时,它们都存储在 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;
}
}
最佳答案
你能尝试创建一个 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/
我有两个 ASP.Net 下拉列表,其中的值相同,根据用户输入的不同数据从数据库填充,并且每次都有动态大小。我的情况是,我希望禁用第二个下拉列表中已在第一个下拉列表中选择的任何选项。我做了一些谷歌,大
我有两个相关的模型。 public partial class bs_delivery_type { public decimal delivery_id { get; set; }
我需要能够根据先前 DropDownList 的选择来填充 DropDownList。 我注意到在 SO 上有很多类似的主题,不幸的是,我需要在不使用 AJAX 调用的情况下完成此操作,并且需要在 M
我无法在 ASP.NET MVC 应用程序的 Create View 中获得我们想要的功能。 我们的 Create View 中有两个 DropDownList。 一个是类别选择,第二个应该根据第一个
这个问题在这里已经有了答案: Easiest way to create a cascade dropdown in ASP.NET MVC 3 with C# (1 个回答) 关闭 9 年前。 加
有什么区别 DropDownList.ClearSelection(); 和 DropDownList.SelectedIndex = -1; 在使用下拉列表时? 编辑:我知道 MSDN 上提供的这些
这是一个常见问题,但我不知道如何使用 KendoUI 小部件和 Javascript 来解决它。我有一个 KendoGrid,其数据源来自对 Web 服务的 AJAX 调用。数据绑定(bind)到列。
我有一组问题供用户选择,其中一些问题有第二个选项列表可供选择。我的目标是有一个下拉列表,如果您选择其中一个在其 SecondaryChoiceList 中有项目的选项,那么第二个列表将出现在初始下拉列
我有一组问题供用户选择,其中一些问题有第二个选项列表可供选择。我的目标是有一个下拉列表,如果您选择其中一个在其 SecondaryChoiceList 中有项目的选项,那么第二个列表将出现在初始下拉列
我正在使用带有 ajax 工具包的 asp.net 3.5。 问题:我有一个自定义控件,在更新面板中有两个下拉列表。第一个 DDL 具有属性 AutoPostBack="true" 并且在选择时填充第
我在我的一个 ddl 上选择了其他选项,并且成功获得了该值,但同时我想在代码中的其他 ddl 上获得选项。我怎样才能获得其他 ddl 选项....?我在这里放置了我的问题的代码示例: (no
我们在我们的应用程序中使用自定义表单控件。当我们尝试在这个自定义表单控件中使用 asp dropdownlist 控件时,我得到了这个给定的异常。但是,当我使用 asp 文字控件时,我们不会收到此错误
我有两个 asp.net 下拉列表,我想用 Javascript 操作客户端。这两个下拉菜单位于模态打开内: function() 当 dropdown1.value == "me"时我想要禁用 dr
我有一个 WebForms 页面,上面有两个 DropDownList 控件,它们都包含 60-80 度的温度范围,一个用于加热,另一个用于冷却。它们在 .aspx 中声明为: 每个列表的值都使用
抱歉这个愚蠢的问题,我以前使用过 MVC,但以前从未做过这样的事情。 好吧,假设我正在实现一个门票系统,用户可以在特定时间和巴士上预订门票。 在“预订”页面中,我有2个DropDownList,其中一
我有一个 activeDropDownList 类型的字段,用户在其中选择所需的月份,并且 View “mobilizer”处理所选月份的大量查询。 但是,渲染页面后,activeDropDownLi
我试图禁用列表框中的多选,但是当我调用removeAttr('multiple')时,它会将我的列表框变成下拉列表。我不需要下拉菜单,我想保留列表框。我该如何调整才能实现这一目标?谢谢。 @Ht
我需要在 WinForm 上显示一些韩文文本。文本在我的 ListBox 控件中显示良好。相同的文本不会显示在我的 DropDownList 控件中。两个控件的字体均为 Arial 8pt。两个控件的
我有一个简单的问题。我有一个 dropDownList,我试图用存储过程中的 id 填充它。然而它似乎不起作用。这是我的下拉列表: Select new CaseFile:
绑定(bind)后下拉菜单显示以下值: 第一优先级低 第二优先级中 第三优先级高 每次回发后下拉列表显示以下值: 第一优先级低 第二优先级中 第三优先级高 第一优先级低 第二优先级中 第三优先级高 这
我是一名优秀的程序员,十分优秀!