2017-04-25 38 views
0

當頁面加載時,Dropbox的第一項不工作,但如果選擇Dropbox中的第二項,表單將填充相關數據。如果我回到之前選擇的第一個項目,這次將工作。請任何幫助。由於首次使用C#加載頁面時,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 
          [email protected]", 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(); 
     } 

    } 
+0

當頁面加載'selectedindexchanged'事件將不會被稱爲 – WhatsThePoint

+0

你應該綁定數據源後,設置''SelectedValue' = dropdownlist.SelectedValue youValue;' – TriV

回答

1

使用下面的代碼:

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 
         [email protected]", 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); 

希望這能解決你的問題。

+0

感謝您的幫助 –

+0

@EricMbiada,是否能解決你的問題,請接受答案。 –

相關問題