【发布时间】:2013-02-27 05:54:55
【问题描述】:
我已经写了这个分割字符串的代码
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
string oldstr = DropDownList2.SelectedItem.Value;
string[] exp = System.Text.RegularExpressions.Regex.Split(oldstr, "-");
int int1 = Convert.ToInt32(exp[0]);
int int2 = Convert.ToInt32(exp[1]);
}
它给了我例外
“索引超出了数组的范围。”
在线int int2 = Convert.ToInt32(exp[1]);
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True"
onselectedindexchanged="DropDownList2_SelectedIndexChanged">
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="1-2">1-2 years</asp:ListItem>
<asp:ListItem Value="3-4 ">3-4 years</asp:ListItem>
<asp:ListItem Value="5-7">5-7 years</asp:ListItem>
</asp:DropDownList>
【问题讨论】:
-
你调试了吗?将断点放在那里时,您可以在同一行的“exp”中看到什么值?
-
这意味着
exp没有第二个元素。检查exp.Length -
那么一个空的
在你的下拉列表中做了什么?