下拉列表选项不会更改数据

时间:2017-04-16 16:32:57

标签: c# asp.net



我的下拉列表不能正常工作,我想要它,我无法弄清楚原因。我查看了stackoverflow,但找不到与我相符的问题。我的下拉列表显示数据库中的所有名称,但是当我选择名称时,附加到数据库中名称的数据不会出现在文本框中。第一个名称的数据在文本框中正确加载,但之后数据不会更改。在涉及datasouceselect代码时,我必须做错事。这是我的代码。

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;



    }

}

谢谢

1 个答案:

答案 0 :(得分:0)

确保在“OnSelectedIndexChanged”事件处理程序中处理下拉列表选择更改的代码。

此外,似乎ContactID列是一个数字int或类似的东西,如果是这样,请确保从该行中删除单引号:

Contacts.RowFilter = string.Format("ContactID = '{0}'", DropDownList1.SelectedValue);

是:

Contacts.RowFilter = string.Format("ContactID = {0}", DropDownList1.SelectedValue);

希望它有所帮助。