Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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"

share|improve this question

1 Answer 1

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.

share|improve this answer
    
Hi! I works find but when i create new news its puts the latest news that i have created in the buttom –  Martin Jan 22 '13 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. –  Nadeem Yousuf Jan 22 '13 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? –  Martin Jan 22 '13 at 16:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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