重新排列通用列表中的数字

发布于 2024-12-13 16:42:55 字数 1299 浏览 1 评论 0原文

我不确定我的问题标题与我的问题内容是否同步。

我有一个下拉列表,数据源为列表 List 数字; 在“删除”按钮上,单击下拉列表中的数字将被删除,在“添加”按钮上,单击数字将被添加到下拉列表中。

现在,如果我的下拉列表中有 1、2、3,并且如果我删除 1,我希望数字 2 和 3 被 1 和 2 替换。我不知道该怎么做。有人可以帮我吗?

   if(!IsPostBack)
    {
     List<int> numbers = new List<int>();
            Session["data"] = numbers;
      rest of other code....
     }

   protected void btnAddDetail_Click(object sender, EventArgs e)
    {
        numbers = (List<int>)Session["data"];
        if (numbers == null) { numbers = new List<int>(); }
        if (numbers.Count!=0)
        {
            int max = numbers.Max();
            numbers.Add(max + 1);
            drpdown1.DataSource = numbers;
            drpdown1.DataBind();
            drpdown1.SelectedValue = numbers.Max().ToString();
            Session["data"] = numbers;

        }
       }
     protected void btnDelete(object sender, EventArgs e)
     {
       numbers = (List<int>)Session["data"];
        int detailIndex = numbers.FindIndex(c => c == Convert.ToInt32      (deleteNo));
        numbers.RemoveAt(detailIndex);
        drpdown1.DataSource = numbers;
        drpdown1.DataBind();
         Session["data"] = numbers;
       }

现在,当我重新绑定下拉列表时,我希望将数字 2 和 3 替换为 1 和 2。

I am not sure if the title of my question is in sync with the content of my question.

I have a dropdown list with the datasource being list List<int> numbers;
On a Delete button click the numbers from the dropdown get deleted and and on Add Button click numbers are added to the dropdown.

Now if have 1, 2, 3 in my dropdown and if I delete 1, I want the number 2 and 3 to be replaced by 1 and 2. I am not sure how to do this. Can someone please help me?

   if(!IsPostBack)
    {
     List<int> numbers = new List<int>();
            Session["data"] = numbers;
      rest of other code....
     }

   protected void btnAddDetail_Click(object sender, EventArgs e)
    {
        numbers = (List<int>)Session["data"];
        if (numbers == null) { numbers = new List<int>(); }
        if (numbers.Count!=0)
        {
            int max = numbers.Max();
            numbers.Add(max + 1);
            drpdown1.DataSource = numbers;
            drpdown1.DataBind();
            drpdown1.SelectedValue = numbers.Max().ToString();
            Session["data"] = numbers;

        }
       }
     protected void btnDelete(object sender, EventArgs e)
     {
       numbers = (List<int>)Session["data"];
        int detailIndex = numbers.FindIndex(c => c == Convert.ToInt32      (deleteNo));
        numbers.RemoveAt(detailIndex);
        drpdown1.DataSource = numbers;
        drpdown1.DataBind();
         Session["data"] = numbers;
       }

Now when I rebind the dropdown list I want the numbers 2 and 3 to be replaced by 1 and 2.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羁〃客ぐ 2024-12-20 16:42:55

如果我明白你想要做什么,请尝试使用索引数组:

将这两个控件放在 aspx 页面中:

<asp:DropDownList ID="ddlNumbers" runat="server" />
<asp:Button id="btnRemove" runat="server" />

将此代码放在你的代码隐藏中:

public ArrayList  Numbers
{
    get
    {
        return (ArrayList)this.ViewState["Numbers"];
    }
    set
    {
        this.ViewState["Numbers"] = value;
    }
}

protected override void OnInit(EventArgs e)
{
    this.btnRemove.Click += new EventHandler(btnRemove_Click);
    base.OnInit(e);
}

void btnRemove_Click(object sender, EventArgs e)
{
    this.Numbers.RemoveAt(Convert.ToInt32(this.ddlNumbers.SelectedValue));
    this.BindList();
}

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        this.Numbers = new ArrayList();
        for (int i = 0; i < 10; i++)
        {
            this.Numbers.Add(i);
        }
        this.BindList();
    }
}

private void BindList()
{
    this.ddlNumbers.Items.Clear();
    for (int i = 0; i < this.Numbers.Count; i++)
    {
        this.ddlNumbers.Items.Add((i + 1).ToString());
    }
}

If I understand what you are trying to do, try using an index array:

put these two controls in the aspx page:

<asp:DropDownList ID="ddlNumbers" runat="server" />
<asp:Button id="btnRemove" runat="server" />

put this code in your code-behind:

public ArrayList  Numbers
{
    get
    {
        return (ArrayList)this.ViewState["Numbers"];
    }
    set
    {
        this.ViewState["Numbers"] = value;
    }
}

protected override void OnInit(EventArgs e)
{
    this.btnRemove.Click += new EventHandler(btnRemove_Click);
    base.OnInit(e);
}

void btnRemove_Click(object sender, EventArgs e)
{
    this.Numbers.RemoveAt(Convert.ToInt32(this.ddlNumbers.SelectedValue));
    this.BindList();
}

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        this.Numbers = new ArrayList();
        for (int i = 0; i < 10; i++)
        {
            this.Numbers.Add(i);
        }
        this.BindList();
    }
}

private void BindList()
{
    this.ddlNumbers.Items.Clear();
    for (int i = 0; i < this.Numbers.Count; i++)
    {
        this.ddlNumbers.Items.Add((i + 1).ToString());
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文