Auto Resize Dropdown Box

Auto Resize Dropdown

Sometimes, using a built-in dropdown control does not meet your requirement. Suppose a dropdown box contains text that is longer in length than your box; therefore, the width of the dropdown will increase as the text length increases. But, an increase in the dropdown's size affects your form design. You don't have enough space on your page to fit this lengthy dropdown box. In that case, you can use this Auto Resize Dropdown box.

Create a blank solution; in the solution, add a class library; and in the class file, add the following code. This code shows that, at the time of rendering the control, with the help of a style you can maintain the initial width of your dropdown box.

Note: You must inherit WebControls.DropDownList.
Inherits WebControls.DropDownList
   Protected Overrides Sub Render(ByVal writer As _
      System.Web.UI.HtmlTextWriter)
      writer.Write("<div style='width:" & Me.Width.ToString & "'>")
      MyBase.Render(writer)
      writer.WriteEndTag("div")
End Sub

At the time of initialization, it will call JavaScript functions. These JavaScript functions will attach with onactivate and ondeactivate events.

Private Sub AutoSizeDropDown_Init(ByVal sender As Object, _
   ByVal e As System.EventArgs) Handles Me.Init
   Dim ControlsJS As String
   ControlsJS = "<script type=""text/javascript"" src=""" & 
   HttpRuntime.AppDomainAppVirtualPath & "/js/CustomControls.js""> _
   </script>"
   Me.Attributes.Add("originalWidth", "")
   Me.Attributes.Add("onactivate", "RearrangeDdnWidth(this)")
   Me.Attributes.Add("ondeactivate", "ResetDdnWidth(this)")
   Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), _
      "ControlsJS", ControlsJS)
End Sub

After building the solution, add a new web site in your solution. In the default.aspx page, register your DLL with the page.

<%@ Register Assembly="AutoDropDown"
    Namespace="AutoDropDown" TagPrefix="cc1" %>

After that, you can add the dropdown:

<cc1:AutoSizeDropDown runat="server"
                      ID="AutoSizeDropDown1"
                      Width="50px"></cc1:AutoSizeDropDown>

The complete source code is available with this article.



About the Author

Ashish Singh

I am working as I.T. Analyst at Tata Consultancy Services Ltd. I have 4+ years of experience in Microsoft Technologies. I have worked primarily as a Dot Net Developer with C# and VB.Net as code behind.

Downloads

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • IT decision support impacts all aspects of technology management, from governance and strategy to budgets and resource planning. IT decision support effectiveness often falls prey to data-driven challenges that make it difficult to understand the data in context. These challenges: overwhelming data volumes, heterogeneous data types, and growing data complexity. This Forrester Consulting Paper reports the three key findings from their study conducted, on behalf of BDNA, to test the hypothesis that data …

  • The first phase of API management was about realizing the business value of APIs. This next wave of API management enables the hyper-connected enterprise to drive and scale their businesses as API models become more complex and sophisticated. Today, real world product launches begin with an API program and strategy in mind. This API-first approach to development will only continue to increase, driven by an increasingly interconnected web of devices, organizations, and people. To support this rapid growth, …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds