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

ASP.NET中根据下拉选择动态修改收件人邮箱(避免页面刷新)

解决方案

一、前端无刷新实现方案(jQuery/JavaScript)

启用AutoPostBack=true会触发页面回发导致弹窗关闭,直接在前端处理下拉选择事件是最优解,具体步骤如下:

1. 把后端配置渲染到前端

在ASP.NET页面的<script>标签中,将Web.config里的邮箱配置输出为JavaScript对象:

// 从后端配置获取各塔楼对应的邮箱列表
var towerEmails = {
    "AO": "<%= ConfigurationManager.AppSettings["ToAO"] %>",
    "IO": "<%= ConfigurationManager.AppSettings["ToIO"] %>",
    "MOD": "<%= ConfigurationManager.AppSettings["ToMOD"] %>",
    "SD": "<%= ConfigurationManager.AppSettings["ToSD"] %>",
    "Security": "<%= ConfigurationManager.AppSettings["ToSecurity"] %>"
};

2. 监听下拉框选择事件

用jQuery监听下拉框的change事件,根据选中值直接设置收件人文本框内容:

$(function() {
    // ASP.NET会自动生成控件ID前缀,用ClientID获取页面真实ID
    $("#<%= txtTowerValue.ClientID %>").on("change", function() {
        var selectedTower = $(this).val();
        var emailInput = $("#<%= txtNotifTo.ClientID %>");
        
        // 根据选中塔楼匹配邮箱,默认清空文本框
        if (towerEmails[selectedTower]) {
            emailInput.val(towerEmails[selectedTower]);
        } else {
            emailInput.val("");
        }
    });
});

3. 调整DropDownList控件属性

移除AutoPostBack="true"和onselectedIndexchanged="Selection_Change",避免触发后端回发:

<asp:DropDownList ID="txtTowerValue" runat="server" SkinID="EmailTextbox" 
    Font-Size="14px" Width="580px" Height="30px" BorderStyle="None">
    <asp:ListItem Value="--Please Select--">--Please Select--</asp:ListItem>
    <asp:ListItem Value="AO">AO</asp:ListItem>
    <asp:ListItem Value="MOD">MOD</asp:ListItem>
    <asp:ListItem Value="IO">IO</asp:ListItem>
    <asp:ListItem Value="SD">SD</asp:ListItem>
    <asp:ListItem Value="Security">Security</asp:ListItem>
</asp:DropDownList>

二、后端代码优化建议(若后续需保留后端逻辑)

如果之后需要保留后端处理逻辑,可优化原有Selection_Change方法,减少冗余判断:

1. 使用字典映射简化代码

protected void Selection_Change(Object sender, EventArgs e)
{
    var towerEmailMap = new Dictionary<string, string>
    {
        {"AO", ConfigurationManager.AppSettings["ToAO"]},
        {"IO", ConfigurationManager.AppSettings["ToIO"]},
        {"MOD", ConfigurationManager.AppSettings["ToMOD"]},
        {"SD", ConfigurationManager.AppSettings["ToSD"]},
        {"Security", ConfigurationManager.AppSettings["ToSecurity"]}
    };

    if (towerEmailMap.TryGetValue(txtTowerValue.SelectedItem.Text, out string email))
    {
        txtNotifTo.Text = email;
    }
    else
    {
        txtNotifTo.Text = "";
    }
}

2. 或使用Switch语句优化

protected void Selection_Change(Object sender, EventArgs e)
{
    string selectedTower = txtTowerValue.SelectedItem.Text;
    string email = string.Empty;

    switch (selectedTower)
    {
        case "AO":
            email = ConfigurationManager.AppSettings["ToAO"];
            break;
        case "IO":
            email = ConfigurationManager.AppSettings["ToIO"];
            break;
        case "MOD":
            email = ConfigurationManager.AppSettings["ToMOD"];
            break;
        case "SD":
            email = ConfigurationManager.AppSettings["ToSD"];
            break;
        case "Security":
            email = ConfigurationManager.AppSettings["ToSecurity"];
            break;
    }

    txtNotifTo.Text = email;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 08:47:23