I've some tables in my db with the following structure.
I've two asp.net ajax comboboxes in the same update panel which is supposed to load data based on the tables mentioned above.
<asp:UpdatePanel ID="updatesetloc" runat="server">
<ContentTemplate>
<p>
<asp:ComboBox ID="cbloc" runat="server" AutoPostBack="True"
ondatabound="OnComboBoxDataBound" oniteminserted="OnComboBoxItemInserted"
oniteminserting="OnComboBoxItemInserting" onprerender="OnComboBoxPreRender"
onselectedindexchanged="OnSelectedIndexChanged">
</asp:ComboBox>
</p>
<p>
<asp:ComboBox ID="cbsets" runat="server" AutoPostBack="True"
ondatabound="OnComboBoxDataBound" oniteminserted="OnComboBoxItemInserted"
oniteminserting="OnComboBoxItemInserting"
onselectedindexchanged="OnSelectedIndexChanged">
</asp:ComboBox>
</p>
<p>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</p>
</ContentTemplate>
</asp:UpdatePanel>
The requirement is that at page load the two comboboxes should load with default data. (ie. from sets and locations tables respectively). Now if we are adding a new location we should have an associated set with it too. This has to happen vice versa too. But for the time being we'll concentrate on the first.
So I've handled the combobox's ItemInserting and ItemInserted events. While inserting a new location I've to add the new location into the db and get a location id for it. After this point I have a valid list entry in my locations combobox. This location doesn't have a set associated with it. We should have a set. But anyhow I am binding the sets combobox with the newly generated id.
This gives me the following error out of no where. I guess it is after the cbsets databind.
Here is the codebehind for the page:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;
using GEDBTableAdapters;
public partial class testcombo : System.Web.UI.Page
{
enum ComboBoxData { LocationsComboBoxChanged, SetsComboBoxChanged, NothingSet }
ComboBoxData setter;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
setter = ComboBoxData.NothingSet;
LocationsTableAdapter loc = new LocationsTableAdapter();
cbloc.DataSource = loc.GetData();
cbloc.DataTextField = "name";
cbloc.DataValueField = "lid";
cbloc.DataBind();
SetsTableAdapter sets = new SetsTableAdapter();
cbsets.DataSource = sets.GetData();
cbsets.DataTextField = "setname";
cbsets.DataValueField = "setid";
cbsets.DataBind();
}
}
void GetSets(int locationID)
{
SetsTableAdapter sets = new SetsTableAdapter();
cbsets.DataSource = sets.GetLocationSets(locationID);
cbsets.DataTextField = "setname";
cbsets.DataValueField = "setid";
//if (!IsPostBack)
cbsets.DataBind();
}
void GetLocations(int setID)
{
LocationsTableAdapter loc = new LocationsTableAdapter();
cbloc.DataSource = loc.GetSetLocations(setID);
cbloc.DataTextField = "name";
cbloc.DataValueField = "lid";
cbloc.DataBind();
}
protected void OnComboBoxDataBound(object sender, EventArgs e)
{
if (setter == ComboBoxData.NothingSet)
{
if (sender.Equals(cbloc))
{
if (cbloc.Items.Count == 0)
{
cbloc.Items.Add("Add new location");
return;
}
cbloc.Items.Insert(0, new ListItem("[Select or add new location]", "0"));
}
else
{
if (cbsets.Items.Count == 0)
{
cbsets.Items.Add("Add new set");
return;
}
cbsets.Items.Insert(0, new ListItem("[Select or add new set]", "0"));
}
}
}
protected void OnSelectedIndexChanged(object sender, EventArgs e)
{
if (sender.Equals(cbloc))
{
setter = ComboBoxData.LocationsComboBoxChanged;
GetSets(int.Parse(cbloc.SelectedValue));
}
else
{
setter = ComboBoxData.SetsComboBoxChanged;
GetLocations(int.Parse(cbsets.SelectedValue));
}
}
protected void OnComboBoxItemInserting(object sender, AjaxControlToolkit.ComboBoxItemInsertEventArgs e)
{
if (sender.Equals(cbloc))
{
LocationsTableAdapter loc = new LocationsTableAdapter();
e.Item.Value = loc.AddNewLocation(e.Item.Text).ToString();
}
else
{
SetsTableAdapter sets = new SetsTableAdapter();
e.Item.Value = sets.AddNewSet(e.Item.Text).ToString();
}
}
protected void OnComboBoxItemInserted(object sender, AjaxControlToolkit.ComboBoxItemInsertEventArgs e)
{
//return;
if (sender.Equals(cbloc))
GetSets(int.Parse(e.Item.Value));
else
GetLocations(int.Parse(e.Item.Value));
}
protected void OnComboBoxPreRender(object sender, EventArgs e)
{
//if (IsPostBack)
//{
// AjaxControlToolkit.ComboBox cbx = (AjaxControlToolkit.ComboBox)sender;
// cbx.SelectedIndex = 0;
//}
}
}
Another thread by someone who has the same issue -> http://forums.asp.net/t/1607905.aspx But it didn't help.