什么是从数据库中填充DropDownList的正确方法?
我正在从SQL Server数据库填充DropDownList,如下所示。 它工作正常,但我不知道这是一个好方法。 有人可以阐明这种方法,并提出一些改进?
private void LoadSubjects() { ddlSubjects.Items.Clear(); string selectSQL = "SELECT SubjectID,SubjectName FROM Students.dbo.Subjects"; SqlConnection con = new SqlConnection(connectionString); SqlCommand cmd = new SqlCommand(selectSQL, con); SqlDataReader reader; try { ListItem newItem = new ListItem(); newItem.Text = "<Select Subject>"; newItem.Value = "0"; ddlSubjects.Items.Add(newItem); con.Open(); reader = cmd.ExecuteReader(); while (reader.Read()) { newItem = new ListItem(); newItem.Text = reader["SubjectName"].ToString(); newItem.Value = reader["SubjectID"].ToString(); ddlSubjects.Items.Add(newItem); } reader.Close(); } catch (Exception err) { //TODO } finally { con.Close(); } }
您可以将DropDownList绑定到数据源(DataTable,List,DataSet,SqlDataSource等)。
例如,如果你想使用一个DataTable:
ddlSubject.DataSource = subjectsTable; ddlSubject.DataTextField = "SubjectNamne"; ddlSubject.DataValueField = "SubjectID"; ddlSubject.DataBind();
编辑 – 更完整的例子
private void LoadSubjects() { DataTable subjects = new DataTable(); using (SqlConnection con = new SqlConnection(connectionString)) { try { SqlDataAdapter adapter = new SqlDataAdapter("SELECT SubjectID, SubjectName FROM Students.dbo.Subjects", con); adapter.Fill(subjects); ddlSubject.DataSource = subjects; ddlSubject.DataTextField = "SubjectNamne"; ddlSubject.DataValueField = "SubjectID"; ddlSubject.DataBind(); } catch (Exception ex) { // Handle the error } } // Add the initial item - you can add this even if the options from the // db were not successfully loaded ddlSubject.Items.Insert(0, new ListItem("<Select Subject>", "0")); }
要通过标记设置初始值而不是代码隐藏,请指定选项并将AppendDataBoundItems属性设置为true:
<asp:DropDownList ID="ddlSubject" runat="server" AppendDataBoundItems="true"> <asp:ListItem Text="<Select Subject>" Value="0" /> </asp:DropDownList>
然后,您可以将DropDownList绑定到代码隐藏中的DataSource(请记住删除:
ddlSubject.Items.Insert(0, new ListItem("<Select Subject>", "0"));
从代码隐藏,或者你将有两个“”项目。
我希望我不会夸大这个显而易见的,但为什么不直接在ASP方面呢? 除非你根据程序中的某些条件dynamic修改SQL,否则应该尽可能地避免代码隐藏。
您可以直接在ASP中使用SqlDataSource控件和下拉列表中的一个属性来执行上述操作。
<asp:GridView ID="gvSubjects" runat="server" DataKeyNames="SubjectID" OnRowDataBound="GridView_RowDataBound" OnDataBound="GridView_DataBound"> <Columns> <asp:TemplateField HeaderText="Subjects"> <ItemTemplate> <asp:DropDownList ID="ddlSubjects" runat="server" DataSourceID="sdsSubjects" DataTextField="SubjectName" DataValueField="SubjectID"> </asp:DropDownList> <asp:SqlDataSource ID="sdsSubjects" runat="server" SelectCommand="SELECT SubjectID,SubjectName FROM Students.dbo.Subjects"></asp:SqlDataSource> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
public void getClientNameDropDowndata() { getConnection = Connection.SetConnection(); // to connect with data base Configure manager string ClientName = "Select ClientName from Client "; SqlCommand ClientNameCommand = new SqlCommand(ClientName, getConnection); ClientNameCommand.CommandType = CommandType.Text; SqlDataReader ClientNameData; ClientNameData = ClientNameCommand.ExecuteReader(); if (ClientNameData.HasRows) { DropDownList_ClientName.DataSource = ClientNameData; DropDownList_ClientName.DataValueField = "ClientName"; DropDownList_ClientName.DataTextField="ClientName"; DropDownList_ClientName.DataBind(); } else { MessageBox.Show("No is found"); CloseConnection = new Connection(); CloseConnection.closeConnection(); // close the connection } }
((TextBox)GridView1.Rows[e.NewEditIndex].Cells[3].Controls[0]).Enabled = false;