无法在formview中的下拉列表中设置所选项目

时间:2015-07-31 04:52:45

标签: c# asp.net

问题是我无法设置下拉列表选定项目。我有一个带有两个下拉列表的面板。从第一个下拉列表(editMerchGroupDropDownList1)中选择产品组时,它应根据ProductGroup的CategoryID值设置第二个下拉列表(MerchGroupDropDownList)的选定值。

  • 类别:ID,名称
  • ProductGroup:ID,Name,CategoryID

即使我尝试固定值,也不会更改所选项目。

MerchGroupDropDownList.Items.FindByValue("2").Selected = true;

ASP.NET代码:

<asp:Panel ID="editMerchGroup" runat="server">
    <h3>Edit Mechandise Group:</h3>
    Select a Mechandise Group to edit:
    <asp:DropDownList ID="editMerchGroupDropDownList1" runat="server"
        ItemType="App.Models.ProductGroup"
        SelectMethod="GetMerchProductGroups" DataTextField="ProductGroupName"
        DataValueField="ProductGroupID" OnSelectedIndexChanged="editMerchGroupDropDownList1_SelectedIndexChanged" AutoPostBack="true">
    </asp:DropDownList>

    <asp:FormView ID="editMerchGroupFormView1" runat="server" ItemType="App.Models.ProductGroup" SelectMethod="GetMerchGroup" RenderOuterTable="false">
        <ItemTemplate>
            <asp:HiddenField ID="editMerchGroupHiddenField" runat="server" value="<%#:Item.CategoryID %>" />
            <br />
            <br />
            <table>
                <tr>
                    <td>
                        <asp:Label ID="Label4" runat="server">Belongs To Category:</asp:Label></td>
                    <td>
                        <asp:DropDownList ID="MerchGroupDropDownList" runat="server"
                            ItemType="App.Models.Category"
                            SelectMethod="GetMerchCategories" DataTextField="CategoryName"
                            DataValueField="CategoryID" >
                        </asp:DropDownList>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="MerchGroupNameLabel" runat="server">Name:</asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="editMerchGroupNameTextBox" Text="" runat="server" ></asp:TextBox>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator6" ValidationGroup="EditMerchGroup" runat="server" Text="* Merchandise name required." 
                            ControlToValidate="editMerchGroupNameTextBox" SetFocusOnError="true" Display="Dynamic"></asp:RequiredFieldValidator>
                    </td>
                </tr>
            </table>
            <asp:Button ID="editMerchGroupButton" runat="server" Text="Update Merchandise Group" OnClick="editMerchGroupButton_Click" ValidationGroup="EditMerchGroup" CausesValidation="true" />
            <br />
        </ItemTemplate>
        <EmptyDataTemplate><b>No content found.</b></EmptyDataTemplate>
    </asp:FormView>
</asp:Panel>

CS档案:

protected void editMerchGroupDropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        editMerchGroupFormView1.DataBind();
        HiddenField myHiddenField = (HiddenField)FindControlRecursive(Page, "editMerchGroupHiddenField");
        MerchGroupDropDownList.DataBind();
        MerchGroupDropDownList.Items.FindByValue(myHiddenField.Value).Selected = true;
    }

0 个答案:

没有答案
相关问题