How to add a RequiredFieldValidator to DropDownList control?

前端 未结 4 840
青春惊慌失措
青春惊慌失措 2020-11-29 01:51

I have a DropDownList binded with aSqlDataSource to display the values from the database.

I am unable to validate using a RequiredFie

4条回答
  •  余生分开走
    2020-11-29 02:15

    If you are using a data source, here's another way to do it without code behind.

    Note the following key points:

    • The ListItem of Value="0" is on the source page, not added in code
    • The ListItem in the source will be overwritten if you don't include AppendDataBoundItems="true" in the DropDownList
    • InitialValue="0" tells the validator that this is the value that should fire that validator (as pointed out in other answers)

    Example:

    
        
    
    
    
    

提交回复
热议问题