更新时间:2023-02-11 19:34:33
这就像将标准文本框值保存到数据库中一样。
有关保存到数据库的标准教程,请通过 -
将文本框中的数据保存到asp.net中的数据库 [ ^ ]
如何在asp.net中将数据插入数据库 [ ^ ]
有关下拉列表的具体示例 -
选中后,从下拉列表中保存数据库中的多个选定列表项 [ ^ ]
This is to be done just like you would save a standard textbox value into the database.
For standard tutorials on saving to database, go through -
Save data from textboxes to database in asp.net [^]
How to Insert data into database in asp net [^]
For specific example on dropdowns -
Save multiple selected list items in database from dropdown list when selected[^]
protected void Page_Init(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillCountry();
}
}
public void FillCountry()
{
cn.Open();
cmd.Connection = cn;
cmd.CommandText = "Select ID, Countries from Countries";
SqlDataReader dr = cmd.ExecuteReader();
Country.Items.Clear();
Country.DataSource = dr;
Country.DataTextField = "Countries";
Country.DataValueField = "ID";
Country.DataBind();
Country.Items.Insert(0, "--Select--");
cn.Close();
cmd.Dispose();
}
public void FillStates()
{
if (Page.IsPostBack)
{
cn.Open();
cmd.Connection = cn;
cmd.CommandText = "Select StateID, States from States where CountryId = '" + Country.SelectedValue + "'";
SqlDataReader dr = cmd.ExecuteReader();
State.Items.Clear();
State.DataSource = dr;
State.DataTextField = "States";
State.DataValueField = "StateID";
State.DataBind();
State.Items.Insert(0, "--Select--");
cn.Close();
cmd.Dispose();
}
}
protected void Country_SelectedIndexChanged(object sender, EventArgs e)
{
FillStates();
}
}