英文:
Ajax Toolkit ComboBox exception System.NullReferenceException in AjaxControlToolkit.dll
问题
我在我的网页中有一个Ajax工具包的组合框。这个组合框对于一些用户是隐藏的,而且为了绑定,我必须从另一个下拉列表中选择一个值。
<% if (HasNPermissions) { %>
<ajaxToolkit:ComboBox ID="cbTest" runat="server" CssClass="searchContentCombobox" ItemInsertLocation="OrdinalText" />
<% } %>
以及所选的下拉列表:
<asp:DropDownList CssClass="dropDownStyle1" ID="cboOwner" runat="server"
AutoPostBack="True" OnSelectedIndexChanged="cboOwner_SelectedIndexChanged">
</asp:DropDownList>
在代码后台:
protected void cboOwner_SelectedIndexChanged(object sender, EventArgs e)
{
BindCombo(null);
}
private void BindCombo()
{
cbTest.Items.Insert(0, new ListItem("none", "0"));
}
当用户能够访问此组合框时,一切都正常,但当用户无法看到它时,我会收到图片中的错误,并且调试器不会进入cboOwner_SelectedIndexChanged方法。
我尝试在页面加载中调用Bind方法,但出现了相同的问题System.NullReferenceException: 'Object reference not set to an instance of an object.' Except从AjaxControlToolkit.dll抛出。
英文:
I have an ajax toolkit combo-box in my web page. This combo-box is hidden for some users and for binding I have to select a value from another dropdown.
<% if (HasnPermissions) { %>
<ajaxToolkit:ComboBox ID="cbTest" runat="server" CssClass="searchContentCombobox" ItemInsertLocation="OrdinalText" />
<% } %>
and the selected dropdown:
<asp:DropDownList CssClass="dropDownStyle1" ID="cboOwner" runat="server"
AutoPostBack="True" OnSelectedIndexChanged="cboOwner_SelectedIndexChanged">
</asp:DropDownList>
on the code behind
protected void cboOwner_SelectedIndexChanged(object sender, EventArgs e)
{
BindCombo(null);
}
private void BindCombo()
{
cbTest.Items.Insert(0, new ListItem("none", "0"));
}
when the user has access to this combobox is working fine but when the user has no access to see I'm getting the error in the picture and the debugger is not entering the method cboOwner_SelectedIndexChanged.
I tried to put the Bind method in the page-load is the same problem System.NullReferenceException: 'Object reference not set to an instance of an object.' Except thown from AjaxControlToolkit.dll
答案1
得分: 0
我会使用样式来显示/隐藏控件。那些AJ控件在页面加载时执行大量的初始化代码,并且在页面上的条件性代码可能会出现问题。
更糟糕的是,后台代码中的事件可能必须存在。
因此,将该代码移到页面加载事件中,并在页面加载时进行隐藏/显示。
换句话说,不要根据内联条件代码添加控件,而是让该控件一直存在,以便移动部分能够运行以正确设置该控件的工作。
所以,只需隐藏/显示,但让/允许/设置该控件在页面上存在,这样后台代码的事件也不必条件添加/移除。而且由于后台代码是一个页面类,因此该解析在编译时发生。
所以,假设你想要隐藏/显示这个:
<div id="MyHideShowExample" runat="server">
<ajaxtoolkit:combobox id="cbTest" runat="server"
cssclass="searchContentCombobox"
iteminsertlocation="OrdinalText" />
<asp:DropDownList CssClass="dropDownStyle1"
ID="cboOwner" runat="server" AutoPostBack="True"
OnSelectedIndexChanged="cboOwner_SelectedIndexChanged">
</asp:DropDownList>
</div>
那么在页面加载时,这样做:
protected void Page_Load(object sender, EventArgs e)
{
if (HasPermissions)
{
MyHideShowExample.Style.Add("display", "inline");
}
else
{
MyHideShowExample.Style.Add("display", "none");
}
}
所以,不要尝试条件性地注入服务器端控件。它们在编译时解析,而不是运行时,如果你尝试连接服务器端代码(后台代码)和页面上的标记服务器端控件,那么你会遭受严重的后果。这里的编译时解析是一件“伟大”的事情。
我在上面使用了一个 "div",以防有一组控件,或者不止一个控件。如果你愿意,也可以直接使用样式来隐藏/显示控件,但对于一个 "组",只需将它们包装在一个带有服务器端标记的 div 中,以便后台代码可以轻松控制该内容的显示/隐藏。
英文:
I would show/hide the control by using style. Those aj controls do a whole bunch of initialization code on page load, and a conditional bit of code on the page is going to be problem matic.
And worse yet, events in code behind likely have to exist.
so, move that code to say page load event, and hide/show on page load.
In other words, do NOT add the control based on in-line conditional code, but leave/have/setup/enjoy/assume the control to always exist, and that way the boatloads of moving parts can run to setup that control for it to work correctly.
So, just hide show, but leave/allow/have/set that control to exist on the page, so the code behind events "also" don't have to be conditional added/removed. And since code behind is a page class, then that resolution occurs at compile time anyway.
so, say you want to hide/show this:
<div id="MyHideShowExample" runat="server">
<ajaxtoolkit:combobox id="cbTest" runat="server"
cssclass="searchContentCombobox"
iteminsertlocation="OrdinalText" />
<asp:DropDownList CssClass="dropDownStyle1"
ID="cboOwner" runat="server" AutoPostBack="True"
OnSelectedIndexChanged="cboOwner_SelectedIndexChanged">
</asp:DropDownList>
</div>
So, say on page load, then this:
protected void Page_Load(object sender, EventArgs e)
{
if (HasnPermissions)
{
MyHideShowExample.Style.Add("display", "inline");
}
else
{
MyHideShowExample.Style.Add("display", "none");
}
}
So, don't try to inject server side controls conditional. they are resolved at compile time, not run time, and you suffer greatly if you try to connect up server side code (code behind) and that of markup server side controls on the page. It is a "great" thing that you have compile time resolution here.
I used a "div" in above, just in case there is a group of controls, or more then one control. You can also hide/show using style directly against the controls if you wish, but for a "group", then just wrap them in a div with a server side tag so code behind can easy control the show/hide of that content.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论