Last active
August 29, 2015 14:16
-
-
Save jammykam/d935b2c5b6054ca10282 to your computer and use it in GitHub Desktop.
Custom Lookup Field with labels
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Specialized; | |
using System.Web; | |
using System.Web.UI; | |
using Sitecore; | |
using Sitecore.Data.Items; | |
using Sitecore.Diagnostics; | |
using Sitecore.Shell.Applications.ContentEditor; | |
using Sitecore.StringExtensions; | |
using Sitecore.Text; | |
namespace LaunchSitecore.Shell | |
{ | |
public class NameLookupList : NameLookupValue | |
{ | |
private UrlString urlString = null; | |
protected override void OnLoad(EventArgs e) | |
{ | |
Assert.ArgumentNotNull((object)e, "e"); | |
string origValue = this.Value; | |
base.OnLoad(e); // this will modify this.Value using existing base logic and falsely throw a modified alert | |
this.Value = origValue; // so reset the value back for comparison below | |
Sitecore.Context.ClientPage.Modified = false; // if values have actually changed our code below will set this to true | |
urlString = new UrlString(this.Value); | |
if (Sitecore.Context.ClientPage.IsEvent) | |
this.LoadValue(); | |
else | |
this.BuildControl(); | |
} | |
private void LoadValue() | |
{ | |
if (this.ReadOnly || this.Disabled) | |
return; | |
System.Web.UI.Page page = HttpContext.Current.Handler as System.Web.UI.Page; | |
NameValueCollection nameValueCollection = page == null ? new NameValueCollection() : page.Request.Form; | |
UrlString urlString = new UrlString(); | |
foreach (string index1 in nameValueCollection.Keys) | |
{ | |
if (!string.IsNullOrEmpty(index1) && index1.StartsWith(this.ID + "_Param", StringComparison.InvariantCulture) && !index1.EndsWith("_value", StringComparison.InvariantCulture)) | |
{ | |
string input = nameValueCollection[index1]; | |
string str = nameValueCollection[index1 + "_value"]; | |
if (!string.IsNullOrEmpty(str)) | |
{ | |
urlString[input] = str; | |
} | |
} | |
} | |
string str1 = urlString.ToString(); | |
if (this.Value == str1) | |
return; | |
this.Value = str1; | |
this.SetModified(); | |
} | |
private void BuildControl() | |
{ | |
this.Controls.Clear(); | |
Item[] items = this.GetItems(Sitecore.Context.ContentDatabase.GetItem(this.ItemID)); | |
foreach (var item in items) | |
{ | |
this.Controls.Add(new LiteralControl(this.BuildParameterKeyValue(item))); | |
} | |
} | |
private string BuildParameterKeyValue(Item item) | |
{ | |
Assert.ArgumentNotNull(item, "item"); | |
string uniqueId = GetUniqueID(this.ID + "_Param"); | |
string vertical = this.IsVertical ? "</tr><tr>" : string.Empty; | |
string layout = "<table cellpadding=\"4\" cellspacing=\"0\" border=\"0\"><tr><td width=\"150px\">{0}{1}</td>{4}<td>{2} {3}</td></tr></table>"; | |
return layout.FormatWith( | |
item["Chemical Property"], | |
GetHtmlControl(uniqueId, null, "hidden", item.ID.ToString()), | |
GetHtmlControl(uniqueId, "_value", "text", urlString.Parameters[item.ID.ToString()]), | |
item["Chemical Unit"], | |
(object) vertical); | |
} | |
private string GetHtmlControl(string uniqueId, string suffix, string type, string value) | |
{ | |
string readOnly = this.ReadOnly ? " readonly=\"readonly\"" : string.Empty; | |
string disabled = this.Disabled ? " disabled=\"disabled\"" : string.Empty; | |
value = !String.IsNullOrEmpty(value) ? StringUtil.EscapeQuote(HttpUtility.UrlDecode(value)) : null; | |
string control = "<input id=\"{0}{1}\" name=\"{0}{1}\" type=\"{2}\" style=\"width:100px\" value=\"{3}\" {4} {5} />"; | |
return control.FormatWith(uniqueId, suffix, type, value, readOnly, disabled); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
http://i.imgur.com/9Cal9j3.png |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment