vb.net 当用户从 ASP.NET 的下拉列表中选择时重定向到新页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16927270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:50:55  来源:igfitidea点击:

Redirect to new page when user select from a dropdown list in ASP.NET

asp.netvb.netdrop-down-menu

提问by SteveCode

I created an asp.net web application (VB) with a dropdownlist that have lists of url to pages on the server. I am new to asp and VB. I have research different forums for solution and decided to ask for a specific solution to my problem.

我创建了一个带有下拉列表的 asp.net web 应用程序 (VB),其中包含服务器上页面的 url 列表。我是 ASP 和 VB 的新手。我研究了不同的解决方案论坛,并决定为我的问题寻求特定的解决方案。

Break down. - I have a fully built page - this page gets archive every two hrs to an archive folder(using a vbs) - An XML file is generated with the file name and url (using a VBS) - XMl is the datasource for the DDL.

分解。- 我有一个完全构建的页面 - 此页面每两小时将存档存档到一个存档文件夹(使用 vbs) - 生成一个带有文件名和 url 的 XML 文件(使用 VBS) - XMl 是 DDL 的数据源。

What I want to accomplish is, when the user click an item from the DDL, they should be directed to that page.

我想要完成的是,当用户单击 DDL 中的一个项目时,他们应该被定向到该页面。

After follow some of the suggestion from other forum and this one, nothing seems to work.

在遵循其他论坛和这个论坛的一些建议之后,似乎没有任何效果。

Once we dive into this, we will get some better understanding of any confusion.

一旦我们深入了解这一点,我们将对任何混淆有更好的理解。

  • The code-behind is VB, so i will prefered that language.
  • 代码隐藏是 VB,所以我更喜欢那种语言。

ASPX Page

ASPX 页面

enter code here

<%@ Page Title="Home" Language="vb" MasterPageFile="~/Site.Master"     AutoEventWireup="false"CodeBehind="Default.aspx.vb" Inherits="Status._Default" %>     <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"></asp:Content><asp:Content ID="BodyContent"runat="server" ContentPlaceHolderID="MainContent"></asp:Content>


<asp:XmlDataSource ID="statsXML" 
    runat="server" DataFile="~/Archive/Stats.xml" 
    XPath="Elements/Element" /> 
<asp:DropDownList ID="DropDownList1" runat="server" 
     DataSourceID="statsXML"
     DataTextField="Name" 
     DataValueField="Value" 
     AutoPostBack="True" 
     CssClass="rightCol"  />
<br />
<p>

    <asp:Table ID="Table1" runat="server" GridLines="Horizontal" Width="100%">
        <asp:TableRow BorderWidth="1" BorderStyle="Solid" Font-Size="12">
            <asp:TableCell HorizontalAlign="Center" Text="Text here" BorderStyle="Solid" BorderWidth="0"
                ForeColor="White" BackColor="#006699"></asp:TableCell>
        </asp:TableRow>
        <asp:TableRow BorderWidth="1" BorderStyle="Solid" Font-Size="12">
            <asp:TableCell HorizontalAlign="Center" Text="Text here" BorderStyle="Solid"
                BorderWidth="0" ForeColor="White" BackColor="#006699"></asp:TableCell>
        </asp:TableRow>
    </asp:Table>
    <br />
</p>
<asp:Table ID="Table2" runat="server" GridLines="both" Width="100%" BorderColor="Black">
    <asp:TableRow BorderWidth="1" BorderStyle="Solid" Font-Size="12" BorderColor="Black">
        <asp:TableCell Width="50%" HorizontalAlign="Center" Text="Enviroment" BorderStyle="Solid" BorderWidth="1"
            ForeColor="White" BackColor="#006699" BorderColor="Black"></asp:TableCell>
        <asp:TableCell Width="50%" HorizontalAlign="Center" Text="State" BorderStyle="Solid"
            BorderWidth="1" ForeColor="White" BackColor="#006699" BorderColor="Black"></asp:TableCell>
    </asp:TableRow>
        </asp:Table>`

Code-Behind

代码隐藏

Public Class webform
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'If Not Page.IsPostBack Then

        'End If
        'If Page.IsPostBack Then
        '    ' Response.Redirect(Me.DropDownList1.SelectedValue)
        ' End If
    End Sub

    Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, 
                                                     ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged    

        Response.Redirect(DropDownList1.SelectedItem.Value)

    End Sub

End Class

回答by mclark1129

What you'll want to do is to first set AutoPostbackto Trueon your DropDownList.

什么,你会想要做的是第一套AutoPostbackTrueDropDownList

<asp:DropDownList ID="myDropDownlist" runat="server" AutoPostback="True" />

After that you should be able to handle the SelectedIndexChangedevent for the DropDownListin your code-behind

之后,您应该能够处理代码隐藏中的SelectedIndexChanged事件DropDownList

Protected Sub myDropDownList_SelectedIndexChanged(sender As Object, e As EventArgs) Handles myDropDownList.SelectedIndexChanged

   ' Lookup the selected item and redirect here
   ' (This assumes that you've bound your URL to redirect to the value of the item,
   ' and not the display text.  Use SelectedItem.Text if the URL is being displayed
   Response.Redirect(myDropDownList.SelectedItem.Value)

End Sub

回答by thunderbird

Try This

尝试这个

 Public Class WebForm1
        Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Private Sub DropDownList1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DropDownList1.SelectedIndexChanged
        Response.Redirect(DropDownList1.SelectedItem.Text)
    End Sub
 End Class