我的网页上有以下代码。
table>
<tr>
<td class="style2"> <asp:ValidationSummary
ID="ValidationSummary1"
runat="server"
HeaderText="Following error occurs....."
ShowMessageBox="false"
DisplayMode="BulletList"
ShowSummary="true"
BackColor="Snow"
Width="200"
ForeColor="Red"
Font-Size="X-Large"
Font-Italic="true" EnableClientScript="true" />
</td>
</tr>
<tr>
<td valign="top" class="style2">
Test1 :
<asp:DropDownList ID="ddlTest1" runat="server">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="ReqTest1" runat="server" ControlToValidate="ddlTest1" InitialValue="Please Select" ErrorMessage="Please Select Test1"></asp:RequiredFieldValidator>
</td>
<td valign="top">
<asp:DropDownList ID="ddlLocation" runat="server">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="ReqLocation" runat="server" ControlToValidate="ddlLocation" InitialValue="Please Select" ErrorMessage="Please select a Location"></asp:RequiredFieldValidator>
</td>
</tr>
当我点击提交按钮而没有选择任何下拉列值时,我没有收到任何错误消息。我的下拉代码是
Private Sub Test1()
ddlTest.DataSource = ' called the class to bid the drop down
ddlTest.DataTextField = "Test"
ddlTest.DataValueField = "Testid"
ddlTest.DataBind()
ddlTest.Items.Insert(0, New ListItem("Please Select", 0))
End Sub
Public Sub LoadLocation()
' ddlLocation.DataSource = ' called the class to bid the drop down
ddlLocation.DataTextField = "location"
ddlLocation.DataValueField = "id"
ddlLocation.DataBind()
ddlLocation.Items.Insert(0, New ListItem("Please Select", 0))
End Sub
任何帮助将不胜感激。当我点击提交按钮并且控件转移到提交按钮后面的代码时,我看不到任何错误消息。
答案 0 :(得分:0)
Dropdown
验证有点不同。您需要在验证程序中设置InitialValue
以验证和选择非选择错误
<asp:RequiredFieldValidator ID="ReqTest1" runat="server" InitialValue=0 ControlToValidate="ddlTest1" InitialValue="Please Select" ErrorMessage="Please Select Test1"></asp:RequiredFieldValidator>
InitialValue
是下拉列表中默认显示的列表项的任何值。
在处理任何事情之前,在按钮点击事件中检查Page.IsValid
也很重要。