它的数据源
本文关键字:数据源 | 更新日期: 2023-09-27 17:50:28
大家好,我有关于下拉列表的问题。我使用下拉列表与数据源。我怎样才能得到我选择的值?
// I need a if statement here because my programme doesn't know which value of dropdown list selected and I don't know how to use this with datasource.
if(//if I select quiz 1 from dropdown list ,quiz 1 should list questions.)
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["RegConnectionString"].ConnectionString);
string chooce = "Select Quiz from tblQuiz where Quiz=1 ";
SqlCommand userExist = new SqlCommand(chooce, con);
con.Open();
int temp = Convert.ToInt32(userExist.ExecuteScalar().ToString());
if (temp == 1)
{
if (rbList.Items[0].Selected == true)
{
string cmdStr = "Select Question from tblQuiz where ID=1";
SqlCommand quest = new SqlCommand(cmdStr, con);
lblque.Text = quest.ExecuteScalar().ToString();
con.Close();
}
您可以使用List, Dictionary, Enum, DataSet DataTable
以不同的方式绑定下拉列表。
在绑定下拉菜单的数据源时,你必须考虑三件事:
- DataSource -数据集或数据表或数据源的名称
- DataValueField -这些字段将被隐藏
- DataTextField -这些字段将显示在dropwon上。
您可以使用以下代码将下拉列表绑定到数据源作为datatable
:
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString);
SqlCommand cmd = new SqlCommand("Select * from tblQuiz", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt=new DataTable();
da.Fill(dt);
DropDownList1.DataTextField = "QUIZ_Name";
DropDownList1.DataValueField = "QUIZ_ID"
DropDownList1.DataSource = dt;
DropDownList1.DataBind();
如果你想处理下拉列表的选择,那么你必须改变AutoPostBack="true"
,你可以使用SelectedIndexChanged
事件来写你的代码。
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string strQUIZ_ID=DropDownList1.SelectedValue;
string strQUIZ_Name=DropDownList1.SelectedItem.Text;
// Your code..............
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
drpCategory.DataSource = CategoryHelper.Categories;
drpCategory.DataTextField = "Name";
drpCategory.DataValueField = "Id";
drpCategory.DataBind();
}
}
参考此链接中的示例。这可能对你有帮助。
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.dropdownlist.aspxvoid Page_Load(Object sender, EventArgs e)
{
// Load data for the DropDownList control only once, when the
// page is first loaded.
if(!IsPostBack)
{
// Specify the data source and field names for the Text
// and Value properties of the items (ListItem objects)
// in the DropDownList control.
ColorList.DataSource = CreateDataSource();
ColorList.DataTextField = "ColorTextField";
ColorList.DataValueField = "ColorValueField";
// Bind the data to the control.
ColorList.DataBind();
// Set the default selected item, if desired.
ColorList.SelectedIndex = 0;
}
}
void Selection_Change(Object sender, EventArgs e)
{
// Set the background color for days in the Calendar control
// based on the value selected by the user from the
// DropDownList control.
Calendar1.DayStyle.BackColor =
System.Drawing.Color.FromName(ColorList.SelectedItem.Value);
}
这取决于您如何设置下拉菜单的默认值。使用选定值,但必须设置选定值。例如,我用表/列表的name和id字段填充数据源。我将所选值设置为id字段,并将显示设置为名称。当我选择时,我得到id字段。我使用它来搜索关系表并查找实体/记录。