Wednesday, January 20, 2010

How can i set a limit for the number of selections in an asp.net listbox control?

sure. you can do that.





You will have to use JavaScript along with ASP.NET.





I will try to explain this because i have done this but with a validation situation but never done with the select limit.





I will assume that you are a pro in web development and JavaScript.





OK, let's start. Server Controls have an property called ';Attributes';. There Attributes are the ones placed at run time when the control renders to the browser (i.e.; onClick, onChange ..etc.). The trick here is to attach a JavaScript function to monitor the ListBox selection when the user select items from it. Here is the ASP.NET code:





C#:


---


private void Page_Load(object sender, System.EventArgs e) {





myListBox..Attributes.Add(';onChange';, ';return CheckItmes();';);





}


this code instructs the IIS to add an ';onChange'; event to the ListBox that runs at the client's side and calls the JavaScript CheckItems() function.





Inside the HTML page, you will write the JavaScript function body that handles the onChange for the myListBox control.





It's easy, right? try it.





If you have any question or help contact me:


yasserhy@yahoo.com
  • eye make up
  • No comments:

    Post a Comment