【问题标题】:How can I pass Data in URL如何在 URL 中传递数据
【发布时间】:2011-10-19 11:17:21
【问题描述】:

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

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

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

现在我有一个 Javascript 函数,但我认为通过 Code-Behind 会更好

    $('.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;
}

谁能帮帮我?

【问题讨论】:

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


    【解决方案1】:

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

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

    这会给你这个网址:

    "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() 会抛出异常。

    【讨论】:

    • 如果我要传递 2 个数据?例如,电子邮件和 ClienteID ?我怎样才能分开?
    【解决方案2】:

    您需要对 url 字符串进行编码,然后才能轻松传递数据。

    例子:

       Server.URLEncode("http://www.w3schools.com?mykey=datavalue");
    

    【讨论】:

    • 你不应该编码整个字符串,而应该只编码参数的值,即"http://www.w3schools.com?mykey=" + Server.URLEncode("datavalue");
    • @Jan Aagaard - 这只是一个例子......展示方法的要点......并且该方法的使用取决于您的需要
    【解决方案3】:

    您必须对关键字符进行编码。查看here 以了解字符的描述。您的网址必须如下所示:

    Client.aspx?Email=teste%40gmail.com
    

    【讨论】:

      【解决方案4】:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-07-05
        • 2015-04-03
        • 1970-01-01
        • 2012-06-28
        • 1970-01-01
        • 2012-05-10
        • 2019-07-11
        相关资源
        最近更新 更多