C# 当使用C首次加载页面时,Dropbox的第一项不工作#
加载页面时,Dropbox的第一项不起作用,但如果在Dropbox中选择第二项,表单将填充相关数据。如果我回到之前选择的第一个项目,它将在这次工作。请帮忙。谢谢 HTML代码C# 当使用C首次加载页面时,Dropbox的第一项不工作#,c#,html,sql-server,C#,Html,Sql Server,加载页面时,Dropbox的第一项不起作用,但如果在Dropbox中选择第二项,表单将填充相关数据。如果我回到之前选择的第一个项目,它将在这次工作。请帮忙。谢谢 HTML代码 <asp:DropDownList ID="DropDownListUpdateSample" runat="server" Height="37px" Width="132px" CssClass="auto-style111" AutoPostBack = "true" OnSelectedIndexChange
<asp:DropDownList ID="DropDownListUpdateSample" runat="server" Height="37px" Width="132px" CssClass="auto-style111" AutoPostBack = "true" OnSelectedIndexChanged="DropDownListUpdateSample_SelectedIndexChanged" AppendDataBoundItems="False">
使用以下代码:
public void functionForSelectedValue(int id)
{
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", id);
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();
}
}
protected void DropDownListUpdateSample_SelectedIndexChanged(object sender, EventArgs e)
{
functionForSelectedValue(DropDownListUpdateSample.SelectedItem.Value);
}
和在页面加载中:
召唤
希望这能解决您的问题。加载页面时不会调用
selectedindexchanged
事件绑定数据源dropdownlist后应设置SelectedValue
。SelectedValue=youValue代码>感谢您的支持help@EricMbiada,如果它解决了您的问题,请接受答案。
public void functionForSelectedValue(int id)
{
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", id);
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();
}
}
protected void DropDownListUpdateSample_SelectedIndexChanged(object sender, EventArgs e)
{
functionForSelectedValue(DropDownListUpdateSample.SelectedItem.Value);
}
foreach (DataRow tempRow_Variable in myDataTable.Rows)
{
var tempRow = tempRow_Variable;
DropDownListUpdateSample.Items.Add(tempRow["SampleID"].ToString());
}
DropDownListUpdateSample.Items.FindByValue("IdforWhichYouWantTobindIt").Selected = true;
functionForSelectedValue(DropDownListUpdateSample.SelectedItem.Value);