我有一个在 ASP.NET 页面中使用的客户端 Javascript 来设置数据列表中几个项目的可见性。基本上,当项目 1 可见时,项目 2 应该被隐藏,反之亦然。此代码还根据客户端周围字段的可见性启用/禁用一些 ASP.NET 所需的字段验证器。
除非我遇到 ASP.NET 验证错误,否则代码可以正常工作。当验证失败时,这些字段将重置为其默认可见性(隐藏)。
我已经排除这是回发,因为当问题发生时我的服务器端断点不会受到影响。这似乎与客户端脚本有关。
由于页面从不回发,服务器端修复程序已发布。我想知道如何从 Javascript 的角度来做到这一点?当验证脚本触发时,有没有办法触发自定义代码?
我考虑过使用 cookie,但我认为这对我没有帮助,除非我可以加入验证事件。
代码如下...
function handleDropDown(e, a, b, c, v1, v2, h1, h2) {
var i = parseInt(c) + 2;
var x = parseInt(c) + 3;
if (e.value == "Yes") {
document.getElementById(i).style.display = "inline";
document.getElementById(i).style.visibility = "visible";
document.getElementById(x).style.display = "none";
document.getElementById(x).style.visibility = "hidden";
document.getElementById(a).focus();
//Enable validator if needed
var oVal1 = document.getElementById(v1);
var oVal2 = document.getElementById(v2);
var oVis1 = document.getElementById(h1);
var oVis2 = document.getElementById(h2);
ValidatorEnable(oVal1, true);
if (oVis1 != null) {
oVis1.value = "true";
}
ValidatorEnable(oVal2, false);
if (oVis2 != null) {
oVis2.value = "false";
}
}
else if (e.value == "No") {
document.getElementById(x).style.display = "inline";
document.getElementById(x).style.visibility = "visible";
document.getElementById(i).style.display = "none";
document.getElementById(i).style.visibility = "hidden";
document.getElementById(b).focus();
//Enable validator if needed
var oVal1 = document.getElementById(v1);
var oVal2 = document.getElementById(v2);
var oVis1 = document.getElementById(h1);
var oVis2 = document.getElementById(h2);
ValidatorEnable(oVal1, false);
if (oVis1 != null) {
oVis1.value = "false";
}
ValidatorEnable(oVal2, true);
if (oVis2 != null) {
oVis2.value = "true";
}
}
}
ASPX 标记片段:**
<span id="spTxtAnswer" class="required" visible="false" runat="server">*</span>
<asp:TextBox ID="txtAnswer" Text='<%# Bind("Answer") %>' runat="server"
TextMode="MultiLine" Height="76px" MaxLength="2000" Width="370px" Visible="false" >
</asp:TextBox>
<asp:RequiredFieldValidator ID="valTextBox" ErrorMessage="This question is required." Enabled="false"
Display="Static" ControlToValidate="txtAnswer" runat="server" ValidationGroup="Request" />
<asp:RequiredFieldValidator ID="valAnswer" ControlToValidate="txtAnswer" ErrorMessage="<br />Other description is required."
Display="Static" enabled="false" runat="server" ValidationGroup="Request" />
<span id="spDdlAnswer" class="required" visible="false" runat="server">*</span>
<asp:DropDownList ID="ddlAnswer" Visible="false" runat="server" />
<asp:RequiredFieldValidator ID="valDropDown" ControlToValidate="ddlAnswer" Enabled="false"
ErrorMessage="This field is required." Display="Static" runat="server"
InitialValue="(Select)" ValidationGroup="Request" />
<asp:HiddenField ID="hfQuestionID" Value='<%# Bind("QuestionID") %>' runat="server" />
<asp:HiddenField ID="hfAnswer" Value='<%# Bind("Answer") %>' runat="server" />
<asp:HiddenField ID="hfRequired" Value='<%# Bind("Required") %>' runat="server" />
<asp:HiddenField ID="hfVisible" Value="false" runat="server" />
代码隐藏片段:
TextBox tb1 = (TextBox)gridUserSupplierTypeQuestionsAndAnswers.Rows[rowIndex + 1].FindControl("txtAnswer");
TextBox tb2 = (TextBox)gridUserSupplierTypeQuestionsAndAnswers.Rows[rowIndex + 2].FindControl("txtAnswer");
RequiredFieldValidator rfv1 = (RequiredFieldValidator)gridUserSupplierTypeQuestionsAndAnswers.Rows[rowIndex + 1].FindControl("valTextBox");
RequiredFieldValidator rfv2 = (RequiredFieldValidator)gridUserSupplierTypeQuestionsAndAnswers.Rows[rowIndex + 2].FindControl("valTextBox");
HiddenField hf1 = (HiddenField)gridUserSupplierTypeQuestionsAndAnswers.Rows[rowIndex + 1].FindControl("hfVisible");
HiddenField hf2 = (HiddenField)gridUserSupplierTypeQuestionsAndAnswers.Rows[rowIndex + 2].FindControl("hfVisible");
ddl.Attributes.Add("onchange", "handleDropDown(this,'" + tb1.ClientID + "','" + tb2.ClientID + "','" + questionID + "','" + rfv1.ClientID + "','" + rfv2.ClientID + "'," +
"'" + hf1.ClientID + "','" + hf2.ClientID + "');");
//When selected and viewing in edit mode, set the visibility during page load
string script = "<script language='javascript'>";
script += System.Environment.NewLine + "var el = document.getElementById('" + ddl.ClientID + "');";
script += System.Environment.NewLine + "handleDropDown(el,'" + tb1.ClientID + "','" + tb2.ClientID + "','" + questionID + "','" + rfv1.ClientID + "','" + rfv2.ClientID + "');</script>";
ClientScript.RegisterStartupScript(Page.GetType(),"startScript", script);
**