【问题标题】:Dropdownlist in Formview will not bind in insert modeFormview中的下拉列表不会在插入模式下绑定
【发布时间】:2014-01-09 01:59:12
【问题描述】:

需要一点意见,因为我不明白为什么会发生这种情况。
我在表单视图中有几个下拉列表。
在编辑模式下,所有列表都按应有的方式绑定。
但是在插入模式下,列表是空的。
正在找到控件,没有空值。
他们被背后的代码绑定。我不使用 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" />
                &nbsp;<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" />
                &nbsp;<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" />
                &nbsp;<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"));

    }

在编辑模式下,所有列表都绑定得很好,并且选择的值是正确的。
但在插入模式下,所有列表都是空的。
正在找到控件,因为没有空值。

我在这里错过了什么?

谢谢!!

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    我找到了解决方案,我应该知道得更多。
    我需要使用数据绑定事件。

        protected void fvTest_DataBound(object sender, EventArgs e)
        {
            if (fvTest.CurrentMode == FormViewMode.Insert)
            {
                FillInsertLists();
    
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多