Dropdownlist控件出错

时间:2013-12-05 06:51:26

标签: c# asp.net drop-down-menu

shownotice.aspx页面中有一个Repeater控件,用于显示从Database(sql server 2008)加载的通知和用于过滤的Dropdownlist控件:all notices(默认值= 0)/lecturer's notices( value = 1)/ student notices(value = 2)。

<asp:DropDownList ID="DropDownList" runat="server" OnSelectedIndexChanged="DropDownlist_Changed"
            AutoPostBack="true" >
                <asp:ListItem Selected="True" Value="0"> All notice </asp:ListItem>
                <asp:ListItem Value="1"> Lecturer </asp:ListItem>
                  <asp:ListItem Value="2"> Student </asp:ListItem>
            </asp:DropDownList>

NOTICE数据库中的表

 NOTICE(id, title, content,object)

object = 1 - &gt;讲师通知

object = 2 - &gt;学生通知。

请查看下面的代码:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        bool check = BindRepeater(madoituong, maloai);
        if (check == false)
            LabelError.Text = "There is no notice.";
    }
}

private bool BindRepeater()
{
    string sql = "";
    string key = DropDownList.SelectedValue;
    if (key == "0")
        sql = "select * from NOTICE";
    else
        sql = "select * from NOTICE where object=" + key;

    DataTable tb = EXECUTEQUERYSQL(sql);
    if (tb.Rows.Count > 0)
       {

        Repeater1.DataSource = tb;
        Repeater1.DataBind();
        return true;

         }
    else
        return false;
}

public void DropDownlist_Changed(Object sender, EventArgs e)
{
    bool check = BindRepeater();
    if (check == false)
        LabelError.Text = "There is no notice.";
}

我想要的只是:

    客户端访问shownotice页面时
  • - &gt; key = 0 - &gt; Repeater加载NOTICE表中的所有通知(包括讲师和学生)

  • 然后客户选择LecturerStudent过滤并按对象显示通知。

当我构建这个页面时,如果它有一些讲师(对象= 1)的注意事项和数据库中学生(对象= 2)的一些注意事项,该程序工作正常。

但是当它只有讲师的通知或只有学生的通知时 - &gt;程序错误。

例如:

NOTICE表中只有一条记录:

id = 1,title = aaa,content = holiday,object = 1

当我选择Lecturer或Student时 - &gt;它始终显示通知aaa,当我选择Student选项时,它应该显示“没有通知”的消息。

尝试调试:

当我选择Student选项:Datatable tb = null并返回false时,程序跳转到if语句并点击行:LabelError =“没有通知。”;但是在完成调试之后,Repeater仍会显示通知aaa而不是消息。

帮助!!!我真的不知道为什么当我调试时,程序命中行LabelError =“没有通知。”;但是,没有出现LabelError消息。帮助!

1 个答案:

答案 0 :(得分:1)

  DataTable tb = EXECUTEQUERYSQL(sql);
        Repeater1.DataSource = tb;
        Repeater1.DataBind();
        return true;

    if (tb.Rows.Count > 0)
       {
           return true;       
       }
    else
        return false;

只有在Repeater&gt; 0时才会看到您绑定RowCount,所以当您的行数为0时,您的转发器不会更新并显示  之前的值,您必须再次绑定Repeater以更新其值,但如果表中没有值且RowCount为零,则if条件不会让Repeater再次绑定。