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
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.