c# - 如何在 URL 中传递数据

标签 c# javascript asp.net post query-string

如何通过我的 URL 传递数据?

我需要传递,电子邮件,client_id 等。

Client.aspx?Email='teste@gmail.com'

现在我有一个 Javascript 函数,但我认为通过代码隐藏会更好

    $('.btIncluirAtendimento').live('click', function () {
        idCliente = $(this).attr('id').replace('cad_', '');
        popup = openPopup('../Cliente/AtendimentoNew.aspx?Cliente_Id=' + idCliente, 'IncluirAtendimento', 'toolbar=no,directories=no,status=no,menubar=no, scrollbars=yes,resizable=no', '720', '600', 'true');
    });


function openPopup(theURL, winName, features, myWidth, myHeight, isCenter) {
    if (window.screen) if (isCenter) if (isCenter == "true") {
        var myLeft = (screen.width - myWidth) / 2;
        var myTop = (screen.height - myHeight) / 2;
        features += (features != '') ? ',' : '';
        features += ',left=' + myLeft + ',top=' + myTop;
    }
    popup = window.open(theURL, winName, features + ((features != '') ? ',' : '') + 'width=' + myWidth + ',height=' + myHeight);
    return popup;
}

谁能帮帮我?

最佳答案

您走在正确的轨道上。首先,像这样对值进行 URL 编码。 (对数字进行编码不会做任何事情,因此如果所有客户端 ID 都是数字,则可以省略第二个 UrlEncode。)

string url = String.Format("Client.aspx?Email={0}&ClientId={1}",
    HttpUtility.UrlEncode("test@gmail.com"),
    HttpUtility.UrlEncode("1234"));

这会给你这个 url:

"Client.aspx?Email=test%40gmail.com&ClientId=1234"

您可以使用以下代码行读取 Client.aspx.cs 中的值:

string emailAddress = Request.QueryString["Email"];
int clientId = Int32.Parse(Request.QueryString["ClientId"]);

记得检查参数。如果 ClientId 不是数字,Int32.Parse() 将抛出异常。

关于c# - 如何在 URL 中传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7820502/

相关文章:

c# - 异步方法中的警告消息说它缺少等待运算符

c# - ASP.NET MVC5 EF6 : Given multiplicity constraints, 对应的 'User_AppUser_Target' 也必须处于 'Deleted' 状态。存储库模式

C# MongoDB 驱动程序序列化 - 使用二进制文件

javascript - 在循环中用迭代器匹配模式

javascript - Ionic 2 **panup** 和 **pandown** 事件不起作用

c# - 在运行时更改 CSS 文件(主题)(ASP.NET)

c# - 托盘图标的 Windows 10 任务栏颜色检测

c# - Google .NET API - 除了 FileDataStore 之外还有其他数据存储吗?

c# - 在更新面板内的回发上保持可滚动的 gridview 位置

javascript - IE6-7 JavaScript