locked
How to run a code only the first time a selected index value has changed RRS feed

  • Question

  • User1277740678 posted

    HI all,

    I've looked everywhere but can't find an answer to this. 

    I have a webform with a dropdownlist. 

    The first time the dropdown list selected item is changed, I want to record the time that its changed. 

    If its changed again then I don't want that time to be overwritten. 

    When the user presses the submit button, the event number is reset, so the next time they change the selected item, the time is recorded again. 

    I've tried

    protected void productCategoryDDL_SelectedIndexChanged(object sender, EventArgs e)
    {
    if (productCategoryDDL_SelectedIndexChanged.Events==1)

    { string stime = DateTime.Now.ToString();
    timeLabel.Text = stime; }

    Obviously "productCategoryDDL_SelectedIndexChanged.Events==1" is wrong, but does anyone know the right term for it? 

    Thanks in advance

    Monday, July 3, 2017 12:13 AM

All replies

  • User-1838255255 posted

    Hi Kvetch,

    According to your description and needs, as far as I know, you could add a global variable, then add one to it when you execute the dropdownlist change event, use if statement to judge the variable, then execute the corresponding code, here is sample code, please check:

    Sample Code:

    static int a = 0;
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
            protected void productCategoryDDL_SelectedIndexChanged(object sender, EventArgs e)
            {
                a = a + 1;
                if (a == 1)
                {
                    string stime = DateTime.Now.ToString();
                    timeLabel.Text = stime;
                }
                else
                {
                    Response.Write("This is the second time postback!");
                }
            }

    Best Regards,

    Eric Du

    Monday, July 3, 2017 2:26 AM
  • User-1440941333 posted

    Are you assigning the data source to the webDropDown on page load.?

    Monday, July 3, 2017 6:33 AM
  • User1277740678 posted

    Yeah, but only when it first loads. so:

    if (!IsPostBack)

    {databind to datasource}

    Tuesday, July 4, 2017 11:00 AM
  • User-158764254 posted

    Eric Du

    static int a = 0;

    wow - no - don't do this.

    static variables are shared by all users of the application.

    so user 1 hits the site and variable "a" gets set. Then user 2 hits the site and sees the very same value of "a" already set by user 1...

    static variables are very very rarely going to be used in an asp.net app given that they are shared by all users.

    You need to use a variable that is specific to the individual user accessing your page and that persists across postbacks.

    options are:

    hidden field, viewstate, session, cookie

    Tuesday, July 4, 2017 1:07 PM
  • User1277740678 posted

    Thanks for the heads up-would have been pretty disastrous once we get more than one user. 

    This is what I've done instead. 

    if (!IsPostBack)
    {

    int a = 0;
    Session["a"] = a;
    }

    protected void productCategoryDDL_SelectedIndexChanged(object sender, EventArgs e)
    {

    int a = (int)(Session["a"]);

    a = a + 1;
    Session["a"] = a;
    string test = a.ToString();
    testalbl.Text = test;
    if (a == 1)
    {
    blahblah
    }

    protected void submitButton_Click(object sender, EventArgs e)
    {

    blahblah

    int a = 0;
    Session["a"] = a;
    //a = 0;
    }

    Also are public strings  and public ints also shared across all users? If so then I'll have to change some more code. 

    Wednesday, July 5, 2017 10:02 AM
  • User-158764254 posted

    are public strings  and public ints also shared across all users

    public is fine and those variables are not shared between users.

    Wednesday, July 5, 2017 10:18 PM