1

i need to assign value for selected value drop down in aspx for example

dropdownlist items

<asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
    AutoPostBack="True">
    <asp:ListItem>1</asp:ListItem>
    <asp:ListItem>2</asp:ListItem>
    <asp:ListItem>3</asp:ListItem>
</asp:DropDownList>

<asp:DropDownList ID="DropDownList2" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
    AutoPostBack="True">
    <asp:ListItem>a</asp:ListItem>
    <asp:ListItem>b</asp:ListItem>
    <asp:ListItem>c</asp:ListItem>
</asp:DropDownList>

if user select any item in dropdownlist1 it should increment value 2 then if user select any item in dropdownlist2 it should increment value 2

i need to display total

i tried this code

static int i = 0;
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    i += 2;
    Label1.Text = "hello"+i;
}

protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
    i += 2;
    Label1.Text = "hello"+i;
}

its working but problem is if user first select 1 in dropdown //i=2 then user select b //i=4 if user again select 1 //i=6. it should not increment if user select any value in particular drop down list. how to do it. any idea....

Happy
  • 323
  • 1
  • 3
  • 12

3 Answers3

2

You're using a static variable so the i value will be kept between postbacks and will be common to all users, this is incorrect.

You need to store it in ViewState, HiddenField, or Session in order to keep the value between postbacks and also keep the value different for each user.

Here's what I would've done using ViewState:

private int Counter
{
   get
   {
      if (ViewState["Counter"] == null)
      {
         return 0;
      }
      else
      {
         return (int)ViewState["Counter"];
      }
   }
   set
   {
      ViewState["Counter"] = value;
   }
}

private bool DropDown1Selected
{
   get
   {
      if (ViewState["DropDown1Selected"] == null)
      {
         return false;
      }
      else
      {
         return (bool)ViewState["DropDown1Selected"];
      }
   }
   set
   {
      ViewState["DropDown1Selected"] = value;
   }
}


protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (!this.DropDown1Selected)
    {
        this.DropDown1Selected = true;
        this.Counter += 2;
    }
    Label1.Text = string.Format("hello{0}", this.Counter);
}

protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
    this.Counter += 2;
    Label1.Text = string.Format("hello{0}", this.Counter);
}
ekad
  • 14,436
  • 26
  • 44
  • 46
  • its incrementing values if user select twice in dropdownlist1.i need i should increament once if user select more times in dropdownlist1 – Happy Jan 01 '14 at 08:29
  • thanks...if i need for dropdownlist2 also.i should create new ViewState["DropDown2Counter"] ????? – Happy Jan 01 '14 at 08:44
  • I just edited my answer again, it's more efficient using bool variable to check if dropdownlist1 has been selected or not. You can use the same way for dropdownlist2 – ekad Jan 01 '14 at 08:52
1

Few of the answers above are talking about static variable getting reset after post back, this is incorrect, Static variables keep their values for the duration of the application domain. It will survive many browser sessions until you restart the web server Asp.net Static Variable Life time Across Refresh and PostBack

That being said, it is definitely not a good idea to use Static variables and instead go with the approaches suggested using Session or Viewstate.

About your question, I guess you want to increment the value only first time a value is chosen from the drop down list, to achieve this you would want to have a flag to let you know if the value is already selected, something on the below lines:

static bool DrpDown1;
    static bool DrpDown2;
    static int i = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DrpDown1 = false;
            DrpDown2 = false;
        }
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (!DrpDown1)
        {
            i += 2;
            Label1.Text = "hello" + i;
            DrpDown1 = true;
        }
    }

    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (!DrpDown2)
        {
            i += 2;
            Label1.Text = "hello" + i;
            DrpDown2 = true;
        }
    }
Community
  • 1
  • 1
Vasan
  • 375
  • 1
  • 12
0

You need a temporary store like ViewState or Session to keep you values and get it back from there.

private int GetValue()
{
     return Int32.Parse(ViewState["temp"]);
}
private void SetValue(int i)
{      
   if(ViewState["temp"]==null)
   {
      ViewState["temp"]=i; 
   }
   else
   {
       ViewState["temp"]= i+Int32.Parse(ViewState["temp"]);
   }
}

and use it in your code as follows

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
  SetValue(2);
   Label1.Text = string.Format("hello{0}", GetValue());
}

protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
    SetValue(2);
    Label1.Text = string.Format("hello{0}", GetValue());
}
शेखर
  • 17,412
  • 13
  • 61
  • 117