重新排列通用列表中的数字
我不确定我的问题标题与我的问题内容是否同步。
我有一个下拉列表,数据源为列表 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我明白你想要做什么,请尝试使用索引数组:
将这两个控件放在 aspx 页面中:
将此代码放在你的代码隐藏中:
If I understand what you are trying to do, try using an index array:
put these two controls in the aspx page:
put this code in your code-behind: