需要一些输入,因为我没有看到为什么会发生这种情况 我在formview中有几个下拉列表 在编辑模式下,所有列表都应按原样绑定 但在插入模式下,列表为空 正在找到控件,没有空值 他们受到背后代码的束缚。我不使用sqldatasource 但我测试了一个并使用了sqldatasource,它运行正常 以下是一些思考的代码:
<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>
背后的代码:
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"));
}
在编辑模式下,所有列表都绑定得很好,所选值正确
但在插入模式下,所有列表都是空的
由于没有空值,因此找到了控件。
我在这里缺少什么?
谢谢!
答案 0 :(得分:1)
我找到了解决方案,我应该更清楚 我需要使用数据绑定事件。
protected void fvTest_DataBound(object sender, EventArgs e)
{
if (fvTest.CurrentMode == FormViewMode.Insert)
{
FillInsertLists();
}
}