C# 如何为下拉选择项c赋值

C# 如何为下拉选择项c赋值,c#,asp.net,C#,Asp.net,例如,我需要为aspx中的“选定值”下拉列表指定值 下拉列表项目 <asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True"> <asp:ListItem>1</asp:ListItem> <asp:ListItem>2&l

例如,我需要为aspx中的“选定值”下拉列表指定值

下拉列表项目

<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>

它可以工作,但问题是如果用户首先在下拉列表中选择1//i=2,然后用户选择b//i=4,如果用户再次选择1//i=6。如果用户在特定下拉列表中选择任何值,则该值不应增加。怎么做。任何想法……

您需要一个像ViewState或Session这样的临时存储来保存您的值并从 在那里

并在代码中使用它,如下所示

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());
}
您使用的是静态变量,因此i值将在回发之间保留,并且对所有用户都是通用的,这是不正确的

您需要将其存储在ViewState、HiddenField或Session中,以便在回发之间保持该值,并使每个用户的值保持不同

下面是我使用ViewState应该做的:


上面的答案中很少有关于在回发后重置静态变量的,这是不正确的,静态变量在应用程序域期间保持其值。在重新启动web服务器之前,它将在许多浏览器会话中存活

也就是说,使用静态变量而不是使用Session或Viewstate建议的方法绝对不是一个好主意

关于您的问题,我想您只想在第一次从下拉列表中选择一个值时增加该值,要实现这一点,您需要有一个标志,让您知道该值是否已被选择,如下所示:

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;
        }
    }

但静态字段对于所有登录到您的应用程序的用户来说都是公用的。您希望它只是第一次递增吗?如果用户选择任何值或多次,我应该递增once@DimitarDimitrov我正在更新我的答案。如果用户在dropdownlist1中选择两次,它的值将递增。如果用户选择更多次,我需要我应该递增一次在dropdownlist1hanks中…如果我还需要dropdownlist2。我应该创建新的视图状态[DropDown2Counter]???我刚刚再次编辑了我的答案,使用bool变量检查dropdownlist1是否已被选中更有效。对于dropdownlist2,可以使用相同的方法
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());
}
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);
}
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;
        }
    }