gpt4 book ai didi

c# - 下拉列表中的选定值无效

转载 作者:行者123 更新时间:2023-11-29 08:32:10 25 4
gpt4 key购买 nike

我一直在开发一个项目,并面临一个错误,因为当我在其选择索引上填充其中一个文本框时,我有两个文本框和一个下拉列表,更改了下拉列表被填充,发生的情况是当我单击重置按钮来重置下拉文本框为空,下拉索引值为 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;
}

最佳答案

在您的 aspx.cs 页面上做一件事,添加一个值为 0 的 ListItem 到下拉列表

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

它会解决你的问题。

希望它能起作用。

关于c# - 下拉列表中的选定值无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16294385/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com