locked
lookup value from another list RRS feed

  • Question

  • i created a list (List A)  that has the following columns

    Staff ID | Rank | Age ...

    and another list (List B)  that has the following columns Staff ID | Dept. |  Age | Sex

    i set Staff ID property in List B to lookup from List A.

    Pls, i want when a Staff ID is selected from the lookup drop-down on list B the corresponding Age in list A will automatically display on Age column in list B.

    thanks,


    • Edited by Mike Walsh FIN Wednesday, January 11, 2012 2:19 PM while i'm waiting removed don't be pushy
    Monday, January 9, 2012 3:30 PM

Answers

  • Hi Menyki,

     

    According to your description, I suggest you to develop an customer web part to achieve it. here is my simple code ,you can have a try. About how to develop a web part, you can refer to the following link.

    http://oidatsmyleg.wordpress.com/2009/07/23/wrap-a-user-control-inside-a-web-part-using-wspbuilder/

     

     

      public void bind()

            {

                this.DropDownList1.DataTextField = "Title";

                this.DropDownList1.DataValueField = "staffID";

                this.DropDownList1.DataSource = GetSource();

                this.DropDownList1.DataBind();

            }

     

            public DataTable GetSource()

            {

                using (SPSite site = new SPSite("http://jack-7716f30e37:2012/personal/Test/"))

                {

                    using (SPWeb Web = site.OpenWeb())

                    {

                        SPList oList = Web.Lists["A"];

                        DataTable dt = oList.Items.GetDataTable();

                        return dt;

                    }

                }

            }

     

            protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)

            {

                using (SPSite site = new SPSite("http://jack-7716f30e37:2012/personal/Test/"))

                {

                    using (SPWeb Web = site.OpenWeb())

                    {

                        SPList oList = Web.Lists["A"];

                        SPQuery query = new SPQuery();

                        query.Query = " <Where><Eq><FieldRef Name='staffID' /><Value Type='Text'>" + this.DropDownList1.SelectedValue + "</Value></Eq> </Where>";

                        SPListItemCollection ic= oList.GetItems(query);

                        foreach (SPListItem i in ic)

                        {

                          this.TextBox1.Text=i["Age"].ToString();

                        }

                    }

                }

            }

     

    Thanks,

    Jack

    Tuesday, January 10, 2012 5:20 AM
    Moderator