110,561
社区成员
发帖
与我相关
我的任务
分享
<asp:DropDownList ID="DropDownList1" runat="server" style="z-index: 1; left: 360px; top: 80px; position: absolute; height: 35px; width: 220px; bottom: 343px">
</asp:DropDownList>
public static DataTable Table(string sql)
{
using (MySqlConnection conn = new MySqlConnection(ConnStr))
{
MySqlDataAdapter sda = new MySqlDataAdapter(sql, conn);
DataTable dt = new DataTable();
sda.Fill(dt);
return dt;
}
}
protected void Page_Load(object sender, EventArgs e)
{
string sql = string.Format(" select * from table1");
DropDownList1.Items.Clear();
DropDownList1.DataSource = Table(sql);;
DropDownList1.DataTextField = "name";
DropDownList1.DataValueField = "id";
DropDownList1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
string ss1 = DropDownList1.SelectedValue.ToString();
}
public static DataTable Table(string sql)
{
using (MySqlConnection conn = new MySqlConnection(ConnStr))
{
MySqlDataAdapter sda = new MySqlDataAdapter(sql, conn);
DataTable dt = new DataTable();
sda.Fill(dt);
return dt;
}
}
static string ConnStr = "Server=localhost;UserId=root;Password=12345678;Database=reservoirinfo;pooling=false;CharSet=utf8;port=3306";
protected void Page_Load(object sender, EventArgs e)
{
string sql = string.Format(" select * from table1");
if (!IsPostBack)
{
DropDownList1.Items.Clear();
DropDownList1.DataSource = Table(sql); ;
DropDownList1.DataTextField = "id";//这才是要显示的字段
DropDownList1.DataValueField = "id";//必须绑定int类型,不写发布会出错
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, new ListItem("--请你选择--")); //插入空项,此项必须放到数据绑定之后
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string ss1 = DropDownList1.SelectedValue.ToString();
}
总之,Page_Load(object sender, EventArgs e) 新增加了一行代码
if (!IsPostBack)
{