ASP.NET Cascading DropdownLists不保存回数据库

时间:2018-01-11 23:00:57

标签: asp.net gridview

我是ASP新手,但我花了2个小时搜索此问题。我有一个gridview和2个级联下拉菜单。 (部件组 - >部件类型) 我正在使用下面的代码。在编辑模式下,下拉列表可视地工作。当我点击"更新"链接,Part_Type总是作为NULL发送回数据库。当我在属性中使用Bind(" Part_Type")时,我得到错误"数据绑定方法,如Eval(),XPath()和Bind ......"

                <asp:TemplateField HeaderText="Part Group" SortExpression="Part_Group">
                <EditItemTemplate>
                    <asp:DropDownList ID="Part_GroupDD" runat="server" DataSourceID="PartGroupDD" DataTextField="Part_Group" DataValueField="Part_Group" SelectedValue='<%# Bind("Product_Group") %>' AutoPostBack="true">
                    </asp:DropDownList>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label14" runat="server" Text='<%# Bind("Product_Group") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Part Type" SortExpression="Part_Type">
                <EditItemTemplate>
                    <asp:DropDownList ID="PartType_DD" runat="server" DataSourceID="Part_Type_DD" DataTextField="Part_Type" DataValueField="Part_Type" />
                    <asp:SqlDataSource runat="server" ID="Part_Type_DD" ConnectionString="<%$ ConnectionStrings:ApplicationsDBConnect %>"
              SelectCommand="SELECT Part_Group, Part_Type FROM Part.dbo.PartGroup_PartTypeDD" FilterExpression="Part_Group = '{0}'">
            <FilterParameters>
            <asp:ControlParameter Name="PartGroup" ControlID="Part_GroupDD"
                 PropertyName="SelectedValue" />
            </FilterParameters>                       
        </asp:SqlDataSource>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="PartTypelbl" runat="server" Text='<%# Bind("Part_Type") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>

这是我在这里找到的并尝试过的 2 Dropdownlists in Gridview cause error: Databinding methods such as Bind() can only be used in the context of a databound control

但我一直在&#34;对象未设置..&#34;错误意味着标签也为空。

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if ((e.Row.RowState & DataControlRowState.Edit) > 0)
        {
            DropDownList DDL1 = e.Row.FindControl("PartType_DD") as DropDownList;

            Label LBL1 = new Label();
            LBL1 = e.Row.FindControl("PartTypelbl") as Label;

            if (LBL1.Text != null)
            {
                DDL1.SelectedValue = LBL1.Text;
            }
        }
    }
}

0 个答案:

没有答案