Reputation: 557
Need a little input as I am not seeing why this is occurring.
I have several dropdownlists in a formview.
In edit mode, all the lists bind as they should.
But in insert mode, the lists are empty.
The controls are being found, there are no null values.
They are being bound from the code behind. I do NOT use sqldatasource.
But I did test one and used a sqldatasource and it worked ok.
Here's some code for thought:
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:FormView ID="fvTest" runat="server" DataKeyNames="ID" OnItemUpdating="fvTest_ItemUpdating"
OnItemInserting="fvTest_ItemInserting" OnModeChanging="fvTest_ModeChanging">
<EditItemTemplate>
<asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0">
<asp:View ID="View1" runat="server">
<asp:DropDownList ID="ddlEditTest" runat="server" CssClass="ddlAutoWidth">
</asp:DropDownList>
</asp:View>
</asp:MultiView>
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
Text="Update" />
<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False"
CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
<InsertItemTemplate>
<asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0">
<asp:View ID="View1" runat="server">
<asp:DropDownList ID="ddlAddTest" runat="server" CssClass="ddlAutoWidth">
</asp:DropDownList>
</asp:View>
</asp:MultiView>
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert"
Text="Insert" />
<asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False"
CommandName="Cancel" Text="Cancel" />
</InsertItemTemplate>
<ItemTemplate>
<asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0">
<asp:View ID="View1" runat="server">
</asp:View>
</asp:MultiView>
<asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit"
Text="Edit" />
<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" CommandName="New"
Text="New" />
</ItemTemplate>
</asp:FormView>
</ContentTemplate>
</asp:UpdatePanel>
And the code behind:
protected void fvTest_ModeChanging(object sender, FormViewModeEventArgs e)
{
int myID = int.Parse(ddlItem.SelectedValue);//another ddl that is not on the formview
switch (e.NewMode)
{
case FormViewMode.Edit:
fvTest.ChangeMode(FormViewMode.Edit);
BindFormView(myID);
break;
case FormViewMode.Insert:
fvTest.ChangeMode(FormViewMode.Insert);
BindFormView(myID);
break;
case FormViewMode.ReadOnly:
fvTest.ChangeMode(FormViewMode.ReadOnly);
BindFormView(myID);
break;
}
}
private void BindFormView(int myID)
{
DataTable dt = BLL.TestBLL.GetItemTest(myID);
fvTest.DataSource = dt;
fvTest.DataBind();
switch (fvTest.CurrentMode)
{
case FormViewMode.Edit:
FillEditLists(dt);
break;
case FormViewMode.Insert:
//dt = null;
//fvTest.DataBind();
FillInsertLists();
break;
}
}
private void FillEditLists(DataTable dtFrmView)
{
string selValue = "";
MultiView MultiView1 = (MultiView)fvTest.FindControl("MultiView1");
View View1 = (View)MultiView1.FindControl("View1");
DropDownList ddlEditTest = (DropDownList)View1.FindControl("ddlEditTest");
DataTable dt = BLL.ListsBLL.GetCommonCategory(1, 1);
ddlEditTest.DataTextField = "Name";
ddlEditTest.DataValueField = "CommonID";
ddlEditTest.DataSource = dt;
ddlEditTest.DataBind();
ddlEditTest.Items.Insert(0, new ListItem("", "0"));
selValue = dtFrmView.Rows[0]["TestTypeID"].ToString();
if (!string.IsNullOrEmpty(selValue))
ddlEditTest.SelectedValue = selValue;
}
private void FillInsertLists()
{
MultiView MultiView1 = (MultiView)fvTest.FindControl("MultiView1");
View View1 = (View)MultiView1.FindControl("View1");
DropDownList ddlAddTest = (DropDownList)View1.FindControl("ddlAddTest");
DataTable dt = BLL.ListsBLL.GetCommonCategory(1, 1);
ddlAddTest.DataTextField = "Name";
ddlAddTest.DataValueField = "CommonID";
ddlAddTest.DataSource = dt;
ddlAddTest.DataBind();
ddlAddTest.Items.Insert(0, new ListItem("", "0"));
}
In edit mode, all the lists are bound just fine and the selected values are correct.
But in Insert mode, all the lists are empty.
The controls are being found as there are no null values.
What am I missing here?
Thanks!!
Upvotes: 1
Views: 953
Reputation: 557
I found the solution and I should know better.
I needed to use the databound event.
protected void fvTest_DataBound(object sender, EventArgs e)
{
if (fvTest.CurrentMode == FormViewMode.Insert)
{
FillInsertLists();
}
}
Upvotes: 1