链接到选项下拉菜单
您可以从另一个页面链接到选项下拉框中的特定值吗?
换句话说,假设我在第 1 页,我想将链接锚定到第 2 页,该页面有一个选项下拉框,其中包含 3 个不同的值。 假设默认情况下,当您转到第 2 页时,下拉选项框显示值 1。
是否可以链接到第 2 页并即时更改该选项框的值? 而当您单击第 1 页上的链接时,它将自动显示值 3,而不是第 2 页上的 1。
Can you link to a specific value in an options drop down box from another page?
In other words, let's say that I'm on page 1 and I want to anchor link to page 2 that has an options drop down box with 3 different values in it. Let's say by default when you go to page 2, the drop down option box is showing value 1.
Is it possible to link to page 2 and change the value of that option box on the fly? Whereas when you click the link on page 1 it will automatically show value 3 instead of 1 on page 2.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这当然是可能的。 您可以在查询字符串中传递一个标志。 因此,在 page1 上您有一个指向 page2 的链接,例如“page2.aspx?option=3”。 然后,在 page2 的
PageLoad
方法中,只需从查询字符串 (Request.QueryString["option"]
) 中读取该值并设置DropDownList< 的所选项目/code> 适当地。
您将在第 1 页中...
在第 2 页的代码隐藏中,基于 Al 的示例...
This is certainly possible. You can pass a flag in your querystring. So, on page1 you have a link to page2 like "page2.aspx?option=3". Then, in page2's
PageLoad
method, simply read that value from the querystring (Request.QueryString["option"]
) and set the selected item of theDropDownList
appropriately.One page1 you would have...
In the codebehind of page2, based on Al's example...
乔恩·弗里兰的回答基本上就是我的做法。 您可能希望将用于设置列表索引的代码放在 Page_Load 函数内的代码隐藏类中。
您还可以保存要在 ASP.Net 会话中设置的选项的值,但如果您开始让用户在网站上跳来跳去,这会变得有点棘手。 他们可能会回到第 2 页,但仍然将会话变量设置为意外的值。 此外,如果用户一段时间不活动或服务器重置,您可能会遇到会话被删除的问题。 从好的方面来说,如果将其放入 Session 对象中,则可以在页面之间来回移动,并随时保存所需的所有数据。
如果您想查看示例,请尝试以下操作:
您希望将代码放入 !IsPostBack 部分,以便它仅在用户第一次定向到该页面时运行。
Jon Freeland's answer is basically the way I would do it. You probably want to put the code to set the list index in the codebehind class inside the Page_Load function.
You could also save the value of the option to set in the ASP.Net session, but that gets a little trickier if you start letting the user bounce around the site. They may come back to page 2 and still have the session variable set to something unexpected. Also, you may run into trouble with the Session getting deleted if the user is inactive for a while or if the server is reset. On the plus side, if you put it in the Session object, you can move back and forth between your pages and keep all the data you need right handy.
If you want to see a sample, try something like:
You want to put the code inside the !IsPostBack section so that it will only run the first time the user is directed to the page.