Here's the code I have. It works. The only problem is that the first index of the drop down box gets wiped out ("Select Source Database") when I bind the data set to its data source.
这是我的密码。它的工作原理。唯一的问题是,当我将数据集绑定到它的数据源时,下拉框的第一个索引(“Select Source Database”)将被删除。
I want to bind the data set and INCLUDE the first index "Select Source Database".
我要绑定数据集并包含第一个索引“Select Source Database”。
How can I modify my code to accomplish this?
如何修改代码来实现这一点?
protected void ddlServer_SelectedIndexChanged(object sender, EventArgs e)
{
ddlSourceDatabases.Items.Clear();
ddlSourceDatabases.Items.Add("Select Source Database");
lbxSourceTables.Items.Clear();
if (ddlSourceServers.SelectedIndex != 0)
{
try
{
ddlSourceDatabases.DataSource = Database.GetDatabases(ddlSourceServers.Text);
ddlSourceDatabases.DataTextField = "name";
ddlSourceDatabases.DataValueField = "name";
ddlSourceDatabases.DataBind();
}
catch (Exception)
{
}
}
}
4 个解决方案
#1
1
Insert your item after data binding to the first position:
将数据绑定后插入到第一个位置:
//...
ddlSourceDatabases.DataBind();
ddlSourceDatabases.Items.Insert(0, "Select Source Database");
//...
#2
0
I have refatored your code, now it should work
我重新修改了您的代码,现在应该可以工作了
protected void ddlServer_SelectedIndexChanged(object sender, EventArgs e)
{
ddlSourceDatabases.Items.Clear();
lbxSourceTables.Items.Clear();
if (ddlSourceServers.SelectedIndex != 0)
{
try
{
ddlSourceDatabases.DataSource = Database.GetDatabases(ddlSourceServers.Text);
ddlSourceDatabases.DataTextField = "name";
ddlSourceDatabases.DataValueField = "name";
ddlSourceDatabases.DataBind();
ddlSourceDatabases.Items.Add("Select Source Database");
}
catch (Exception)
{
}
}
}
#3
0
Add the "Select Source Database" as a new item to your datasource as the DataTextField and null as the DataValueField. That'll fix it. When you databind to a datasource, any other values will be deleted and only the ones from your datasource will be displayed.
将“选择源数据库”作为新项添加到数据源中作为DataTextField,将null添加到DataValueField。可以解决它。当对数据源进行数据库时,将删除任何其他值,只显示数据源中的值。
#4
0
Independent of what you added previously (in your case is the "Select Source Database" string), when you set the datasource manually you're replacing ALL the items included the previously added. So add the item before setting its datacontext (to the zero index or first position) using dlSourceDatabases.Items.Insert(0, "Select Source Database")
独立于您先前添加的内容(在您的示例中是“选择源数据库”字符串),当您手动设置数据源时,您将替换包含先前添加的所有项。因此,在使用dlsourcedatabase . items设置它的datacontext(到零索引或第一个位置)之前添加这个条目。插入(0,“选择源数据库”)
#1
1
Insert your item after data binding to the first position:
将数据绑定后插入到第一个位置:
//...
ddlSourceDatabases.DataBind();
ddlSourceDatabases.Items.Insert(0, "Select Source Database");
//...
#2
0
I have refatored your code, now it should work
我重新修改了您的代码,现在应该可以工作了
protected void ddlServer_SelectedIndexChanged(object sender, EventArgs e)
{
ddlSourceDatabases.Items.Clear();
lbxSourceTables.Items.Clear();
if (ddlSourceServers.SelectedIndex != 0)
{
try
{
ddlSourceDatabases.DataSource = Database.GetDatabases(ddlSourceServers.Text);
ddlSourceDatabases.DataTextField = "name";
ddlSourceDatabases.DataValueField = "name";
ddlSourceDatabases.DataBind();
ddlSourceDatabases.Items.Add("Select Source Database");
}
catch (Exception)
{
}
}
}
#3
0
Add the "Select Source Database" as a new item to your datasource as the DataTextField and null as the DataValueField. That'll fix it. When you databind to a datasource, any other values will be deleted and only the ones from your datasource will be displayed.
将“选择源数据库”作为新项添加到数据源中作为DataTextField,将null添加到DataValueField。可以解决它。当对数据源进行数据库时,将删除任何其他值,只显示数据源中的值。
#4
0
Independent of what you added previously (in your case is the "Select Source Database" string), when you set the datasource manually you're replacing ALL the items included the previously added. So add the item before setting its datacontext (to the zero index or first position) using dlSourceDatabases.Items.Insert(0, "Select Source Database")
独立于您先前添加的内容(在您的示例中是“选择源数据库”字符串),当您手动设置数据源时,您将替换包含先前添加的所有项。因此,在使用dlsourcedatabase . items设置它的datacontext(到零索引或第一个位置)之前添加这个条目。插入(0,“选择源数据库”)