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

如何在FormView绑定标签中正确显示解密文本?

问题解决方案

问题根源

后台通过ScriptManager.RegisterStartupScript传递包含+的参数时,+会被浏览器解析为空格,同时直接拼接字符串会破坏JS语法结构,导致语法错误。

解决方法

方法1:URL编码+解码处理

后台C#代码

对包含特殊字符的参数进行URL编码,避免+等字符破坏JS语法:

// 假设encryptedValue是带"+"的加密字符串
string encryptedValue = "abc+def123";
// 对参数进行URL编码,"+"会转为"%2B"
string encodedParam = HttpUtility.UrlEncode(encryptedValue);

// 拼接JS调用代码,确保参数用单引号包裹
string script = $"Show_Decrypt('param1_value', '{encodedParam}', 'param3_value');";
ScriptManager.RegisterStartupScript(this, GetType(), "DecryptScript", script, true);
前端JS函数

在函数内对参数进行解码,还原原始字符:

function Show_Decrypt(param1, param2, param3) {
    // 解码参数,还原"+"字符
    let decodedParam2 = decodeURIComponent(param2);
    
    // 执行解密逻辑
    let decryptedData = CryptoJS.AES.decrypt(decodedParam2, '你的AES密钥').toString(CryptoJS.enc.Utf8);
    
    // 将解密结果显示到FormView的标签中
    document.getElementById('FvActions_目标标签ID').textContent = decryptedData;
}

方法2:JSON序列化传递参数(更通用)

如果参数包含多种特殊字符(如引号、换行),用JSON序列化能彻底避免转义问题:

后台C#代码
// 封装参数为对象
var decryptParams = new {
    Param1 = "param1_value",
    EncryptedValue = "abc+def123",
    Param3 = "param3_value"
};

// 序列化为JSON字符串
string jsonParams = Newtonsoft.Json.JsonConvert.SerializeObject(decryptParams);
string script = $"Show_Decrypt({jsonParams});";
ScriptManager.RegisterStartupScript(this, GetType(), "DecryptScript", script, true);
前端JS函数
function Show_Decrypt(params) {
    // 直接使用序列化后的参数,无需额外解码
    let decryptedData = CryptoJS.AES.decrypt(params.EncryptedValue, '你的AES密钥').toString(CryptoJS.enc.Utf8);
    
    // 渲染到FormView标签
    document.getElementById('FvActions_目标标签ID').textContent = decryptedData;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 05:04:58