Hi...
M working with ListBox Control in aspx.cs.
Can AnyOne help me out regarding....How to ensure that atleast one item gets selected in a listbox before button click event gets fired when button is clicked???
Thanks in Advance.....

Recommended Answers

All 7 Replies

Use RequiredFieldValidator.

Can u tell me which properties to be set for required field validator so as to accomplish the thing.....

ControlToValidate property,

<asp:ListBox ID="ListBox1" runat="server"></asp:ListBox> 
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
            ErrorMessage="(required)" ControlToValidate="ListBox1"></asp:RequiredFieldValidator>

Thanks for u Support...
But my problem is....
i hav 2 listboxes...
1st got populated with some items....
when i click button1,selected items in listbox1 got populated in to
listbox2...&& finally if i click button2,the selected items in listbox2 get in touch with datagrid logic......
here i m struck ensuring atleast one item shuld get selected in listbox2....if not,whenever i click button2(with out selected items) m gettin error....
&&
i already used required field validator for listbox2....but its gettin fired when i clicking button1...

hi..

If ListBox1.SelectedIndex < 0 Then            
      Exit Sub
 End If

yeah.....but mine is c#......
so do u mean that i need to exit from the button click event???
How??/

hi..

If ListBox1.SelectedIndex < 0 Then            
      Exit Sub
 End If

yeah.....but mine is c#......
so do u mean that i need to exit from the button click event???
How??/

If you don't use "exit sub", it will continue until "End Sub"
but, at least, you can give a message to the user

If ListBox1.SelectedIndex < 0 Then   
      LbMsg.Text = "No Record is selected."         
      Exit Sub
 End If
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.