November Happy Hour will be moved to Thursday December 5th.

Adding LinkItems to LinkItemCollection from CodeBehind

Vote:
 

Hi guys,

I would like to enable the editors to add links to LinkCollection right from the page.
There is this button called "Add Links". Activates Jquery which slides out 2 fields and a button.
Field 1                                  Field 2
Enter Name of the Link           Enter the http:// address                [Button Add]

If the user press the [Button], there will be a new line added with empty field and the user can add more links i.e

Field 1                                  Field 2                                         [Remove]
Field 1                                  Field 2                                         [Button Add]

Now over to my question:
I don't really know how to, in CodeBehind, add Field 1 and Field 2 (asp:TextBox) to a  LinkCollection.
Also, I guess i have to do asp.net asynchronous in order to skip postbacks for Link add.

Can anyone please point me in right direct or explain how i could complete this task?

//Thank

#42432
Aug 24, 2010 8:33
Vote:
 



    public partial class NewBlog : EPiServer.UserControlBase
    {
        private readonly string _removeInvalidRegEx = @"(</?)(i|p|b|strong|em)[^>]*(>)|<[^>]*>";
        private readonly string _restoreValidTagsRegEx = @"&lt;(/?)(i|p|b|strong|em)&gt;";

        protected void PostComment_Click(object sender, System.EventArgs e)
        {
            // Make sure that the page, and particurlarly the captcha, is valid.
            if (Page.IsValid)
            {
                PageData newPost = DataFactory.Instance.GetDefaultPageData(CurrentPage.PageLink,
                                                                              BlogUtil.ItemPageTypeName);

                // comment is used by a PropertyXhtmlString, and XhtmlString are built to work with an editor. Hence, they expect encoded text.
                // Therefor we must html encode the content.
                string post =
                    Server.HtmlEncode(Regex.Replace(PostTextNew.Text, _removeInvalidRegEx, "$1$2$3", RegexOptions.IgnoreCase));
                
                // name will be saved in a PropertyString, and should not be encoded. PropertyString encodeds itself when it's rendered.
                string name = Regex.Replace(NewPostName.Text, _removeInvalidRegEx, "$1$2$3", RegexOptions.IgnoreCase);

                post = Regex.Replace(post, _restoreValidTagsRegEx, "<$1$2>", RegexOptions.IgnoreCase);
                name = Regex.Replace(name, _restoreValidTagsRegEx, "<$1$2>", RegexOptions.IgnoreCase);

                newPost.PageName = name;
                newPost["MainBody"] = post;
                newPost.VisibleInMenu = false;

                // To get the new comment into the list. If we don't do this the
                // StartPublish value will be greater than the one used for
                // the publishing-filter.
                newPost.StartPublish = DateTime.Now.AddMinutes(-1);

                DataFactory.Instance.Save(newPost, EPiServer.DataAccess.SaveAction.Publish, AccessLevel.NoAccess);

                PostTextNew.Text = String.Empty;
                NewPostName.Text = String.Empty;

              
            }
        }//End

        protected void AddUrl_Click(object sender, System.EventArgs e)
        {
            //btnAdd.Click += new EventHandler(this.AddUrl_Click);
            LinkItemCollection links = ((LinkItemCollection)CurrentPage["LinkCollectionProperty"]);
            //LinkName  -  LinkUrl
            
            LinkItem link = new LinkItem();
            link.Href = LinkUrl.Text;
            link.Text = LinkName.Text;
            link.Title = LinkName.Text;

            links.Add(link);

        }
    }
}

Getting the EM:

Object reference not set to an instance of an object.

#42440
Aug 24, 2010 10:44
Vote:
 

Which line gets the error? My guess is that the links variable is null, and then you do Add on it. If the property has not been previously set on the page, its value will be null.

Mind also that properties are read-only unless you create a writeable clone of the parent page first.

#42441
Aug 24, 2010 10:58
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.