我不确定我的问题标题是否与我的问题内容同步。
我有一个下拉列表,数据源是列表List<int>
个数字;
在“删除”按钮上,单击下拉列表中的数字将被删除,并且“添加按钮”单击数字将添加到下拉列表中。
现在如果在我的下拉列表中有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。
答案 0 :(得分:0)
如果我理解你要做什么,请尝试使用索引数组:
将这两个控件放在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());
}
}