You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET中DropdownList点击选项无法跳转新页面的问题求助

ASP.NET DropDownList点击选项跳转页面问题解决

问题现象

在ASP.NET中尝试实现点击DropdownList选项跳转到新页面,点击“More...”选项时页面仅刷新,无报错但无法完成跳转。

原代码

前端ASPX代码

<tr>
    <th> <input type="checkbox" id="" name="number" value="No" /></th>
    <th> 1 </th>
    <th>
        <asp:DropDownList ID="pd" runat="server" Class="form-pd" AutoPostBack="true" OnTextChanged="Redirect">
            <asp:ListItem Value="">Calculus</asp:ListItem>
            <asp:ListItem Value="">More....</asp:ListItem> 
        </asp:DropDownList> 
    </th>
    <th> </th>
    <th> </th>
    <th> </th>
    <th> </th>
    <th> </th> 
</tr>

后台C#代码

protected void Redirect(object sender, EventArgs e)
{
    if (string.IsNullOrEmpty(pd.SelectedItem.Text))
    {
        Response.Redirect("~/Form2.aspx?value=" + pd.SelectedItem.Text);
    } 
}

问题原因

后台代码的判断条件逻辑错误:string.IsNullOrEmpty(pd.SelectedItem.Text)是判断选中项的文本为空,但“More...”的文本是有内容的,因此该条件永远不成立,Response.Redirect代码根本不会执行,页面仅触发回发刷新,不会跳转。

修正方案

方案1:直接判断选中项文本

修改后台代码,将判断条件改为匹配“More...”文本:

protected void Redirect(object sender, EventArgs e)
{
    // 匹配目标选项文本
    if (pd.SelectedItem.Text == "More....")
    {
        // 对参数进行URL编码,避免特殊字符问题
        Response.Redirect("~/Form2.aspx?value=" + Server.UrlEncode(pd.SelectedItem.Text));
    }
}

方案2:通过Value属性判断(更推荐)

给ListItem设置有意义的Value值,通过Value判断更稳定,避免文本修改导致逻辑失效:

修改前端代码

<asp:DropDownList ID="pd" runat="server" Class="form-pd" AutoPostBack="true" OnTextChanged="Redirect">
    <asp:ListItem Value="calculus">Calculus</asp:ListItem>
    <asp:ListItem Value="more">More....</asp:ListItem> 
</asp:DropDownList>

修改后台代码

protected void Redirect(object sender, EventArgs e)
{
    if (pd.SelectedValue == "more")
    {
        Response.Redirect("~/Form2.aspx?value=" + Server.UrlEncode(pd.SelectedValue));
    }
}

内容的提问来源于stack exchange,提问作者kimk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 13:25:50