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

Need to get the Newest added Item To Display in Texboxes and Labels

try
        {
            using (SPSite site = new SPSite("http://wingtip:49074"))
            {
                TextBoxContents.Visible = true;
                TextBoxContents2.Visible = true;
                LabelShowTitle.Text = string.Empty;
                TextBoxContents.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);

                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();

                        LabelShowDatum.Text = items[1]["Date"].ToString();
                        LabelAuthor.Text = items[1]["Authors"].ToString();
                        LabelLink.Text = new SPFieldUrlValue(items[1]["Links"].ToString()).Url;

            }
        }

Even how many items add in my List i what it to display the latest added someone now how to do that ? and also the old ones. I what the latest added item in the List to Update the Textboxes and Labels

share|improve this question
add comment

2 Answers 2

The latest item added would be the last item in the collection, so you could try something like this:

SPListItemCollection items = list.GetItems();

TextBoxContents.Text = items[items.Count - 1]["Contents"].ToString();
LabelShowTitle.Text = items[items.Count - 1]["Title"].ToString();

(Remember that the collection index is zero-based, and the "Count" property is not, so you have to subtract one from the Count to get the last index number.)

EDIT: Upon further consideration, maybe this bit about event receivers is not applicable here...

As far as having the latest item added automatically update the labels / textboxes, you might have to do that through an ItemAdded event receiver attached to the list.

This might also be helpful regarding event receivers.

share|improve this answer
    
i dont really understand what u mean with , As far as having the latest item added automatically update the labels / textboxes, you might have to do that through an ItemAdded event receiver attached to the list. –  Martin Jan 22 '13 at 20:17
    
Plus i dont get it to work beacuse all the news updates i what to keep the old and then add the new news –  Martin Jan 22 '13 at 20:24
    
Hmm, maybe you are right, maybe using an event receiver in this situation doesn't make sense. I'll edit my answer to reflect that. As far as keeping the old and adding the new... so you mean you want to show all the list items, no matter how many there are? –  Dylan Cristy Jan 22 '13 at 20:34
add comment

tbh I dont know exactly what you want to do, I think you want to iterate through the list and display the values? I had to redo your code to add the controls and assign a unique id per control, as the controls are all taken from the same one you need to increment the value as i have done, as i have already incremented the int value you can add it to the list to increment through that aswell. This will display the whole list start to finish :). I havent tested it as i only did it in notepad and have no means to test it atm :( but im sure it will work like a charm ;)

    try
    {
        using (SPSite site = new SPSite("http://wingtip:49074"))
        {
            TextBox TextBoxContents;
            Label LabelShowTitle;
            Label LabelShowDatum;
            Label LabelAuthor;
            Label LabelLink;


            SPWeb web = SPContext.Current.Web;

            SPList list = web.Lists.TryGetList(DropDownListSelectCategory.SelectedItem.Value);
            int increment = 0;
            foreach(SPListItem item in spList.Items)
            {
                TextBoxContents = TextBox();
                LabelShowTitle = Label();
                LabelShowDatum = Label();
                LabelAuthor = Label();
                LabelLink = Label();

                TextBoxContents.Text = items[increment]["Contents"].ToString();
                TextBoxContents.ID = "TextBoxContents"+increment;
                LabelShowTitle.Text = items[increment]["Title"].ToString();
                LabelShowTitle.ID = "LabelShowTitle"+increment;
                LabelShowDatum.Text = items[increment]["Date"].ToString();
                LabelShowDatum.ID ="LabelShowDatum"+increment;
                LabelAuthor.Text = items[increment]["Authors"].ToString();
                LabelAuthor.ID = "LabelAuthor"+increment; 
                LabelLink.Text = new SPFieldUrlValue(items[increment]["Links"].ToString()).Url;
                LabelLink.ID = "LabelLink"+increment;

                this.Controls.Add(TextBoxContents);
                this.Controls.Add(LabelShowTitle);
                this.Controls.Add(LabelShowDatum);
                this.Controls.Add(LabelAuthor);
                this.Controls.Add(LabelLink);

                increment++;
            }
        }
    }
    catch(Exception a)
    {
    }
share|improve this answer
    
Hi! Thanks itw works fine it updates but let me explease what i am doing , i am doing an news site in sharepoint there i save the news to the list then i what to display in in another webpart that show the news but now the problem is that the first news updates but i also what to keep the old news in the bottom of the code that u send me righ now –  Martin Jan 22 '13 at 20:47
    
So when i click in my dropbox we say i what to red about fotball and press ok button i want it to display new news and old news that i have in my list –  Martin Jan 22 '13 at 20:48
    
so if i upload two news the first news should be first and second the old from the list so this textboxes should show the old news –  Martin Jan 22 '13 at 20:53
add comment

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.