2

I'm creating a Custom Field that shows me multiple links... In edit or new mode it uses an Asset Picker to let the user choose links... When the item is display in the view it shows (using overriding of GetFieldValueAsHtml and getType) and seems to work great..

The problem i'm facing appears when i Diplay the single Item (clicking on it). It shows me the "default value" and not the controls created by the create child control i write down... Debugging i can see he goes in the code part, but this does not affect my html response.. Any suggestion?? Is there anything else to override?

protected override void CreateChildControls()
    {
        base.CreateChildControls();
        if ((base.ControlMode == SPControlMode.New) || (base.ControlMode == SPControlMode.Edit))
        {
            list = new DVListBox();
            list.Width = 380;
            this.Controls.Add(list);

            HtmlGenericControl br = new HtmlGenericControl("br");
            this.Controls.Add(br);


            urlSelector = new AssetUrlSelector();
            urlSelector.PickerButtonText = "Add";
            //urlSelector.AssetUrlTextBoxVisible = false;
            urlSelector.AutoPostBack = true;
            this.Controls.Add(urlSelector);

            HtmlGenericControl br2 = new HtmlGenericControl("br");
            this.Controls.Add(br2);

            HtmlInputButton remove = new HtmlInputButton();
            remove.ID = "REMOVE_ITEM";
            remove.Value = "Remove";
            remove.Style.Add(HtmlTextWriterStyle.Width, "150px");
            remove.ServerClick += new EventHandler(reset_ServerClick);
            this.Controls.Add(remove);

            HtmlInputButton reset = new HtmlInputButton();
            reset.ID = "RESET_FIELD";
            reset.Value = "Reset Field";
            reset.Style.Add(HtmlTextWriterStyle.Width, "150px");
            reset.ServerClick += new EventHandler(reset_ServerClick);
            this.Controls.Add(reset);

            if (ViewState["Links"] == null)
            {
                if (this.ItemFieldValue == null)
                {
                    ViewState["Links"] = new List<string>();
                }
                else
                {
                    ViewState["Links"] = ConvertStringToList((string)this.ItemFieldValue);
                    list.DataSource = ViewState["Links"];
                    list.DataBind();
                }
            }
        }
        else
        {

            if (base.ControlMode == SPControlMode.Display)
            {
                StringBuilder sb = new StringBuilder();
                using (SPWeb spWeb = SPContext.Current.Web)
                {
                    string[] indirizzi = base.ListItemFieldValue.ToString().Split(';');

                    foreach (string s in indirizzi)
                    {
                        if (s != String.Empty)
                        {
                            try
                            {
                                SPFile file = spWeb.GetFile(s);

                                try
                                {
                                    file.OpenBinary();
                                    sb.Append("<a href=\"" + s + "\"> " + file.Name + " </a><br />");
                                }
                                catch (Exception exx)
                                {
                                }
                            }
                            catch (System.IO.FileNotFoundException exx)
                            {
                                sb.Append("<a href=\"" + s + "\"> " + s + " </a><br />");
                            }
                        }

                    }
                }
                HtmlGenericControl controllo = new HtmlGenericControl();
                controllo.InnerHtml = sb.ToString();
                this.Controls.Add(controllo);
            }
        }


    }


    protected override void Render(HtmlTextWriter output)
    {
        base.Render(output);
    }

Do i have to change the OnRender? Thank you very much!!

1
  • This should be posted as an answer, as this will help people find it and show that a solution has been found. I have done this for you as a community wiki answer. Thanks.
    – SPDoctor
    Dec 2, 2011 at 13:39

1 Answer 1

0

The OnRender function calls the base.onRender and this "counter" my code... I modify the function something like that (and now it works)

  protected override void Render(HtmlTextWriter output)
    {
        if (base.ControlMode == SPControlMode.Display)
        {
            StringBuilder sb = new StringBuilder();
            using (SPWeb spWeb = SPContext.Current.Web)
            {
                string[] indirizzi = base.ListItemFieldValue.ToString().Split(';');

                foreach (string s in indirizzi)
                {
                    if (s != String.Empty)
                    {
                        try
                        {
                            SPFile file = spWeb.GetFile(s);

                            try
                            {
                                file.OpenBinary();
                                sb.Append("<a href=\"" + s + "\"> " + file.Name + " </a><br />");
                            }
                            catch (Exception exx)
                            {
                            }
                        }
                        catch (System.IO.FileNotFoundException exx)
                        {
                            sb.Append("<a href=\"" + s + "\"> " + s + " </a><br />");
                        }
                    }

                }
            }
            output.Write(sb.ToString());
        }
        else
        {
            base.Render(output);
        }
    }

Now the Custom Field works! If any has questions about it, contact me and i'll be pleased to answer you!

(posted as community wiki on behalf of original poster)

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.