Hi all,
I'm trying to create a set of cascading DropDownLists within a BOUND FormView.
<asp:FormView ID="FormView1" ....>
<EditItemTemplate>
Program Mode:
<asp:DropDownList ID="ProgramModeID" SelectedValue='<%# Bind("ProgramModeID") %>'
runat="server" DataSourceID="dsProgramMode" DataTextField="ProgramModeDesc" DataValueField="ProgramModeID"
AppendDataBoundItems="true" OnSelectedIndexChanged="Program_SelectedIndexChanged"
AutoPostBack="true">
</asp:DropDownList>
<asp:SqlDataSource ...>
Program Name:
<ajax:UpdatePanel ID="pnlProgramName" OnPreRender="FormView1_DataBinding" runat="server">
<Triggers>
<ajax:AsyncPostBackTrigger ControlID="ProgramCategoryID" EventName="SelectedIndexChanged" />
<ajax:AsyncPostBackTrigger ControlID="ProgramModeID" EventName="SelectedIndexChanged" />
</Triggers>
<ContentTemplate>
<asp:DropDownList ID="ProgramNameID" SelectedValue='<%# Bind("ProgramNameID") %>' runat="server" DataSourceID="dsProgramName"
DataTextField="ProgramNameDesc" DataValueField="ProgramNameID" AppendDataBoundItems="false">
</asp:DropDownList>
<asp:SqlDataSource ID="dsProgramName" runat="server" ConnectionString="<%$ ConnectionStrings:SqlServer %>">
</asp:SqlDataSource>
</ContentTemplate>
</ajax:UpdatePanel>
Here's what I'm trying to do:
Program Name dropdownlist (DDL) should change depending on what is selected in Program Mode DDL.
The events Program_SelectedIndexChanged and FormView1_DataBinding both call this function in the codebehind:
protected void GetdsProgramName()
{
SqlDataSource dsProgramName = (SqlDataSource)FormView1.FindControl("dsProgramName");
DropDownList ProgramNameID = (DropDownList)FormView1.FindControl("ProgramNameID");
if (dsProgramName != null && ProgramNameID != null)
{
string SelectCommand = "SELECT ...";
dsProgramName.SelectCommand = SelectCommand;
ProgramNameID.DataBind();
}
}
My problem is that I keep getting the error:
'ProgramNameID' has a SelectedValue which is invalid because it does not
exist in the list of items.
every time I go into the FormView. I think it's because I'm not calling the GetdsProgramName function early enough and the Binding is going ahead before the ProgramNameID DDL has been bound to the list of data. If I put the same SelectCommand within the <SqlDataSource> tag, it works fine.
When should I set the SelectCommand (and databind) the DDL? On which event? I'm super confused when it comes to this Bind command of FormView, because I can never figure out when the Bind takes place.
Would greatly appreciate any help. Thanks.