How to dynamically create Sharepoint ComboBoxes?

128 views Asked by At

I dynamically create Textboxes with C# in my Sharepoint project like so:

boxRequestDate = new TextBox
{
    CssClass = "dplatypus-webform-field-input",
    Text = DateTime.Today.ToShortDateString()
};

...but I need ComboBoxes (or their HTML equivalent), too, at times. How is that done? "ComboBox" is not recognized in Sharepoint. Surely there's a way to create a TextBox (input element of type text) that "acts like" a combo box.

Specifically, I need to create controls that act as the elements (no pun intended) of a date, namely a "Month" combo box containing the vals January...December, a "Days of Month" control containing vals from 1..[28...31], and a "Year" combobox with say, 2000 to 2016.

How is this done, programmatically, in C#?

UPDATE

It may turn out that this, or something like it, will work:

boxReturnDateMonth = new TextBox
{
    CssClass = "dplatypus-webform-field-input",
    TextMode = TextBoxMode.MultiLine,
    Rows = 12
};
boxReturnDateMonth.Text.Insert(0, "Jan");
boxReturnDateMonth.Text.Insert(1, "Feb");
boxReturnDateMonth.Text.Insert(2, "Mar");
boxReturnDateMonth.Text.Insert(3, "Apr");
boxReturnDateMonth.Text.Insert(4, "May");
boxReturnDateMonth.Text.Insert(5, "Jun");
boxReturnDateMonth.Text.Insert(6, "Jul");
boxReturnDateMonth.Text.Insert(7, "Aug");
boxReturnDateMonth.Text.Insert(8, "Sep");
boxReturnDateMonth.Text.Insert(9, "Oct");
boxReturnDateMonth.Text.Insert(10, "Nov");
boxReturnDateMonth.Text.Insert(11, "Dec");

IOW, use a Textbox, but sets its TextMode to Multiline, assing a count of rows (entries), and then add those entries.

2

There are 2 answers

1
Michael Rudner Evanchik On BEST ANSWER
DropDownList ddlReturnDateMonth = new DropDownList();
ddlReturnDateMonth.CssClass = "dplatypus-webform-field-input";
ddlReturnDateMonth.Items.Add(new ListItem("Jan", "1"));
2
B. Clay Shannon-B. Crow Raven On

This seems to be the way to do it:

DropDownList ddlReturnDateMonth = new DropDownList();
ddlReturnDateMonth.Items.Add(new System.Web.UI.WebControls.ListItem("Jan", "1")); 
ddlReturnDateMonth.Items.Add(new System.Web.UI.WebControls.ListItem("Feb", "2"));
ddlReturnDateMonth.Items.Add(new System.Web.UI.WebControls.ListItem("Mar", "3"));
ddlReturnDateMonth.Items.Add(new System.Web.UI.WebControls.ListItem("Apr", "4"));
ddlReturnDateMonth.Items.Add(new System.Web.UI.WebControls.ListItem("May", "5"));
ddlReturnDateMonth.Items.Add(new System.Web.UI.WebControls.ListItem("Jun", "6"));
ddlReturnDateMonth.Items.Add(new System.Web.UI.WebControls.ListItem("Jul", "7"));
ddlReturnDateMonth.Items.Add(new System.Web.UI.WebControls.ListItem("Aug", "8"));
ddlReturnDateMonth.Items.Add(new System.Web.UI.WebControls.ListItem("Sep", "9"));
ddlReturnDateMonth.Items.Add(new System.Web.UI.WebControls.ListItem("Oct", "10"));
ddlReturnDateMonth.Items.Add(new System.Web.UI.WebControls.ListItem("Nov", "11"));
ddlReturnDateMonth.Items.Add(new System.Web.UI.WebControls.ListItem("Dec", "12"));