我的dropdownlist里面的item是
<asp:ListItem>王,3</asp:ListItem>
<asp:ListItem>李,5</asp:ListItem>
<asp:ListItem>果,8</asp:ListItem>旁边有个按钮,按钮的功能是:
每点击一下按钮,dropdownlist的SelectedValue里面的数据-1,知道减到了0,当减到0的时候,再点击按钮,就删除这个item
现在的灵异事件是:
<asp:ListItem>王,3</asp:ListItem>
可以很顺利的按我的要求走下去但是当把<asp:ListItem>王,3</asp:ListItem>这个选项删除以后
SelectedValue变成了
<asp:ListItem>李,5</asp:ListItem>但是,不管怎么点按钮 李,5 变成 李,4以后,就不再变了我debug一下看了看
变成 李,4 以后,再点的时候,
取得的值应该是 李,4 但是,取到的却是 李,5请大家帮帮忙呀~~很着急,在线等
<asp:ListItem>王,3</asp:ListItem>
<asp:ListItem>李,5</asp:ListItem>
<asp:ListItem>果,8</asp:ListItem>旁边有个按钮,按钮的功能是:
每点击一下按钮,dropdownlist的SelectedValue里面的数据-1,知道减到了0,当减到0的时候,再点击按钮,就删除这个item
现在的灵异事件是:
<asp:ListItem>王,3</asp:ListItem>
可以很顺利的按我的要求走下去但是当把<asp:ListItem>王,3</asp:ListItem>这个选项删除以后
SelectedValue变成了
<asp:ListItem>李,5</asp:ListItem>但是,不管怎么点按钮 李,5 变成 李,4以后,就不再变了我debug一下看了看
变成 李,4 以后,再点的时候,
取得的值应该是 李,4 但是,取到的却是 李,5请大家帮帮忙呀~~很着急,在线等
protected void Button1_Click(object sender, EventArgs e)
{
string a = DropDownList1.SelectedValue.Substring(0, 1).TrimEnd(); if (DropDownList1.Items.Count != 0)
{
if (DropDownList1.SelectedValue.Substring(2, 1).TrimEnd() == "0")
{
DropDownList1.Items.Remove(a + ",0");
}
else
{
aa = DropDownList1.SelectedValue.Substring(2, 1).TrimEnd();
aaint = Int32.Parse(aa) - 1;
DropDownList1.SelectedItem.Text = a + "," + aaint.ToString().TrimEnd();
}
}
}
你没仔细看,这个看debug看不出来为什么
ListItem li1 = new ListItem("王,3");
ListItem li2 = new ListItem("李,5");
ListItem li3 = new ListItem("果,8");
DropDownList1.Items.Add(li1);
DropDownList1.Items.Add(li2);
DropDownList1.Items.Add(li3);
}
那么也是出现你的情况,不是Remove的原因了,我想还应改是Value和Text的原因。
再增加
DropDownList1.SelectedItem.Value = aaint
{ string a = DropDownList1.SelectedValue.Substring(0, 1).TrimEnd(); if (DropDownList1.Items.Count != 0)
{
if (DropDownList1.SelectedValue.Substring(2, 1).TrimEnd() == "0")
{
DropDownList1.Items.Remove(a + ",0"); }
else
{
aa = DropDownList1.SelectedValue.Substring(2, 1).TrimEnd();
aaint = Int32.Parse(aa) - 1;
DropDownList1.SelectedItem.Text = a + "," + aaint.ToString().TrimEnd();
DropDownList1.SelectedItem.Value = a + "," + aaint.ToString().TrimEnd();
}
}
}
果然把SelectedItem.Value 赋值就可以了!
谢谢大家!!