以下是我的代碼的測試場景。 1)一旦用戶選擇Webpage.aspx上的一個單選按鈕,就會顯示一個模式彈出式擴展器。 2)用戶控件(SSL_Ticket.ascx)在模態彈出窗口中定義。 3)RequiredFieldValidator是爲用戶控件內包含的下拉列表定義的。 4)如果用戶從下拉列表中選擇「0」值,則不顯示驗證錯誤消息。用戶控件內下拉控件的RequiredFieldValidator不會觸發
代碼
Webpage.aspx
<asp:RadioButtonList ID="RadioButtonListForTicket" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="radioButtonListForTicket_OnSelectedIndexChanged">
<asp:ListItem Selected="True">No</asp:ListItem>
<asp:ListItem>Yes</asp:ListItem>
</asp:RadioButtonList>
<ajaxToolkit:ModalPopupExtender ID="ModalPopupExtenderForTicket" runat="server" BackgroundCssClass="popUpStyle"
DropShadow="true" PopupControlID="divTicketPreview" PopupDragHandleControlID="panelDragHandle"
TargetControlID="btnForPopupAppear" CancelControlID="btnForPopupDisappear"/>
....
...
Webpage.aspx.cs
protected void radioButtonListForTicket_OnSelectedIndexChanged(object sender, System.EventArgs e)
{
if (RadioButtonListForTicket.SelectedItem.Text.ToString().Equals("Yes"))
{
// Check if the sites are selected
updateSelectionCount();
updateListOfSites();
if (selectionCount == 0)
{
lblSSLTicketSelection.Text = "Please select a site.";
RadioButtonListForTicket.SelectedValue = "No";
return;
}
else
{
lblSSLTicketSelection.Text = "";
}
....
ModalPopupExtenderForTicket.Show();
}
}
...
SSL_Ticket.ascx
<asp:DropDownList ID="cmbRootCause" runat="server" Width="255px" OnSelectedIndexChanged="cmbRootCause_SelectedIndexChanged" AutoPostBack="true"
CausesValidation="true">
<asp:ListItem Value="0">Select</asp:ListItem>
<asp:ListItem Value="1">Item1</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="reqdFieldForRootCause" runat="server" ControlToValidate="cmbRootCause" InitialValue="Select"
ErrorMessage="Please select root cause" ValidationGroup="validateRootCause" Visible="false" Display="Dynamic" EnableClientScript="true">
</asp:RequiredFieldValidator>
...
SSL_Ticket.ascx.cs
protected void cmbRootCause_SelectedIndexChanged(object sender, EventArgs e)
{
if (cmbRootCause.SelectedItem.ToString().Equals("Other"))
{
lblcmbRootCause.Text = "";
lblcmbRootCause.Visible = false;
txtRootCauseOther.Visible = true;
}
else if (cmbRootCause.SelectedItem.ToString().Equals("Select"))
{
lblcmbRootCause.Visible = true;
lblcmbRootCause.Text = "Please select root cause";
}
else
{
lblcmbRootCause.Text = "";
lblcmbRootCause.Visible = false;
txtRootCauseOther.Visible = false;
}
}
我確實瀏覽了幾個解決方案(Validat eProperty,客戶端驗證,RangeValidation等),但它沒有激發驗證文本。
這並沒有幫助 - Handling RequiredFieldValidator inside of a User Control
我會感謝您的幫助很大。
謝謝!
感謝提供解決方案。僅當我先選擇有效的根本原因然後從DDL中選擇「選擇」選項時,它纔會顯示錯誤消息。如果我將根本原因保留爲「選擇」(默認爲選中),則不會顯示錯誤消息。我打算實施從JavaScript驗證焦點變化的驗證。任何指針? – 2013-03-06 00:11:36
請再提一個問題 - 我不確定上面代碼中顯示的RequiredFieldValidator是否是服務器端驗證或客戶端驗證。請告訴我。 – 2013-03-06 00:48:03
他們執行客戶端和服務器端驗證。 – Sunny 2013-03-06 02:08:58