1
votes

I am creating a little ASP.NET app and have a problem with one field value.

I have defined my enum in a class:

class Column
{
    public enum Type {
    Undefined = 0,
    Integer = 1,
    ShortDate = 2,
    Etc = 3 }

    // some other stuff
}

The app contains some controls to enter properties of a column, namely a dropdownlist for choosing the column type and some unimportant others. And when all properties are properly entered, SaveButton in enabled to save the column type info into a listbox. My Default.aspx.cs contains:

private Column.Type selectedType;

protected void Page_Load(object sender, EventArgs e)
{
    // fill the ColumnTypeDropDownList (from the Column.Type enum)
    if (!IsPostBack)
    {
        foreach (Column.Type ct in Enum.GetValues(typeof(Column.Type)))
        {
            ColumnTypeDropDownList.Items.Add(new ListItem(ct.ToString()));
        }
    }
}

protected void ColumnTypeDropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
    PrepareToSave();
}

// also called from other controls events, therefore in a separate method
private void PrepareToSave()
{
    // control if all needed properties are entered and set the field
    if ((ColumnNameTextBox.Text != "") && (ColumnTypeDropDownList.SelectedValue != Column.Type.Undefined.ToString()))
    {
        foreach (Column.Type ct in Enum.GetValues(typeof(Column.Type)))
        {
            if (ct.ToString() == ColumnTypeDropDownList.SelectedValue) selectedType = ct;
        }
        SaveButton.Enabled = true;
    }
}

protected void SaveButton_Click(object sender, EventArgs e)
{
    ColumnsListBox.Items.Add(selectedType.ToString());    // always writes "Undefined"
}

The problem is that it always writes "Undefined" into the listbox, even though another type was selected from the dropdownlist. I tried to add the item into the listbox inside the PrepareToSave() method and that works correctly, but I need it outside. On the other hand, the condition controlling if any other value than Undefined is selected from the dropdownlist works well. It seems that the field selectedType has the correct selected value only inside the PrepareToSave() method.

AutoPostBack of all the controls is enabled.

Am I missing something about the enums or do you have any tips how to fix it? Thanks.

4
Wouldn't the field's value get reinitialised every time a PostBack call is made? - stuartd
Your selectedType property is not persitent. Every time you change the dropdown index a postback will be made. This means that your local variable will be created at every postback. You should set the AutoPostback=false on your dropdown and handle the enable/disable logic of your button via JS. - Martin E

4 Answers

0
votes

That's most probably because of your if condition as pointed below

if ((ColumnNameTextBox.Text != "") && (ColumnTypeDropDownList.SelectedValue != Column.Type.Undefined.ToString()))
{

Instead of ColumnNameTextBox.Text != "" use !string.IsNullOrEmpty(ColumnNameTextBox.Text)

0
votes

Just another tip:

Use GetNames instead of GetValues in your foreach loop:

foreach (var ct in Enum.GetNames(typeof(Column.Type)))
{
    //do your stuff.
}
0
votes

Your problem is in the line...

ColumnTypeDropDownList.Items.Add(new ListItem(ct.ToString()));

..namely in new ListItem(ct.ToString()). When you use this constructor of the ListItem class, you create an item with Value set to null. Later you compare against the value:

if (ct.ToString() == ColumnTypeDropDownList.SelectedValue) selectedType = ct;

Since Value of each of the items is null, ColumnTypeDropDownList.SelectedValue is also null and your comparison fails. That should be also easily figured out in a debugger.

The correct list item constructor for you is

ListItem listItem = new ListItem(ct.ToString(), ct.ToString());

As an additional issue, you have to call PrepareToSave in SaveButton_Click, since the selectedType field will have lost its value across requests. PrepareToSave will rebuild that value.

0
votes

If you want to use AutoPostBack ...

Add a hidden control to your page. In your PrepareToSave(); method you just can add the selectetType like yourControlName.Text = ct;

And change your save handler to this ....

protected void SaveButton_Click(object sender, EventArgs e)
{
    // Read the value of the hidden control
    ColumnsListBox.Items.Add(yourControlName.Text);   
}