下拉列表中所选值无效

本文关键字:无效 下拉列表 | 更新日期: 2023-09-27 18:11:32

我一直在一个项目上工作,面对一个错误,因为我有两个文本框和一个下拉框,当我在其selecting_indexchanged下拉框得到填充的文本框之一,正在发生的事情是当我点击重置按钮重置下拉文本框为空白,并将索引值降为0,它显示为错误

"下拉列表有一个无效的SelectedIndex,因为它不存在于项目列表中。参数名称:value "

<td class="style4">
            <asp:Label ID="lbl_bookname" runat="server" Text="Book Name" 
                ForeColor="Black"></asp:Label>
        </td>
        <asp:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server"  TargetControlID="txt_bookname" MinimumPrefixLength="1" EnableCaching="true" CompletionSetCount="1" CompletionInterval="1000" ServiceMethod="getbookname">
        </asp:AutoCompleteExtender>
        <td class="style4">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
                        </asp:ScriptManager>
            <asp:TextBox ID="txt_bookname" runat="server" ValidationGroup="a" OnTextChanged="editiondrpfill_TextChanged" AutoPostBack="true"></asp:TextBox>
             <asp:AutoCompleteExtender ID="AutoCompleteExtender2" runat="server" TargetControlID="txt_bookname" MinimumPrefixLength="1" EnableCaching="true"  CompletionSetCount="1" CompletionInterval="1000" ServiceMethod="getbookname">
        </asp:AutoCompleteExtender> 
        </td>
        <td class="style4">
            <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" 
                ErrorMessage="*" ForeColor="Red" ControlToValidate="txt_bookname" 
                ValidationGroup="a"></asp:RequiredFieldValidator>
        </td>
        <td class="style4">
            <asp:Label ID="lbl_condition" runat="server" ForeColor="Black" Text="Condition"></asp:Label>
            </td>
        <td class="style4">
            <asp:TextBox ID="txt_condition" runat="server" BackColor="White" 
                ReadOnly="True" Width="120px"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server" 
                ControlToValidate="txt_condition" ErrorMessage="*" ForeColor="Red" 
                ValidationGroup="a"></asp:RequiredFieldValidator>
            </td>
        <td>
            &nbsp;</td>
    </tr>
    <tr>
        <td class="style4">
            <asp:Label ID="lbl_edition" runat="server" ForeColor="Black" Text="Edition"></asp:Label>
        </td>
        <td class="style4">
            <asp:DropDownList ID="drp_edition" runat="server" ValidationGroup="a" 
                Width="120px" onselectedindexchanged="drp_edition_SelectedIndexChanged" 
                AutoPostBack="true" TabIndex="0">
            </asp:DropDownList>
        </td>
        <td class="style4">
            <asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" 
                ControlToValidate="drp_edition" ErrorMessage="*" ForeColor="Red" 
                ValidationGroup="a"></asp:RequiredFieldValidator>
        </td>

CS下拉框填充代码

  protected void editiondrpfill_TextChanged(object sender, EventArgs e)
{
   sql = "select bt.booktwo_id,bt.edition,b.bookname from library_book b , library_booktwo bt where bookname ='" + txt_bookname.Text+ "' and bt.book_id=b.book_id";
   ds = obj.openDataset(sql, Session["SCHOOLCODE"].ToString());
   drp_edition.Items.Clear();
    ListItem li = new ListItem();
    li.Text = "Select the value";
     li.Value = "0";
    drp_edition.Items.Add(li);
    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
    {
        li = new ListItem();
         li.Text= ds.Tables[0].Rows[i]["edition"].ToString();
         li.Value = ds.Tables[0].Rows[i]["booktwo_id"].ToString();
        drp_edition.Items.Add(li);
    }
}

cs重置按钮事件代码

  protected void btn_reset_Click(object sender, EventArgs e)
{
    txt_bookname.Text = "";
    txt_condition.Text = "";
    txt_member_id.Text = "";
    txt_nameofstudent.Text = "";
    txt_quantity.Text = "";
    drp_edition.SelectedIndex = 0;
    drp_isbn.SelectedIndex = 0;
}

下拉列表中所选值无效

做一件事在你的aspx.cs页面添加一个listtitem值为0的下拉菜单,如

<asp:DropDownList ID="drp_edition" runat="server" ValidationGroup="a" 
                Width="120px" onselectedindexchanged="drp_edition_SelectedIndexChanged" 
                AutoPostBack="true" TabIndex="0">
                <asp:ListItem Value="0" Text="Select" ></asp:ListItem>
            </asp:DropDownList>

它会解决你的问题。

为下拉列表"drp_edition"放置默认列表项。因为,在reset fn中,您被分配了 selecteindex =0。在这种情况下,应该有一些列表项。

<td class="style4">
<asp:DropDownList ID="drp_edition" runat="server" ValidationGroup="a" Width="120px"      
onselectedindexchanged="drp_edition_SelectedIndexChanged" AutoPostBack="true" TabIndex="0">
<asp:ListItem Text="--Select--" Value="0" />
</asp:DropDownList>
</td>
<td class="style4">
<asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server"           
ControlToValidate="drp_edition" ErrorMessage="*" ForeColor="Red" InitialValue="0"    
ValidationGroup="a"></asp:RequiredFieldValidator>
</td>