更新列表框中所选项目的文本框中的文本。列表框工作正常

时间:2012-06-17 00:12:40

标签: c# asp.net sql textbox listbox

我构建了一个WinForm应用程序,可以正常使用在线存储的SQL数据库。我现在正试图转向asp.net应用程序。在学习了构建WinForm应用程序所需的一切之后,我对新的学习曲线感到沮丧!

这是一个基本应用程序,其中包含数据库中所有人的列表框。列表框显示组合的NameLast +','+ NameFirst作为NameFull显示类似“Smith,John”的内容。当用户选择人时,文本框应显示相关信息。一旦我开始它应该很容易但是...如何构建一个OnSelectedChanged类型命令来填充文本框。在这个例子中,我将有四(4)个文本框:NameFirstTxt,NameMiddleTxt,NameLastTxt和RecordIDTxt。

我附加了在所选索引更改事件期间使用的WinForm应用程序的代码。 任何建议和评论(好的或坏的)都会有所帮助。提前谢谢。

    private void peopleDetailsDirectoryLbx_SelectedIndexChanged(object sender, EventArgs e)
    {
        peopleDirectoryGbx.Text = "People Records : " + peopleDetailsDirectoryLbx.Items.Count.ToString();
        DataRowView PeopleRow = peopleDetailsDirectoryLbx.SelectedItem as DataRowView;
        if (PeopleRow != null && PeopleRow.Row != null && PeopleRow.Row.ItemArray != null)
        {
            DataRow row = PeopleRow.Row;
            if (row.ItemArray.Count() > 0)
                try
                {
                    peopleDetailsIDTxt.Text = Convert.ToString(row["ID"]).ToString();

                    try
                    {
                        peopleDetailsPhotoImg.Load(@"http://www.officertech.com/PeoplePhotoBase/" + peopleDetailsIDTxt.Text + ".jpg");
                    }
                    catch
                    {
                        peopleDetailsPhotoImg.Load(@"http://www.officertech.com/PeoplePhotoBase/NoPhoto.jpg");
                    }
                    if (Convert.ToString(row["Active"].ToString()) == "True")
                    {
                        peopleDetailsActiveChk.Checked = true;
                    }
                    else
                    {
                        peopleDetailsActiveChk.Checked = false;
                    }
                    peopleDetailsNameFirstTxt.Text = (row["NameFirst"]).ToString();
                    peopleDetailsNameMiddleTxt.Text = (row["NameMiddle"]).ToString();
                    peopleDetailsNameLastTxt.Text = (row["NameLast"]).ToString();
                    peopleDetailsNameAka1Txt.Text = (row["NameAKA1"]).ToString();
                    peopleDetailsNameAka2Txt.Text = (row["NameAKA2"]).ToString();
                    peopleDetailsDobTxt.Text = Convert.ToDateTime(row["DOB"]).ToShortDateString();
                    peopleDetailsRaceCbx.Text = (row["Race"]).ToString();
                    peopleDetailsGenderCbx.Text = (row["Gender"]).ToString();
                    peopleDetailsAddStreetNumTxt.Text = (row["AddStreetNum"]).ToString();
                    peopleDetailsStreetNameCbx.Text = (row["AddStreetName"]).ToString();
                    peopleDetailsAddCityTxt.Text = (row["AddCity"]).ToString();
                    peopleDetailsAddStateTxt.Text = (row["AddState"]).ToString();
                    peopleDetailsAddZipTxt.Text = (row["AddZip"]).ToString();
                    peopleDetailsAddCountyTxt.Text = (row["AddCounty"]).ToString();
                    peopleDetailsDriverLicenseTxt.Text = (row["DriverLicense"]).ToString();
                    peopleDetailsDriverLicenseStateTxt.Text = (row["DriverLicenseState"]).ToString();
                    peopleDetailsSSNTxt.Text = (row["SSN"]).ToString();
                    peopleDetailsTrackingLab.ForeColor = Color.Black;
                    if (row["Tracking"].ToString() == "True")
                    {
                        peopleDetailsTrackingYesRad.Checked = true;
                        peopleDetailsTrackingLab.ForeColor = Color.Red;
                    }
                    else
                    {
                        peopleDetailsTrackingNoRad.Checked = true;
                        peopleDetailsTrackingLab.ForeColor = Color.Black;
                    }
                    peopleDetailsPhysIDColorHairCbx.Text = (row["PhysIDColorHair"].ToString());
                    peopleDetailsPhysIDColorEyesCbx.Text = (row["PhysIDColorEyes"].ToString());
                    peopleDetailsPhysIDWeightTxt.Text = (row["PhysIDWeight"].ToString());
                    peopleDetailsPhysIDHeightTxt.Text = (row["PhysIDHeight"].ToString());
                    peopleDetailsPhysIDScarTattTxt.Text = (row["PhysIDScarTatt"].ToString());
                    //////
                    if (Convert.ToString(row["School"].ToString()) == "True")
                    {
                        peopleDetailsSchoolRad.Checked = true;
                    }
                    else
                    {
                        peopleDetailsSchoolRad.Checked = false;
                    }
                    //////
                    if (Convert.ToString(row["Work"].ToString()) == "True")
                    {
                        peopleDetailsWorkRad.Checked = true;
                    }
                    else
                    {
                        peopleDetailsWorkRad.Checked = false;
                    }
                    //////
                    if (Convert.ToString(row["ciAssault"].ToString()) == "True")
                    {
                        peopleCIAssaultChk.Checked = true;
                        peopleCIAssaultChk.ForeColor = Color.Red;
                    }
                    else
                    {
                        peopleCIAssaultChk.Checked = false;
                        peopleCIAssaultChk.ForeColor = Color.Black;
                    }
                    //////
                    peopleDetailsSchoolWorkNameTxt.Text = (row["SchoolWorkName"]).ToString();
                    //////
                    if (Convert.ToString(row["ciBattery"].ToString()) == "True")
                    {
                        peopleCIBatteryChk.Checked = true;
                        peopleCIBatteryChk.ForeColor = Color.Red;
                    }
                    else
                    {
                        peopleCIBatteryChk.Checked = false;
                        peopleCIBatteryChk.ForeColor = Color.Black;
                    }
                    //////
                    if (Convert.ToString(row["ciBatteryAgg"].ToString()) == "True")
                    {
                        peopleCIBatteryAggChk.Checked = true;
                        peopleCIBatteryAggChk.ForeColor = Color.Red;
                    }
                    else
                    {
                        peopleCIBatteryAggChk.Checked = false;
                        peopleCIBatteryAggChk.ForeColor = Color.Black;
                    }
                    //////
                    if (Convert.ToString(row["ciBatteryDVR"].ToString()) == "True")
                    {
                        peopleCIBatteryDVRChk.Checked = true;
                        peopleCIBatteryDVRChk.ForeColor = Color.Red;
                    }
                    else
                    {
                        peopleCIBatteryDVRChk.Checked = false;
                        peopleCIBatteryDVRChk.ForeColor = Color.Black;
                    }
                    //////
                    if (Convert.ToString(row["ciBatteryLEO"].ToString()) == "True")
                    {
                        peopleCIBatteryLEOChk.Checked = true;
                        peopleCIBatteryLEOChk.ForeColor = Color.Red;
                    }
                    else
                    {
                        peopleCIBatteryLEOChk.Checked = false;
                        peopleCIBatteryLEOChk.ForeColor = Color.Black;
                    }
                    //////
                    if (Convert.ToString(row["ciBurglary"].ToString()) == "True")
                    {
                        peopleCIBurglaryChk.Checked = true;
                        peopleCIBurglaryChk.ForeColor = Color.Red;
                    }
                    else
                    {
                        peopleCIBurglaryChk.Checked = false;
                        peopleCIBurglaryChk.ForeColor = Color.Black;
                    }
                    //////
                    if (Convert.ToString(row["ciChildCrimes"].ToString()) == "True")
                    {
                        peopleCIChildCrimeChk.Checked = true;
                        peopleCIChildCrimeChk.ForeColor = Color.Red;
                    }
                    else
                    {
                        peopleCIChildCrimeChk.Checked = false;
                        peopleCIChildCrimeChk.ForeColor = Color.Black;
                    }
                    //////
                    if (Convert.ToString(row["ciCrimMisch"].ToString()) == "True")
                    {
                        peopleCICrimMischChk.Checked = true;
                        peopleCICrimMischChk.ForeColor = Color.Red;
                    }
                    else
                    {
                        peopleCICrimMischChk.Checked = false;
                        peopleCICrimMischChk.ForeColor = Color.Black;
                    }
                    //////
                    if (Convert.ToString(row["ciDealStolen"].ToString()) == "True")
                    {
                        peopleCIDealStolenChk.Checked = true;
                        peopleCIDealStolenChk.ForeColor = Color.Red;
                    }
                    else
                    {
                        peopleCIDealStolenChk.Checked = false;
                        peopleCIDealStolenChk.ForeColor = Color.Black;
                    }
                    //////
                    if (Convert.ToString(row["ciDisorderly"].ToString()) == "True")
                    {
                        peopleCIDisorderlyChk.Checked = true;
                        peopleCIDisorderlyChk.ForeColor = Color.Red;
                    }
                    else
                    {
                        peopleCIDisorderlyChk.Checked = false;
                        peopleCIDisorderlyChk.ForeColor = Color.Black;
                    }
                    //////
                    if (Convert.ToString(row["ciDrugs"].ToString()) == "True")
                    {
                        peopleCIDrugsChk.Checked = true;
                        peopleCIDrugsChk.ForeColor = Color.Red;
                    }
                    else
                    {
                        peopleCIDrugsChk.Checked = false;
                        peopleCIDrugsChk.ForeColor = Color.Black;
                    }
                    //////
                    if (Convert.ToString(row["ciDUI"].ToString()) == "True")
                    {
                        peopleCIDUIChk.Checked = true;
                        peopleCIDUIChk.ForeColor = Color.Red;
                    }
                    else
                    {
                        peopleCIDUIChk.Checked = false;
                        peopleCIDUIChk.ForeColor = Color.Black;
                    }
                    //////
                    if (Convert.ToString(row["ciDWLSR"].ToString()) == "True")
                    {
                        peopleCIDWLSRChk.Checked = true;
                        peopleCIDWLSRChk.ForeColor = Color.Red;
                    }
                    else
                    {
                        peopleCIDWLSRChk.Checked = false;
                        peopleCIDWLSRChk.ForeColor = Color.Black;
                    }
                    //////
                    if (Convert.ToString(row["ciFraud"].ToString()) == "True")
                    {
                        peopleCIFraudChk.Checked = true;
                        peopleCIFraudChk.ForeColor = Color.Red;
                    }
                    else
                    {
                        peopleCIFraudChk.Checked = false;
                        peopleCIFraudChk.ForeColor = Color.Black;
                    }
                    //////
                    if (Convert.ToString(row["ciHomicide"].ToString()) == "True")
                    {
                        peopleCIHomicideChk.Checked = true;
                        peopleCIHomicideChk.ForeColor = Color.Red;
                    }
                    else
                    {
                        peopleCIHomicideChk.Checked = false;
                        peopleCIHomicideChk.ForeColor = Color.Black;
                    }
                    //////
                    if (Convert.ToString(row["ciLewdLasciv"].ToString()) == "True")
                    {
                        peopleCILewdLascivChk.Checked = true;
                        peopleCILewdLascivChk.ForeColor = Color.Red;
                    }
                    else
                    {
                        peopleCILewdLascivChk.Checked = false;
                        peopleCILewdLascivChk.ForeColor = Color.Black;
                    }
                    //////
                    if (Convert.ToString(row["ciMental"].ToString()) == "True")
                    {
                        peopleCIMentalChk.Checked = true;
                        peopleCIMentalChk.ForeColor = Color.Red;
                    }
                    else
                    {
                        peopleCIMentalChk.Checked = false;
                        peopleCIMentalChk.ForeColor = Color.Black;
                    }
                    //////
                    peopleCIProbationLab.ForeColor = Color.Black;
                    if (row["ciProbation"].ToString() == "True")
                    {
                        peopleCIProbationYesRad.Checked = true;
                        peopleCIProbationLab.ForeColor = Color.Red;
                    }
                    else
                    {
                        peopleCIProbationNoRad.Checked = true;
                        peopleCIProbationLab.ForeColor = Color.Black;
                    }
                    //////
                    if (Convert.ToString(row["ciResisting"].ToString()) == "True")
                    {
                        peopleCIResistingChk.Checked = true;
                        peopleCIResistingChk.ForeColor = Color.Red;
                    }
                    else
                    {
                        peopleCIResistingChk.Checked = false;
                        peopleCIResistingChk.ForeColor = Color.Black;
                    }
                    //////
                    if (Convert.ToString(row["ciRobbery"].ToString()) == "True")
                    {
                        peopleCIRobberyChk.Checked = true;
                        peopleCIRobberyChk.ForeColor = Color.Red;
                    }
                    else
                    {
                        peopleCIRobberyChk.Checked = false;
                        peopleCIRobberyChk.ForeColor = Color.Black;
                    }
                    //////
                    if (Convert.ToString(row["ciSexCrime"].ToString()) == "True")
                    {
                        peopleCISexCrimeChk.Checked = true;
                        peopleCISexCrimeChk.ForeColor = Color.Red;
                    }
                    else
                    {
                        peopleCISexCrimeChk.Checked = false;
                        peopleCISexCrimeChk.ForeColor = Color.Black;
                    }
                    //////
                    if (Convert.ToString(row["ciShoplift"].ToString()) == "True")
                    {
                        peopleCIShopLiftChk.Checked = true;
                        peopleCIShopLiftChk.ForeColor = Color.Red;
                    }
                    else
                    {
                        peopleCIShopLiftChk.Checked = false;
                        peopleCIShopLiftChk.ForeColor = Color.Black;
                    }
                    //////
                    if (Convert.ToString(row["ciTheft"].ToString()) == "True")
                    {
                        peopleCITheftChk.Checked = true;
                        peopleCITheftChk.ForeColor = Color.Red;
                    }
                    else
                    {
                        peopleCITheftChk.Checked = false;
                        peopleCITheftChk.ForeColor = Color.Black;
                    }
                    //////
                    if (Convert.ToString(row["ciTraffic"].ToString()) == "True")
                    {
                        peopleCITrafficChk.Checked = true;
                        peopleCITrafficChk.ForeColor = Color.Red;
                    }
                    else
                    {
                        peopleCITrafficChk.Checked = false;
                        peopleCITrafficChk.ForeColor = Color.Black;
                    }
                    //////
                    if (Convert.ToString(row["ciTrespass"].ToString()) == "True")
                    {
                        peopleCITrespassChk.Checked = true;
                        peopleCITrespassChk.ForeColor = Color.Red;
                    }
                    else
                    {
                        peopleCITrespassChk.Checked = false;
                        peopleCITrespassChk.ForeColor = Color.Black;
                    }
                    //////
                    if (Convert.ToString(row["ciWarrants"].ToString()) == "True")
                    {
                        peopleCIWarrantsChk.Checked = true;
                        peopleCIWarrantsChk.ForeColor = Color.Red;
                    }
                    else
                    {
                        peopleCIWarrantsChk.Checked = false;
                        peopleCIWarrantsChk.ForeColor = Color.Black;
                    }
                    //////
                    if (Convert.ToString(row["ciWeapons"].ToString()) == "True")
                    {
                        peopleCIWeaponsChk.Checked = true;
                        peopleCIWeaponsChk.ForeColor = Color.Red;
                    }
                    else
                    {
                        peopleCIWeaponsChk.Checked = false;
                        peopleCIWeaponsChk.ForeColor = Color.Black;
                    }
                    //////
                    CalcAge_People();
                    //arrestFieldsFill();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Problem Locating Person Information" + ex, "CARDS 2012 | Message Center", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
        }
    }

1 个答案:

答案 0 :(得分:1)

确保ListBox的 AutoPostback 属性设置为true。

Aspx代码:

<asp:ListBox ID="ListBox1" runat="server" OnSelectedIndexChanged="ListBox1_SelectedIndexChanged"
        AutoPostBack="True">
        <asp:ListItem>1st Person</asp:ListItem>
        <asp:ListItem>2nd Person</asp:ListItem>
        <asp:ListItem>3rd person</asp:ListItem>
    </asp:ListBox>
    <br />
    FIRST NAME:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <br />
    LASTNAME:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    <br />
    EMAIL:<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>

代码背后:

protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (ListItem li in ListBox1.Items)
            {
                if (li.Selected == true)
                {
                    // what ever you want to assign to TextBox 
                    this.TextBox1.Text = li.Text + "First Name"; // (row["NameFirst"]).ToString();
                    this.TextBox2.Text = li.Text + "Last Name";
                    this.TextBox3.Text = li.Text + "Email";
                }
            }
        }

希望这有助于。