Setting the width of a MultipleLookupField control

When using a MultipleLookupField control in a webpart you can easily make the server create the select / deselect logic for you. The result of rendering a MultipleLookupField control will look something like this.


Unfortunately you haven’t got much control over how the control gets rendered and width properties aren’t easily changes. In this case the lookup values are too long and some of the text are therefore unreadable. Changing the width og the two listboxes (Select controls) should be proven rather difficult. However I came up with two different approaches. One where you do it by javascript and one where you can do it server-side in the webpart code.

Clientside approach:

You need to know the select element id for this one so it isn’t really all that useful unless you use something like a Content editor webpart to hold the script. But if you have the problem on just one specific page it can be an easy solution that doesn’t require any coding.

The javascript goes something like this

var Cand = document.getElementById(‘ctl00_m_g_5b989ee3_2686_4be0_be92_248cf98a2c16_RESEmneLU_ctl00_SelectCandidate’); = “300px”; = “300px”;
var Result = document.getElementById(‘ctl00_m_g_5b989ee3_2686_4be0_be92_248cf98a2c16_RESEmneLU_ctl00_SelectResult’); = “300px”; = “300px”;

The rendered MultipleLookupControl automatically gets two HTML select controls (SelectCandidates that holds the candidate values and SelectResult that holds the selected values). You simply changes the width of these two controls and their parent divs. You should of course replace the id’s (ctl00_m_g_5b989ee3_2686_4be0_be92_248cf98a2c16_RESEmneLU_ctl00_SelectCandidate) with your own. Find then by viewing the sourcecode in your browser

Serverside apporach:

The server-side approach is really doing the same just without having the add any Content Editor webparts and without having to know the rendered clientid. All sharepoint controls are initially inherited from the basic Control Class (System.Web.UI.Control) which have the property ClientID that represents the rendered clientID that we had to know with the Clientside approach.

The code for the client side could look something like this

        protected override void RenderContents(System.Web.UI.HtmlTextWriter writer)

        private string AddJavascript(string clientID)
            string s = “”;
            s += “”;
            s += “<script>\n”;
            s += string.Format(“var Cand = document.getElementById(‘{0}_ctl00_SelectCandidate’);\n”, clientID);
            s += “ = ‘300px’;\n”;
            s += “ = ‘300px’;\n”;
            s += string.Format(“var Result = document.getElementById(‘{0}_ctl00_SelectResult’);\n”, clientID);
            s += “ = ‘300px’;\n”;
            s += “ = ‘300px’;\n”;
            s += “</script>”;
            return s;

You can then change the new width values or pass it along the method call to AddJavascript as a parameter.

Regardless of the approach you select the end result should look like this

By the way – You can use the MultipleLookUpField in both a PageLayout and a custom webpart.

In a pagelayout the control could look something like this: <SharePointWebControls:MultipleLookupField runat=”server” id=”MainAreasField” FieldName=”MainAreas”/>
In a webpart the control could look something like this (Placed in the CreateChildControls section): 
                MultipleLookupField MainAreasLookup = new MultipleLookupField();
                MainAreasLookup.ID = “MainAreasLU”;
                MainAreasLookup.ListId = SubscriptionList.ID;
                MainAreasLookup.FieldName = “MainAreas”;
                MainAreasLookup.ControlMode = SPControlMode.Edit;
                MainAreasLookup.ItemId = SubscriptionID;

Good luck.


, ,

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: