根据另一个值填充下拉列表的正确方法?

发布于 2024-10-08 10:25:39 字数 363 浏览 0 评论 0原文

我正在处理的一些 ASP.Net 代码遇到了一个小问题。为了简单起见,假设我有两个下拉列表,一个充满各种项目,另一个充满其他项目,但基于第一个下拉列表的选定项目。我目前将变量设置为隐藏输入,以检查第一个下拉列表中的项目是否已更改以及是否必须更改第二个下拉列表,但我还必须检查以确保是否有其他原因导致回发重新填充第二个下拉列表,以免丢失用户当前选择的内容。此检查还允许我确保当我离开页面时,会选择正确的值,而不是被回发重新填充所替换的值。

我是 ASP.Net/HTML/CSS/Javascript 等的新手。我知道虽然我所拥有的方法有效,但它距离正确的做事方式还差得很远。请帮助我纠正我的方法并找出解决当前问题的最佳方法。一些好的 ASP.Net 教程网站也会很好,因为我需要不断提高我的技能。

I'm running into a minor issue with some ASP.Net code I'm working on. For simplicity sake lets say I have two dropdownlists, one that is full of various items and another that is full of other items, but based off the selected item of the first dropdownlist. I currently set variables into hidden inputs to check to see if the item in the first dropdownlist has changed and if it has to change the second dropdownlist, but I'm also having to check to make sure that if something else causes a postback to NOT repopulate the second dropdownlist as to not lose what is currently selected by the user. This check also allows me to make sure that when I navigate away from the page, the correct values are selected and not something replaced by a postback repopulation.

I'm new to ASP.Net/HTML/CSS/Javascript and the like. I know while what I have works, it isn't even close to being the correct way to do things. Please help me correct my ways and figure out the BEST way to solve this current problem. Some good ASP.Net tutorial websites would be nice too as I need to keep improving my skills with it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妄想挽回 2024-10-15 10:25:39

尝试使用 autopostback true 选定索引更改事件:

<asp:DropDownList ID="DropDownList1" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" runat="server" />
<asp:DropDownList ID="DropDownList2" runat="server" />

并让该事件填充第二个下拉列表:

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
}

编辑
好吧,如果您需要它从第一页加载填充,您可以执行以下操作,或者更好地仍然创建一个从页面加载和选定索引更改调用的方法,该方法将填充您的下拉列表。

if (!Page.IsPostBack)
{
   DropDownList1_SelectedIndexChanged(DropDownList1, EventArgs.Empty);
}

Try autopostback true with the selected index changed event:

<asp:DropDownList ID="DropDownList1" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" runat="server" />
<asp:DropDownList ID="DropDownList2" runat="server" />

And have the event populate the second dropdown:

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
}

EDIT
Well if you need it to populate from first page load you could do the following, or better still make a method which is called from both page load and selected index changed which will populate your dropdown.

if (!Page.IsPostBack)
{
   DropDownList1_SelectedIndexChanged(DropDownList1, EventArgs.Empty);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文