Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法在formview中设置dropdownlist中的选定项_C#_Asp.net - Fatal编程技术网

C# 无法在formview中设置dropdownlist中的选定项

C# 无法在formview中设置dropdownlist中的选定项,c#,asp.net,C#,Asp.net,问题是我无法设置dropdownlist所选项目。我有一个有两个下拉列表的面板。从第一个下拉列表(EditMerchGroupDropDownList 1)中选择产品组时,应根据产品组的CategoryID值设置第二个下拉列表(MerchGroupDropDownList)的选定值 类别:身份证、姓名 产品组:ID、名称、类别ID 即使我尝试固定值,它也不会更改所选项目 MerchGroupDropDownList.Items.FindByValue("2").Selected = true

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

  • 类别:身份证、姓名
  • 产品组:ID、名称、类别ID
即使我尝试固定值,它也不会更改所选项目

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>
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;
    }