0

enter image description here

protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            using (SPSite site = new SPSite("http://wingtip:49074"))
            {
                LabelShowTitle.Text = string.Empty;
                LabelShowContents.Text = string.Empty;
                LabelShowDatum.Text = string.Empty;
                LabelAuthor.Text = string.Empty;
                LabelLink.Text = string.Empty;

                SPWeb web = SPContext.Current.Web;
                SPList list = web.Lists.TryGetList(DropDownListSelectCategory.SelectedItem.Value);

                DateTime dt = DateTime.Now;

                SPListItemCollection items = list.GetItems();

                {
                    foreach (SPListItem news in items)
                    {
                        TextBoxContents2.Text = news["Contents"].ToString();
                        LabelShowTitle2.Text = news["Title"].ToString();
                        //LabelShowContents.Text = news["Contents"].ToString();
                        LabelShowDatum2.Text = news["Date"].ToString();
                        LabelAuthor2.Text = news["Authors"].ToString();
                        LabelLink2.Text = new SPFieldUrlValue(news["Links"].ToString()).Url;

                        TextBoxContents.Text = news["Contents"].ToString();
                        LabelShowTitle.Text = news["Title"].ToString();
                        //LabelShowContents.Text = news["Contents"].ToString();
                        LabelShowDatum.Text = news["Date"].ToString();
                        LabelAuthor.Text = news["Authors"].ToString();
                        LabelLink.Text = new SPFieldUrlValue(news["Links"].ToString()).Url;
                    }
                    site.Dispose();
                }
            }

My Problem is that i want it to display in the first news on news that i have created in my list as u see and in the other one display the other news "My other News"

1 Answer 1

0

With your code it will only take values from single list item. You can use following code to achieve what you want:

SPListItemCollection items = list.GetItems();
                    TextBoxContents2.Text = items[0]["Contents"].ToString();
                    LabelShowTitle2.Text = items[0]["Title"].ToString();
                    LabelShowDatum2.Text = items[0]["Date"].ToString();
                    LabelAuthor2.Text = items[0]["Authors"].ToString();
                    LabelLink2.Text = new SPFieldUrlValue(items[0]["Links"].ToString()).Url;

                    TextBoxContents.Text = items[1]["Contents"].ToString();
                    LabelShowTitle.Text = items[1]["Title"].ToString();
                    //LabelShowContents.Text = news["Contents"].ToString();
                    LabelShowDatum.Text = items[1]["Date"].ToString();
                    LabelAuthor.Text = items[1]["Authors"].ToString();
                    LabelLink.Text = new SPFieldUrlValue(items[1]["Links"].ToString()).Url;

However, I would advise that you do a proper error handling.

3
  • Hi! I works find but when i create new news its puts the latest news that i have created in the buttom
    – Batistuta
    Commented Jan 22, 2013 at 14:58
  • You need to actually define your requirements properly. This code will show only two news items. You can have several items in the list. What is your logic of getting the list items? Do you need the latest/oldest two news items. Commented Jan 22, 2013 at 15:24
  • Hi! I need the latest news items. I think maybe i can have an SPquery to solve it but i dont now how to write the query or what do u think?
    – Batistuta
    Commented Jan 22, 2013 at 16:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.