简体   繁体   中英

How to Check if ListBox is Empty on Client-Side

I created a javascript confirm as below.

<script Type="Text/Javascript">

function CheckListBox(lvi)
{
    if(lvi == "")
    {
        if(confirm("Are you sure?"))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

</script>

I need to test if the ListBox.Items control is empty... I already made reference on my aspx page

<script language="javascript" type="text/javascript" src="/JS/confirm.js"></script>

I want to know how to call it on my aspx.cs page . . . So I can pass the parameter :

string oi = Listbox_Clubes.Items.Count.ToString();//Its the parameter I want to pass

For your javascript, you can get the value without the code-behind (this assumes the script code is in the same page, in order to get the client ID):

 <script>
     function ClickListBox() {
         if ($("#<%= Listbox_Clubes.ClientID  %>").val() === null) {
             if (confirm("Are you sure?")) {
                 return true;
             }
             else {
                 return false;
             }
         }
     }
 </script>

Similarly, you don't use javascript to validate on the server side. The code you posted will return all items in the ListBox. Here is one way to get the count of the number of selected items (I'm using .ToString() based on the OP code example):

string oi = Listbox_Clubes.Items.Cast<ListItem>().Where(i => i.Selected).Count().ToString();

However, there is no reason why you would get this value and pass it back to the client-side to do validation (what it sounds like you want to do in your post). Mainly because this involves a post-back, and client-side validation, by its nature, should occur before post-back. Also, you will still need to do server-side validation, even when you have client-side validation.

Related: in the code-behind, you can test to see if anything is selected by:

bool hasValue = Listbox_Clubes.SelectedItem != null;

The .SelectedItem returns the selected item with the lowest index in the list control. When nothing is selected, this value is null ... so you know if the value isn't null , then at least one item was selected.

If you want to require that they choose at least one item, you can use a RequireFieldValidator and let that handle both validations. If you haven't done much with ASP.NET validators, that would be one good thing to read up on.

It sounds like you probably should read more about client-side validation and server-side validation and how to use them... because it seems like you are mixing them up.

The count code is a modified version of code in ASP:ListBox Get Selected Items - One Liner?

See this link for how to execute javascript from code behind

protected void Page_Load(object sender, EventArgs e)
{
     ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "CheckListBox(" + Listbox_Clubes.Items.Count.ToString() + ");", false);
}

Note: you must add a ScriptManager control in aspx page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM