Change ListBox Width Dynamically in ASP.NET

September 30, 2013 0 Comments

Every control have a width property . In width property you can assign any unit value such as 10, 20 and any other number.If you want to change width of the control at runtime then you must assign width to the ListBox at runtime such as

ListBox1.Width = 20;

If you want to change control width according to listbox item then you must to change Item value to integer.
Lets take an simple example.

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<script runat="server">

    protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
        ListBox1.Width =int.Parse(ListBox1.SelectedValue);

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
        <asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True"
            <asp:ListItem Value="100">Increase 100 pixel</asp:ListItem>
            <asp:ListItem Value="200">Increase 200 pixel</asp:ListItem>

Change ListBox Width Dynamically in ASP.NET

Change ListBox Width Dynamically in ASP.NET

Jacob Lefore

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard. Google