gpt4 book ai didi

javascript - 空 Asp.Net 控件导致 Javascript 对象引用未设置为渲染时对象的实例

转载 作者:行者123 更新时间:2023-12-02 16:01:19 26 4
gpt4 key购买 nike

总而言之,此代码将 Jquery 自动完成添加到位于 UpdatePanel 内部的 Asp.Net Formview 内的文本框。

除非 InsuranceInsertItemTextBox 为空(当 Formview 处于只读模式时总是出现这种情况),否则以下代码可以正常工作。我尝试使用 if 语句在控件为空时终止该函数,但这不起作用。如果 Formview 模式设置为只读,我还尝试终止该语句,但这也不起作用。关于处理此错误有什么建议吗?仅供引用:第 33 行(在堆栈跟踪中标识)是 InitAutoCompl 函数内部的 if 语句。如果我删除 if 语句,错误将转到下一行。

        $(document).ready(function () {
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_initializeRequest(InitializeRequest);
prm.add_endRequest(EndRequest);
InitAutoCompl();
});

function InitializeRequest(sender, args) {
}

function EndRequest(sender, args) {
InitAutoCompl();
} function InitAutoCompl(freeInput) {

if (!document.getElementById('<%= CaseInformationFormView.FindControl("InsuranceInsertItemTextBox").ClientID %>'))return;
$('#<%= CaseInformationFormView.FindControl("InsuranceInsertItemTextBox").ClientID %>').autocomplete({
source: function(request, response) {
$.ajax({
url: '<%= Page.ResolveUrl("PacketsForUpload.aspx/GetInsuranceCompanies") %>',
data: "{ 'prefix': '" + request.term + "'}",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
messages: {
noResults: '',
results: function() {}
},
success: function(data) {
response($.map(data.d, function(item) {
return {
label: item.split("-")[0],
val: item.split("-")[1]
};
}));
},
error: function(response) {
alert(response.responseText);
},
failure: function(response) {
alert(response.responseText);
}
});
},
change: function(e, ui) {
if (!(freeInput || ui.item)) e.target.value = "";
Page_ClientValidate();
},
select: function(e, i) {
console.log("i.item", i.item);
Page_ClientValidate();
},
minLength: 3

});
};

堆栈跟踪:

[NullReferenceException: Object reference not set to an instance of an object.]
ASP.packetsforupload_aspx.__RenderPacketsHeadContent(HtmlTextWriter __w, Control parameterContainer) in :33
System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +268
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +8
System.Web.UI.Control.Render(HtmlTextWriter writer) +10
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +57
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +100
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +128
System.Web.UI.HtmlControls.HtmlHead.RenderChildren(HtmlTextWriter writer) +21
System.Web.UI.HtmlControls.HtmlContainerControl.Render(HtmlTextWriter writer) +32
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +57
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +100
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +128
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +8
System.Web.UI.Control.Render(HtmlTextWriter writer) +10
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +57
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +100
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +128
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +8
System.Web.UI.Page.Render(HtmlTextWriter writer) +29
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +57
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +100
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1386

编辑:此外,我刚刚发现 return 不会破坏该函数。我通过删除 if 语句并仅在其中放置一个 return 来对此进行测试。错误仍然发生。

最佳答案

如果控件“InsuranceInsertItemTextBox”不是 CaseInformationFormView 的子控件,则 CaseInformationFormView.FindControl("InsuranceInsertItemTextBox") 为 null,并且调用属性 ClientID 将生成 NRE。

您可以通过在获取 ClientID 之前检查控件是否可用来防止这种情况:

function InitAutoCompl(freeInput) {
$('#<%= CaseInformationFormView.FindControl("InsuranceInsertItemTextBox") != null ?CaseInformationFormView.FindControl("InsuranceInsertItemTextBox").ClientID : "" %>').autocomplete({
source: function (request, response) {
$.ajax({
url: '<%= Page.ResolveUrl("PacketsForUpload.aspx/GetInsuranceCompanies") %>',
data: "{ 'prefix': '" + request.term + "'}",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
messages: {
noResults: '',
results: function () { }
},
success: function (data) {
response($.map(data.d, function (item) {
return {
label: item.split("-")[0],
val: item.split("-")[1]
};
}));
},
error: function (response) {
alert(response.responseText);
},
failure: function (response) {
alert(response.responseText);
}
});
},
change: function (e, ui) {
if (!(freeInput || ui.item)) e.target.value = "";
Page_ClientValidate();
},
select: function (e, i) {
console.log("i.item", i.item);
Page_ClientValidate();
},
minLength: 3

});
};

关于javascript - 空 Asp.Net 控件导致 Javascript 对象引用未设置为渲染时对象的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31167824/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com