我在一个页面中使用了三个DropDownLists。我使用if(!Page.IsPostBack)条件在pageload上将数据绑定到DDL1(DropDownList1)。在DDL1 selectedIndexChanged事件我将数据绑定到DDL2并且其工作正常。但是当我尝试对DDL3& DDL2(类似于DDL2的SelectedIndexChanged事件上的DDL2的绑定数据)总是DDL2仅在我选择随机时选择第一项,同时DDL2仍然是第一项。这里所有3个DDL都是AutoPostback-true,Vistate-Enabled
以下是我的代码:
ASPX - 代码..
<form id="form1" runat="server">
<div>
<br />
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Value="0">Select</asp:ListItem>
</asp:DropDownList>
<br />
<br />
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged">
<asp:ListItem Value="0">Select</asp:ListItem>
</asp:DropDownList>
<br />
<br />
<asp:DropDownList ID="DropDownList3" runat="server" AutoPostBack="True">
<asp:ListItem Value="0">Select</asp:ListItem>
</asp:DropDownList>
<br />
</div>
</form>
.CS代码...
public partial class getcolumns : System.Web.UI.Page
{
private SqlConnection con;
private SqlDataAdapter da;
private DataTable dt;
private DataSet ds;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
try
{
string query = " SELECT name, dbid FROM sys.sysdatabases where dbid > 4 order by name ";
con = new SqlConnection(ConfigurationManager.AppSettings["godb"].ToString());
da = new SqlDataAdapter(query, con);
dt = new DataTable();
ds = new DataSet();
da.Fill(dt);
DropDownList1.DataSource = dt.DefaultView.ToTable(true, "name", "dbid");
//DropDownList1.DataValueField = "dbid";
DropDownList1.DataTextField = "name";
DropDownList1.DataBind();
}
catch (Exception) { }
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
DropDownList2.Items.Clear();
string query = " SELECT TABLE_CATALOG, TABLE_NAME FROM " + DropDownList1.SelectedItem + ".INFORMATION_SCHEMA.tables ";
con = new SqlConnection(ConfigurationManager.AppSettings["godb"].ToString());
da = new SqlDataAdapter(query, con);
dt = new DataTable();
ds = new DataSet();
da.Fill(ds);
DropDownList2.DataSource = ds; // dt.DefaultView.ToTable(true, "TABLE_NAME", "TABLE_CATALOG");
//DropDownList2.DataValueField = "TABLE_CATALOG";
DropDownList2.DataTextField = "TABLE_NAME";
DropDownList2.DataBind();
DropDownList2.Items.Insert(0, new ListItem("--Select--", "0"));
}
catch { }
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
DropDownList3.Items.Clear();
string query = " SELECT TABLE_NAME, COLUMN_NAME FROM " + DropDownList1.SelectedItem + ".INFORMATION_SCHEMA.columns where TABLE_NAME = '" + DropDownList2.SelectedItem + "' ";
con = new SqlConnection(ConfigurationManager.AppSettings["godb"].ToString());
da = new SqlDataAdapter(query, con);
dt = new DataTable();
da.Fill(dt);
DropDownList3.DataSource = dt.DefaultView.ToTable(true, "TABLE_NAME", "COLUMN_NAME");
//DropDownList3.DataValueField = "TABLE_NAME";
DropDownList3.DataTextField = "COLUMN_NAME";
DropDownList3.DataBind();
}
catch (Exception) { }
}
}
修改: - 当我忽略&#39; DataValueField &#39; DDL 感谢大家的支持......
答案 0 :(得分:0)
这可能是多个autopostback事件,因此当它首先触发时ddl1会在第二个事件中覆盖所选项目。当第二个事件发生时,它将重置为默认状态。
您可以尝试在两者上使用相同的事件方法,具体取决于调用者填充内容。
使用这两个(对象发送者,EventArgs e):D
答案 1 :(得分:0)
尝试使用此代码,它对我来说很好。
<强> ASPX 强>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" >
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged" >
</asp:DropDownList>
<asp:DropDownList ID="DropDownList3" runat="server" AutoPostBack="True" >
</asp:DropDownList>
</div>
</form>
<强>的.cs 强>
public List<string> list1 { get; set; }
public List<string> list2 { get; set; }
public List<string> list3 { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
try
{
list1 = new List<string> { "a", "b", "c" };
DropDownList1.DataSource = list1;
DropDownList1.DataBind();
}
catch (Exception) { }
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
list2 = new List<string> { "1a", "1b", "1c" };
DropDownList2.DataSource = list2;
DropDownList2.DataBind();
}
catch { }
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
list3 = new List<string> { "2a", "2b", "2c" };
DropDownList3.DataSource = list3;
DropDownList3.DataBind();
}
catch (Exception) { }
}
我刚删除了下拉列表中的默认options
并删除了viewstatemode
并使用了数据绑定而不是列表。