0

我一直在做一个项目并面临一个错误,因为我有两个文本框和一个下拉列表,当我在其 selection_index 上填充其中一个文本框时,下拉列表被填充,当我单击重置按钮以重置下拉文本框时发生了什么为空白并将下拉索引值设置为0,它显示为错误

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

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

2 回答 2

2

在您的 aspx.cs 页面上做一件事,将 ListItem 添加到值为 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>

它会解决你的问题。

希望它有效。

于 2013-04-30T07:29:33.233 回答
0

放置下拉菜单“drp_edition”的默认列表项。因为,在重置 fn 中为您分配了selectedindex=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>
于 2013-04-30T07:28:09.107 回答