I have an issue using Ajax Control Toolkit ComboBox.
Here is my test markup page:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
<div>
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True" />
<% if (CheckBox1.Checked)
{ %><asp:ComboBox ID="ComboBox1" runat="server">
</asp:ComboBox>
<% } %>
</div>
</form>
</body>
</html>
After CheckBox1 rises PostBack I get a NullReferenceException in the method AjaxControlToolKit.ComboBox.System.Web.UI.IPostBackDataHandler.LoadPostData(stringpostDataKey, System.Collections.Specialized.Name.ValueCollection postCollection) and Visual Web Developer 2010 Express asks me the path of the Class ComboBox.cs that I don’t have.
Without <% if { } %> the ComboBox works fine.
Can anyone help me?
The reason is that when checkbox becomes checked and postback arised, combobox control expects that in posted from client data collection exists data for his state hidden field control.
There are two workarounds available: the first on suggested in this answer: https://stackoverflow.com/a/12364475/360171
And the second one (that indeed more correct) is don’t use
<% ... %>expressions to hide server controls. UseComboBox1.Visible = CheckBox1.Checkedin the Page_Load method instead.Moreover, try to avoid
<% .. %>expressions in markup to perform any manipulations with server controls.