我正在尝试在我的表单上实现一个必需的字段验证器,这样当我单击“发送”按钮时,它只有在所有必填字段都有值时才有效。现在,即使我设置了RequiredFieldValidator并单击发送,该功能仍然会触发并且不会停止。这是我现在的代码:
Default.aspx的
<form action="./Default.aspx" method="post" runat="server">
<div id="tabs" style="border:none" class="">
<br />
<br />
<button class="backBtn" type="button" name="goBackBtn" onclick="backBtnClicked()"><img src="https://png.icons8.com/back-arrow/win8/1600"></button>
<h1 class="title">Create NetEvent</h1>
<br />
<fieldset>
<legend>Sender Info</legend>
<div class="row">
<label class="senderInfoUniversal senderInfoLabel">Username:</label>
<input class="senderInfoUniversal senderInfoInput" type="text" runat="server" id="eventFormName" readonly="readonly"/>
</div>
<div class="row">
<label class="senderInfoUniversal senderInfoLabel">Email:</label>
<input class="senderInfoUniversal senderInfoInput" type="text" runat="server" id="eventFormEmail" readonly="readonly"/>
</div>
<div class="row">
<label class="senderInfoUniversal senderInfoInputExtra">*Sender email used when sending out NetEvent</label>
</div>
</fieldset>
<br />
<fieldset>
<legend>Type of Event</legend>
<div class="row">
<div data-toggle="buttons" class="btn-group col-md-12">
<label runat="server" id="serviceDisruptionLabel" class="btn btn-default" for="serviceDisruptionBtn" onclick="displayServiceDisruptionForm()">Service Disruption
<input class="labelInput" id="serviceDisruptionBtn" type="radio" required="required"/>
</label>
<label runat="server" id="plannedOutageLabel" class="btn btn-default" for="plannedOutageBtn" onclick="displayPlannedOutageForm()">Planned Outage
<input class="labelInput" id="plannedOutageBtn" type="radio"/>
</label>
</div>
</div>
</fieldset>
<br />
<fieldset>
<legend>NetEvent Information</legend>
<div id="serviceDisruptionForm" hidden="hidden">
<div class="col-md-6">
<div class="form-group">
<label class="userInfo"> Subject Line
<span class="required">*</span>
</label>
<asp:TextBox type="text" runat="server" id="eventFormSubjectLineSD" class="userInfo form-control customFormElement"
title="Invalid Subject Name"></asp:TextBox>
<asp:RequiredFieldValidator
runat="server"
ControlToValidate="eventFormSubjectLineSD"
ErrorMessage="THIS DID NOT WORK">
</asp:RequiredFieldValidator>
</div>
<div class="form-group">
<label class="userInfo"> Summary
<span class="required">*</span>
</label>
<textarea type="text" runat="server" id="eventFormSummarySD" class="userInfo form-control"
title="Invalid Subject Name"/>
</div>
<div class="form-group">
<label class="userInfo"> Resources Affected
<span class="required">*</span>
</label>
<select type="text" runat="server" id="eventFormResourcesAffectedSD" class="userInfo form-control"
title="Invalid Subject Name" clientIdMode="static">
<option style="display:none;"></option>
</select>
<textarea runat="server" id="eventFormResourcesAffectedOtherSD" clientIdMode="static"></textarea>
<textarea runat="server" id="resourcesOtherRowSD" hidden="hidden" clientIdMode="static"></textarea>
</div>
<div class="form-group">
<label class="userInfo"> People Affected
<span class="required">*</span>
</label>
<textarea type="text" runat="server" id="eventFormPeopleAffectedSD" class="userInfo form-control"
title="Invalid Subject Name"/>
</div>
<div class="form-group">
<label class="userInfo"> Work Around
<span class="required">*</span>
</label>
<textarea type="text" runat="server" id="eventFormWorkAroundSD" class="userInfo form-control"
title="Invalid Subject Name"/>
</div>
<div class="form-group">
<label class="userInfo"> Start Time
<span class="required">*</span>
</label>
<input type="text" runat="server" id="eventFormStartTimeSD" class="userInfo form-control"
title="Invalid Subject Name" clientIdMode="static"/>
</div>
<div class="form-group">
<label class="userInfo"> Estimated Time of Resolution
<span class="required">*</span>
</label>
<div class="col">
<label id="unknownTextSD" for="eventFormTimeCheckBoxSD">Unknown</label>
<input type="checkbox" runat="server" id="eventFormTimeCheckBoxSD" clientIdMode="static"/>
</div>
<input type="text" runat="server" id="eventFormEstimatedTimeOfResolutionSD" class="userInfo form-control"
title="Invalid Subject Name" clientIdMode="static"/>
</div>
<div class="form-group">
<label id="draftLabelSD" class="userInfo">Save as Draft</label>
<input type="checkbox" runat="server" id="eventFormDraftCheckBoxSD" clientIdMode="static"/>
<textarea runat="server" id="eventFormDraftEmailInputSD" clientIdMode="static" hidden="hidden"
placeholder="Type in the emails you would like the draft to be set to, seperated by a comma"/>
</div>
</div>
</div>
<div id="plannedOutageForm" hidden="hidden">
<div class="col-lg-6">
<div class="form-group">
<label class="userInfo"> Subject Line
<span class="required">*</span>
</label>
<input type="text" runat="server" id="eventFormSubjectLinePO" class="userInfo form-control"
title="Invalid Subject Name"/>
</div>
<div class="form-group">
<label class="userInfo"> Summary
<span class="required">*</span>
</label>
<textarea type="text" runat="server" id="eventFormSummaryPO" class="userInfo form-control"
title="Invalid Subject Name"/>
</div>
<div class="form-group">
<label class="userInfo"> Resources Being Changed
<span class="required">*</span>
</label>
<select type="text" runat="server" id="eventFormResourcesBeingChangedPO" class="userInfo form-control"
title="Invalid Subject Name" clientIdMode="static">
<option style="display:none;"></option>
</select>
<textarea runat="server" id="eventFormResourcesBeingChangedOtherPO" clientIdMode="static"></textarea>
</div>
<div class="form-group">
<label class="userInfo"> Change Benefits
<span class="required">*</span>
</label>
<textarea type="text" runat="server" id="eventFormChangeBenefitsPO" class="userInfo form-control"
title="Invalid Subject Name"/>
</div>
<div class="form-group">
<label class="userInfo"> Impact on End Users
<span class="required">*</span>
</label>
<textarea type="text" runat="server" id="eventFormImpactOnEndUsersPO" class="userInfo form-control"
title="Invalid Subject Name"/>
</div>
<div class="form-group">
<label class="userInfo"> Proposed Start Date/Time
<span class="required">*</span>
</label>
<input type="text" runat="server" id="eventFormProposedStartDatePO" class="userInfo form-control"
title="Invalid Subject Name"/>
</div>
<div class="form-group">
<label class="userInfo"> Proposed End Date/Time
<span class="required">*</span>
</label>
<input type="text" runat="server" id="eventFormProposedEndDatePO" class="userInfo form-control"
title="Invalid Subject Name"/>
</div>
<div class="form-group">
<label id="draftLabelPO" class="userInfo">Save as Draft</label>
<input type="checkbox" runat="server" id="eventFormDraftCheckBoxPO" clientIdMode="static"/>
<textarea runat="server" id="eventFormDraftEmailInputPO" clientIdMode="static" hidden="hidden"
placeholder="Type in the emails you would like the draft to be set to, seperated by a comma"/>
</div>
</div>
</div>
</fieldset>
<br />
<asp:Button ID="eventFormSubmitBtn" runat="server" Text="Send" OnClientClick="setValidators()" OnClick="eventFormSubmitBtn_Clicked"/>
<asp:HiddenField ID="typeHolder" runat="server" />
</div>
</form>
此外,所有这些都嵌套在服务器上运行的表单中。
Default.aspx.cs
// Buttin Submit Methods
protected void eventFormSubmitBtn_Clicked(object sender, EventArgs e)
{
saveData();
goBackToStartPage();
}
所以现在即使表单有空字段,单击按钮仍会将我发送到另一页。它是否与我的事件类型字段集有关,我在服务中断或计划中断之间切换?我想这样做,当选择服务中断时,按下的发送按钮不起作用,除非必需的字段具有值并且在选择计划中断时相同。