0
我的dropdownlist不工作,爲什麼我想要它,我不明白爲什麼。我已經通過stackoverflow看,我找不到與我的相應的問題。我的下拉列表顯示數據庫中的所有名稱,但當選擇名稱時,附加到數據庫中名稱的數據不會顯示在文本框中。名字的數據在文本框中正確加載,但之後數據不會改變。當涉及到datasouceselect代碼時,我必須做錯誤的事情。這是我的代碼。
Dropdownlist選項不會更改數據
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class Contact : System.Web.UI.Page
{
private EmployeeContact selectedContact;
protected void Page_Load(object sender, EventArgs e)
{
// if it is not a PostBack,
// then bind the Employee data to the ddlContact dropdown list
if (!IsPostBack) DropDownList1.DataBind();
// Store the Contact data in the selectedContact object
selectedContact = this.GetselectedContact();
// Display the ContactID from the object in the txtContactID.Text
txtContactID.Text = selectedContact.ContactID;
// Display the Name from the object in the txtFirst.Text
txtFirst.Text = selectedContact.Contact_FirstName;
// Display the Street from the object in the txtLast.Text
txtLast.Text = selectedContact.Contact_LastName;
// Display the City from the object in the txtCell.Text
txtCell.Text = selectedContact.Contact_Cell;
// Display the State from the object in the txtOfficePhone.Text
txtOfficePhone.Text = selectedContact.Contact_OfficePhone;
// Display the Zip from the object in the txtEmail.Text
txtEmail.Text = selectedContact.Contact_Email;
// Display the Phone from the object in the txtPhone.Text
txtCompany.Text = selectedContact.Contact_Company;
// Display the Email from the object in the txtPosition.Text
txtPosition.Text = selectedContact.Contact_Position;
// Display the Phone from the object in the txtZip.Text
txtZip.Text = selectedContact.Contact_Zip;
// Display the Phone from the object in the txtStreet.Text
txtStreet.Text = selectedContact.Contact_Street;
// Display the Phone from the object in the txtCustomerSince.Text
txtCustomerSince.Text = selectedContact.Contact_CustomerSinceDate;
// Display the Phone from the object in the txtDateCreated.Text
txtDateCreated.Text = selectedContact.Contact_DateCreated;
// Display the Phone from the object in the txtNotes.Text
txtNotes.Text = selectedContact.Contact_Note;
}
private EmployeeContact GetselectedContact()
{
// Use DataView to get a datatable
DataView Contacts = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
// Format a RowFilter
Contacts.RowFilter = string.Format("ContactID = '{0}'", DropDownList1.SelectedValue);
// Get a specific row from the selected customer table row
DataRowView row = (DataRowView)Contacts[0];
// Instantiate a EmployeeContact object
EmployeeContact c = new EmployeeContact();
// Store the ContactID in the object
c.ContactID = row["ContactID"].ToString();
// Store the FirstName in the object
c.Contact_FirstName = row["Contact_FirstName"].ToString();
// Store the LastName in the object
c.Contact_LastName = row["Contact_LastName"].ToString();
// Store the Cellphone in the object
c.Contact_Cell = row["Contact_Cell"].ToString();
// Store the OfficePhone in the object
c.Contact_OfficePhone = row["Contact_OfficePhone"].ToString();
// Store the Email in the object
c.Contact_Email = row["Contact_Email"].ToString();
// Store the Company in the object,
c.Contact_Company = row["Contact_Company"].ToString();
// Store the Position in the object,
c.Contact_Position = row["Contact_Position"].ToString();
// Store the Street in the object,
c.Contact_Street = row["Contact_Street"].ToString();
// Store the Zip in the object
c.Contact_Zip = row["Contact_Zip"].ToString();
// Store the CustomerSinceDate in the object
c.Contact_CustomerSinceDate = row["Contact_CustomerSinceDate"].ToString();
// Store the DateCreated in the object
c.Contact_DateCreated = row["Contact_DateCreated"].ToString();
// Store the Notes in the object
c.Contact_Note = row["Contact_Note"].ToString();
// Return the object
return c;
}
}
謝謝
也許你應該註冊一個事件處理程序dropdrown [更改事件](https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.textchanged(v = vs.110 ).aspx)並在那裏加載適當的數據。您當前的邏輯只在加載頁面時加載數據,這會爲默認下拉選擇設置數據,但之後不會更改。 –
謝謝!它現在有效。 – Cory