Dropdownlist选项不会更改数据
问题描述:
我的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;
}
}
谢谢
答
确保您处理下拉列表选择“OnSelectedIndexChanged”事件处理程序中改变代码。
而且,似乎使用ContactID列是一个数字int或类似的东西,如果是这样,请确保您从该行删除单引号:
Contacts.RowFilter = string.Format("ContactID = '{0}'", DropDownList1.SelectedValue);
是:
Contacts.RowFilter = string.Format("ContactID = {0}", DropDownList1.SelectedValue);
希望它有帮助。
也许你应该注册一个事件处理程序dropdrown [更改事件](https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.textchanged(v = vs.110 ).aspx)并在那里加载适当的数据。您当前的逻辑只在加载页面时加载数据,这会为默认下拉选择设置数据,但之后不会更改。 –
谢谢!它现在有效。 – Cory