日期:2014-05-17  浏览次数:20457 次

有关于CheckBoxList的问题
在一个页面中,有两个或两个以上的CheckBoxList需要AutoPostBack各自的onselectedindexchanged方法,应该怎么弄才是正确的呢?
例如:在如图页面中放置两个CheckBoxList,然后各自的AutoPostBack='true',每个CheckBoxList有各自的onselectedindexchanged方法,若两个CheckBoxList控件同时AutoPostBack时,只有前一个有效,后一个根本没反应,有时会选不中。

------解决方案--------------------
不会吧,你是不是设置了2个CheckBoxList关联了呢?

贴出关键代码来看看。
------解决方案--------------------
1.图片挂了
2.楼主自己调试一下,看看有什么错误码?
------解决方案--------------------
代码没问题啊。
可以测试下
HTML code
<%@ Page Language="C#" EnableViewState="false" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">


  protected void Page_Load(object sender, EventArgs e)
  {
    if (!Page.IsPostBack)
    {

    }
  }

  protected void Fx_hjys_SelectedIndexChanged(object sender, EventArgs e)
  {
    string _hjys = null;
    string temp = "0";

    for (int i = 0; i < this.Fx_hjys.Items.Count; i++)
    {
      if (this.Fx_hjys.Items[i].Selected)
      {
        _hjys += "【" + this.Fx_hjys.Items[i].Text + "】";
        temp = Convert.ToString(Convert.ToInt32(temp) + Convert.ToInt32(this.Fx_hjys.Items[i].Value));
      }
    }
    Response.Write(_hjys);
  }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title></title>
</head>
<body>
  <form id="form1" runat="server">
  <asp:CheckBoxList ID="Fx_hjys" runat="server" RepeatDirection="Vertical" RepeatColumns="4"
    AutoPostBack="true" OnSelectedIndexChanged="Fx_hjys_SelectedIndexChanged">
    <asp:ListItem Value="5">暴雨</asp:ListItem>
    <asp:ListItem Value="5">冰雪</asp:ListItem>
    <asp:ListItem Value="5">大风</asp:ListItem>
    <asp:ListItem Value="5">泥石流</asp:ListItem>
    <asp:ListItem Value="5">地震</asp:ListItem>
    <asp:ListItem Value="5">高温</asp:ListItem>
  </asp:CheckBoxList>
  </form>
</body>
</html>

------解决方案--------------------
你的代码

protected void Fx_sbys_SelectedIndexChanged(object sender, EventArgs e)
{
string _sbys = "";
string temp1 = "0";
Response.Write("xx");
for (int i = 0; i < this.Fx_sbys.Items.Count; i++)
{
if (this.Fx_sbys.Items[i].Selected)
{
_sbys += "【" + this.Fx_sbys.Items[i].Text + "】";
temp1 = Convert.ToString(Convert.ToInt32(temp1) + Convert.ToInt32(this.Fx_sbys.Items[i].Value));
}
}
this.fx_sbysmc.Text = _sbys;
this.Fx_sbysz.Text = temp1;
}

另外,Value不能是重复的内容,如
<tr>
<td class="rtd" colspan="4">
<asp:CheckBoxList ID="Fx_hjys" runat="server" RepeatDirection="Vertical" RepeatColumns="4"
OnSelectedIndexChanged="Fx_hjys_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Value="1">暴雨</asp:ListItem>
<asp:ListItem Value="2">冰雪</asp:ListItem>
<asp:ListItem Value="3">大风</asp:ListItem>
<asp:ListItem Value="4">泥石流</asp:ListItem>
<asp:ListItem Value="5">地震</asp:ListItem>
<asp:ListItem Value="6">高温</asp:ListItem>
</asp:CheckBoxList>
</td>