【发布时间】:2017-09-22 06:22:27
【问题描述】:
当页面加载时,Dropbox 的第一项不工作,但如果选择 Dropbox 中的第二项,表单将填充相关数据。如果我回到之前选择的第一个项目,这次它会起作用。请提供任何帮助。谢谢
HTML 代码
<asp:DropDownList ID="DropDownListUpdateSample" runat="server" Height="37px" Width="132px" CssClass="auto-style111" AutoPostBack = "true" OnSelectedIndexChanged="DropDownListUpdateSample_SelectedIndexChanged" AppendDataBoundItems="False">
C#代码
//Code to populate the Dropbox
using (SqlCommand cmd5 = new SqlCommand(@"SELECT Patient.MBID, Sample.SampleID
FROM Patient INNER JOIN
Sample ON Patient.MBID = Sample.MBID
WHERE
Patient.Surname = @Surname and Patient.DOB = convert(datetime, @DOB, 103)
ORDER by Sample.SampleID ASC ", con))
{
cmd5.Parameters.AddWithValue("@Surname", txtSearchSurname.Text);
cmd5.Parameters.AddWithValue("@DOB", txtSearchDOB.Text);
SqlDataAdapter da5 = new SqlDataAdapter(cmd5);
DataSet dt5 = new DataSet();
da5.Fill(dt5, "Sample");
DataTable myDataTable = dt5.Tables[0];
// Loop to insert the Sample ID in the Drop box
foreach (DataRow tempRow_Variable in myDataTable.Rows)
{
var tempRow = tempRow_Variable;
DropDownListUpdateSample.Items.Add(tempRow["SampleID"].ToString());
}
}
//Code to Populate the form after an item is selected from the Dropbox
protected void DropDownListUpdateSample_SelectedIndexChanged(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["Molecular"].ConnectionString))
{
con.Open();
using (SqlCommand st = new SqlCommand(@"SELECT *
FROM Sample
WHERE
SampleID=@SampleID", con))
{
st.Parameters.AddWithValue("@SampleID", DropDownListUpdateSample.SelectedItem.Value);
using (SqlDataReader reader = st.ExecuteReader())
{
while (reader.Read())
{
txtUpdateSampleID.Text = reader["SampleID"].ToString();
txtUpdateSampleType.Text = reader["SampleType"].ToString();
txtUpdateSampleDate.Text = reader["SampleDate"].ToString();
txtUpdateSampleTrial.Text = reader["SampleTrial"].ToString();
DropDownListUpdateFirstSample.SelectedItem.Value = reader["FirstSample"].ToString();
txtUpdateSampleComments.Text = reader["Comments"].ToString();
txtUpdateSampleConsultant.Text = reader["ConsultantName"].ToString();
DropDownListUpdate.SelectedItem.Value = reader["Diagnosis"].ToString();
DropDownListUpdateConsentConfirm.SelectedItem.Value = reader["ConsentConfirmed"].ToString();
txtUpdateConsentDate.Text = reader["DateConsent"].ToString();
txtUpdateOrther.Text = reader["OtherConsent"].ToString();
DropDownListUpdateSectionDecline.SelectedItem.Value = reader["SectionDecline"].ToString();
DropDownListUpdateQuarantine.SelectedItem.Value = reader["Quarantine"].ToString();
DropDownListUpdateClinicalArchive.SelectedItem.Value = reader["ClinicalArchive"].ToString();
DropDownListUpdateResearch.SelectedItem.Value = reader["Research"].ToString();
//DropDownListUpdateClinicalArchive.SelectedItem.Value= reader["Research"].ToString();
}
}
}
con.Close();
}
}
【问题讨论】:
-
页面加载时不会调用
selectedindexchanged事件 -
绑定数据源
dropdownlist.SelectedValue = youValue;后要设置SelectedValue
标签: c# html sql-server