使用SQL连接填充下拉列表

时间:2016-08-11 12:33:20

标签: c# asp.net scriptmanager

我有一个asp.net形式的DropdownList需要通过SQL填充。我在我的ScriptManager中使用了Page_Load(),并且由于此dropdownList没有填充。我需要使用ScriptManager,因为我使用AjaxCalendarExtender

protected void Page_Load(object sender, EventArgs e)
{  
    if (!IsPostBack)
    {
        DataSet ds = dba.getNames();
        ddNames.DataSource = ds.Tables["EMPLOYEE"].DefaultView;
        ddNames.DataTextField = "Username";
        ddNames.DataValueField = "Username";
        ddNames.DataBind(); 
    }  
    if (ScriptManager.GetCurrent(Page) == null)
    {
        Page.Form.Controls.AddAt(0, new ScriptManager());
    }
}

DB_Access.cs中的getNames()函数

public DataSet getNames()
{
    if (conn.State.ToString() == "Closed")
    {
        conn.Open();
    }

    SqlCommand newCmd = conn.CreateCommand();
    newCmd.Connection = conn;
    newCmd.CommandType = CommandType.Text;
    newCmd.CommandText = "Select DISTINCT Username from dbo.EMPLOYEE";

    SqlDataAdapter da = new SqlDataAdapter(newCmd);
    DataSet ds = new DataSet();
    da.Fill(ds, "EMPLOYEE");

    conn.Close();

    return ds;
}

1 个答案:

答案 0 :(得分:0)

我发现答案是在Write-Host页面的脚本管理器中,而不是在页面加载事件中动态创建它。

  

aspx