日期:2014-05-20  浏览次数:20479 次

button 验证 ListBox1 框中数据是否为空,我要求用 javascript 实现,因为无刷新!请帮我改一下这个小例子!谢谢!


一个按钮:

<asp:button     id= "RemvFile "   runat= "server "   CausesValidation= "False "
Text= "点选下面附件框中某一附件删除一个附件 "> </asp:button>

一个   ListBox1   控件:

<asp:listbox   id= "ListBox1 "   runat= "server "> </asp:listbox>


当我点击   RemvFile   这个按钮时,如何判断   ListBox1   控件中数据为空,然后,提醒用户“附件框中数据为空”,

然后,当   ListBox1   中有数据时,但是用户没有选中其中的任一项,当我点击   RemvFile   这个按钮时,要提示用户:请选中一个项目,

在   .cs   中我是这样判断的:

if(ListBox1.Items.Count   !=   0)
{
            if(ListBox1.SelectedIndex> =0)
                  {    
                        hif.RemoveAt(ListBox1.SelectedIndex);
                        ListBox1.Items.Remove(ListBox1.SelectedItem.Text);
                  }
            else
                  {
                        //提示用户要选中一个项目
                    }
}
else
{
      //提示用户附件框中无数据
}

可是这样的话有刷新,我想实现用   javascript   弹出警告窗口来实现,该如何实现呢?


------解决方案--------------------
if( document.getElementById( "ListBox1 ").length == 0)
------解决方案--------------------
selectedIndex == -1
------解决方案--------------------
if( document.getElementById( "ListBox1 ").selectedIndex == -1)

------解决方案--------------------
楼主的问题没有表述清楚
难为楼上的几位了

你可以在 RemvFile 这个button的属性里写上 onClientClick = "return checkListBoxStatus() "

===========
<asp:button id= "RemvFile " runat= "server " CausesValidation= "False "
Text= "点选下面附件框中某一附件删除一个附件 " onClientClick = "return checkListBoxStatus()> </asp:button>
===========

页面的head里写
<Script>
function checkListBoxStatus()
{
if( document.getElementById( "ListBox1 ").selectedIndex == -1)
{
alert(‘没有选中项’);
return false;
}

</Script>

这样可以实现你的全部功能