Sample Code: How to display multiple spaces in a dropdownlist

webform1.aspx

<asp:DropDownList id="DropDownList1" runat="server"></asp:DropDownList>

VB.NET Code

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 ’Put user code to initialize the page here
 Dim i As Integer
 For i = 0 To 10
  DropDownList1.Items.Add("Item Number" + SpaceDDL(10) + i.ToString)
 Next
End Sub
 
Private Function SpaceDDL(ByVal numberOfSpaces As Integer) As String
   Dim Spaces As String
 Dim i As Integer
 For i = 0 To numberOfSpaces
             Spaces &= "&nbsp;"
 Next
 Return Server.HtmlDecode(Spaces)
End Function

C# Code

private void Page_Load(object sender, System.EventArgs e)
{
 // Put user code to initialize the page here
   for(int i = 0 ;i<=10;i++)
 {
  DropDownList1.Items.Add("Item Number" + SpaceDDL(10) + i.ToString());
 }
}

string SpaceDDL(int numberOfSpaces ) 
{
 string Spaces="";
 for(int i = 0 ;i<=numberOfSpaces;i++)
 {
  Spaces += "&nbsp;";
 }
 return Server.HtmlDecode(Spaces);
}

No Comments