0

我正在 ASP.NET WebForms 中制作一个项目。虽然我已经做了很多次,但是这次下拉列表让我很困扰。

我从数据库中获取项目,然后使用 FOR 循环将它们一一添加到我的下拉列表中。这很好用。但问题是我无法从列表中舒适地选择一个项目,每当我尝试从下拉列表中选择一个项目时,它会将选择捕捉到第一个元素,因此很难选择所需的项目。

我怎么能解决这个问题?

假设我将光标移到列表中的第 9 项上,然后它也交替选择第 1 项和第 9 项,以至于我看到它们都被选中。

代码隐藏

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        DropDownList1.Items.Clear();

        con.ConnectionString = ConfigurationManager.ConnectionStrings["familyConnectionString"].ConnectionString;
        con.Open();

        adp = new SqlDataAdapter("select distinct family_head from family", con);
        DataSet ds = new DataSet();
        adp.Fill(ds, "family");
        con.Close();

        for (int i = 0; i < ds.Tables["family"].Rows.Count; i++)
            DropDownList1.Items.Add(ds.Tables["family"].Rows[i][0].ToString());
    }
}

ASPX

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:DropDownList ID="DropDownList1" runat="server" Width="150px">
    </asp:DropDownList>
    &nbsp;&nbsp;&nbsp;&nbsp;
    <asp:DropDownList ID="DropDownList2" runat="server" Width="150px">
    </asp:DropDownList>
    &nbsp;&nbsp;&nbsp;&nbsp;

    <asp:Button ID="Button1" runat="server" Height="30px" onclick="Button1_Click" 
        Text="Submit" Width="145px" BackColor="#465767" ForeColor="White" />

    <asp:RoundedCornersExtender ID="Button1_RoundedCornersExtender" runat="server" 
        Enabled="True" TargetControlID="Button1" Corners="All" Radius="10">
    </asp:RoundedCornersExtender>
    <br />
    <br />
    <br />
</asp:Content>

CSS 关键帧动画在页面背景中工作,这可能是原因吗?

4

1 回答 1

0

对于 WebControl 的数据绑定如何工作,似乎存在某种混淆。最好的参考是:http: //msdn.microsoft.com/en-us/library/ms178472.aspx

假设显示了 DropDownList1 的所有绑定代码:

DropDownList1.Items.Clear();

不应该是必需的,因为这些项目不应该在每个 PostBack 中持续存在。(至少我假设,因为如果 DataSource 没有再次绑定,则在每次 PostBack 之后所有项目都将丢失)。

如果要使用 DropDownList,则 DropDownList 需要在每个 PostBack 上都有项目。您正在使用

if (!Page.IsPostBack)

这意味着,如果您有 PostBack,则不会再次绑定项目。在上面发布的链接中,您可以看到控件在 LoadEvent 期间设置其属性(例如在单击按钮之前选择了哪个项目),这意味着需要在生命周期的早期绑定项目,例如在 OnInit 期间。

尝试这样的事情:

protected void BindDropDownList1()
{
 con.ConnectionString = ConfigurationManager.ConnectionStrings["familyConnectionString"].ConnectionString;
 con.Open();
 adp = new SqlDataAdapter("select distinct family_head from family", con);
 DataSet ds = new DataSet();
 adp.Fill(ds, "family");
 con.Close();
 for (int i = 0; i < ds.Tables["family"].Rows.Count; i++)
   DropDownList1.Items.Add(ds.Tables["family"].Rows[i][0].ToString());
}

    protected override void OnInit(EventArgs e)
    {
       this.BindDropDownList1();
       base.OnInit(e);
    }

另一个建议是 DropDownList 的 DataSource 属性,而不是DropDownList1.Items.Add. 如果使用这种方式,则需要设置 DropDownList 的 DataKeyValue 和 DataKeyMember 属性:

protected void BindDropDownList1()
{
 con.ConnectionString = ConfigurationManager.ConnectionStrings["familyConnectionString"].ConnectionString;
 con.Open();
 adp = new SqlDataAdapter("select distinct family_head from family", con);
 DataSet ds = new DataSet();
 adp.Fill(ds, "family");
 con.Close();
 DropDownList1.DataSource = ds;
 DropDownList1.DataBind();
}

编辑
我想我发现了你的错误。您正在使用 AjaxControlToolKit 中的控件,但仍在使用ScriptManager. 大约 2 年来,来自 AjaxControlToolKit 的控件需要ToolkitScriptManager. 将 替换ScriptManagerToolkitScriptManager

于 2013-01-05T09:45:33.410 回答