在我的页面中,我有一个用户控件,它会在开关打开时插入一个 jQuery 函数。这是我在用户控件背后的代码:
public bool isRequired {
set {
if (value == true) {
ClientScriptManager cs = Page.ClientScript;
string csname = "isRequiredScript";
if (!cs.IsClientScriptBlockRegistered(this.GetType(), csname)) {
StringBuilder cstext = new StringBuilder();
cstext.Append("<script type=\"text/javascript\">");
cstext.Append("$(document).ready(function () {");
cstext.Append("function QuestionwithConditionalInfo_validation() {");
cstext.Append("if ($(\"#MainPlaceHolder_" + QuestionOption.ClientID + " :checked\").val() == null) {");
cstext.Append("alert(\"Please answer the question '" + setQuestionText + "'\");");
cstext.Append("return false;");
cstext.Append("}} });");
cstext.Append("</script>");
cs.RegisterClientScriptBlock(this.GetType(), csname, cstext.ToString(), false);
}
}
}
}
然后在主页面中,我打算调用那个 jQuery 函数:
function childpage_validation() {
if (QuestionwithConditionalInfo_validation() == false)
return false;
}
<asp:Button ID="page1_Next" Text="Next page" runat="server" OnClick="page1_Next_Command" OnClientClick="return childpage_validation()" />
然后我收到错误说该函数QuestionwithConditionalInfo_validation()
未定义,之后我尝试RegisterStartupScript
而不是RegisterClientScriptBlock
得到相同的错误。有谁知道为什么?