我已经为官员配置文件创建了表格。我希望我的DropDownList
选择的值填充我的 2 Textboxes
。但是,只有DropDownList
具有数据库项,但是当我选择一个项目时,2 Textboxes
不会填充任何内容。我正在使用 ASP.NET C#和SQL Management Studio。如果有人能提供帮助,我会很高兴,如果需要在数据库中包含查询,请教我。感谢!!
这是我的页面加载代码
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ddlName1.Items.Add(new ListItem("-Select Officer-", ""));
ddlName1.AppendDataBoundItems = true;
SqlConnection connName1 = new SqlConnection("Data Source=(...);" + "Initial Catalog=(...); Integrated Security=SSPI");
connName1.Open();
SqlDataAdapter adapName1 = new SqlDataAdapter("SELECT * FROM OfficersProfile", connName1);
DataSet dsName1 = new DataSet();
adapName1.Fill(dsName1);
ddlName1.DataSource = dsName1;
ddlName1.DataTextField = "officer_name";
ddlName1.DataValueField = "officer_id";
ddlName1.DataBind();
connName1.Close();
}
}
这是我选择索引更改的代码
protected void ddlName1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection connName1 = new SqlConnection("Data Source=(...);" + "Initial Catalog=(...); Integrated Security=SSPI");
connName1.Open();
SqlCommand cmdName1 = new SqlCommand("SELECT * FROM OfficersProfile where officer_id=@officerID", connName1);
cmdName1.Parameters.AddWithValue("@officerID", ddlName1.SelectedValue);
SqlDataReader drName1;
drName1 = cmdName1.ExecuteReader();
while(drName1.Read())
{
txtDivision.Text = drName1[3].ToString();
txtLSOExec.Text = drName1[4].ToString();
}
drName1.Close();
connName1.Close();
}
在 DropDownList 中添加AutoPostBack="true"