Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

static bool is always true

Tags:

c#

asp.net

I have a web form application that looks like such.

public partial class MyPage : Page
{
    private static bool _changed = false;

    protected void btnSave_Click(object sender, EventArgs e)
    {
         if(_changed)
         {
             //some code here
         }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/MyPage.aspx");
    }

    protected void dropdownlist_SelectedIndexChanged(object sender, EventArgs e)
    {
        _changed = true;  
    }
}

So the gist of what I'm trying to do is that I have a form, which contains a drop down list and a save button. If the user makes a change to the drop down list, I set the private boolean changed to be true. By default, it is false.

For some reason that I do not understand, changed is true when it gets to the btnSave_click method even though it never visited the dropdownlist_selectedIndexChanged method, which is the only place in my code that sets changed to be true.

The markup:

<asp:DropDownList ID="myDropDown" runat=server" OnSelectedIndexChanged="dropdownlist_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Value="True">Yes</asp:ListItem>
<asp:ListItem Value="False">No</asp:ListItem>

<asp:Button ID="btnSave" Text="Save" OnClick="btnSave_Click" runat="server" />
like image 520
Rhs Avatar asked Jun 04 '26 11:06

Rhs


2 Answers

Are you really sure that you want to use a static field variable which is shared across all requests? So when UserA selects something in the DropDown, UserB will also have _changed = true.

You might want to use a ViewState or Session variable instead, e.g.:

private bool HasChanged
{
    get { object b = ViewState["HasChanged"] ?? false; return (bool)b; }
    set { ViewState["HasChanged"] = value; }
}

Nine Options for Managing Persistent User State in Your ASP.NET Application

like image 172
Tim Schmelter Avatar answered Jun 07 '26 10:06

Tim Schmelter


I think the SelectedIndexChanged gets triggered by the <select> element getting populated with it's initial values. This means that while your form is initializing, before the user can even interact with it, your dropdownlist_SelectedIndexChanged() method is being invoked. You can verify this by using a debugger to see when this method is being executed.

Also, as others have mentioned, a static member is nto a good idea to store request variables; ViewState is better suited for this.

UPDATE

I think the SelectedIndexChanged gets triggered by the <select> element getting populated with it's initial values.

This is not true.

I tested this out with a very simple web-app and the SelectedIndexChanged event does not get fired when initial values are being added to the DropDownList through the declarative file (MyPage.aspx).

I even tried to change the SelectedIndex programmatically in the code behind.

protected void Page_Load(object sender, EventArgs e)
{
    myDropDownList.Items.Add(new ListItem("Text", "Value"));
    myDropDownList.SelectedIndex = 2;
}

Still, even when doing this, the myDropDownList_SelectedIndexChanged event does not get fired until changing the selected item through interacting with the webpage.

like image 41
Jesse Webb Avatar answered Jun 07 '26 10:06

Jesse Webb