Wednesday, February 1, 2012

creating dynamic dropdownlist and applying cssclass in asp net C#

Write the following code in the page_Load or page_PreInit or Button_Click events to create dynamic dropdownlist.


    DropDownList ddl = new DropDownList();
    ddl.ID = "ddlOne";
    ddl.Items.Add(new ListItem("Select", ""));
    ddl.Items.Add(new ListItem("One", "1"));
    ddl.Items.Add(new ListItem("Two", "2"));
    ddl.Items.Add(new ListItem("Three", "3"));
    ddl.Items.Add(new ListItem("Four", "4"));
    ddl.Items.Add(new ListItem("Five", "5"));


        this.form1.Controls.Add(ddl);
// or
    this.pnlDropDownList.Controls.Add(ddl);

still now we created only dynamic dropdownlist and now applying cssClass to the dropdown as follows
write another line as shown below...

       
        ddl.cssClass = ".wrapper"; // Applying style to DropdownList
        this.form1.Controls.Add(ddl);
// or
    this.pnlDropDownList.Controls.Add(ddl);



This is a styleSheet class

.wrapper
{
    background:#f7f6f6;
    width:100%;
    padding:0px;
    margin:0px;
   
}

No comments:

Post a Comment