DropDownList控件学习

时间:2021-01-14 16:47:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data; namespace WebApplication1
{
public partial class DropDownList控件 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindMonthList();
BindUserList();
} }
private void BindMonthList()
{
int[] monthList=new int[];
for (int i = ; i <= ; i++)
{
monthList[i] = i + ;
}
ddlMonthList.DataSource = monthList;
ddlMonthList.DataBind(); }
private void BindUserList()
{
SqlConnection conn = new SqlConnection(@"server=Rose-PC\SQLEXPRESS;Database=User;User Id=sa;password=");
SqlCommand command = new SqlCommand("Select ID,RealName from UserInfo", conn);
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataTable data = new DataTable();
adapter.Fill(data); ddlUserList.DataTextField = "RealName";
ddlUserList.DataValueField = "ID";
ddlUserList.DataSource = data;
ddlUserList.DataBind(); } }
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DropDownList控件.aspx.cs" Inherits="WebApplication1.DropDownList控件" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="ddlMonthList" runat="server">
</asp:DropDownList>
<asp:DropDownList ID="ddlUserList" runat="server">
</asp:DropDownList>
</div>
</form>
</body>
</html>