ASP.NET Cookie用于记住表单中的输入值

时间:2016-05-12 09:54:38

标签: javascript c# asp.net cookies webforms

这是问题所在,我有一个表单,我需要实现一些能够记住特定文本框输入值的cookie,以便当用户输入他的名字时,填充其他方框,并记住他的姓氏等记忆值,电子邮件和电话号码。

这是我需要的形式:

enter image description here

这是asp.net default.aspx代码:

        Navn *
                                                            </td>
                                                            <td style="font-weight: bold" valign="top" colspan="3">
                                                                    <asp:TextBox ID="txtOrdererName" runat="server" ToolTip="Navn" Width="330px" TabIndex="7" OnTextChanged="txtOrdererName_TextChanged"></asp:TextBox>
                                                                    &nbsp;<asp:RequiredFieldValidator ID="rfvOrdererName" Display="Dynamic" ControlToValidate="txtOrdererName"
                                                                            runat="server" ErrorMessage="<img src=Images/validator_requiredInfo.gif class='UCvalidatorSymbol'>" />      
                                                            </td>
                                                    </tr>
                                                    <tr>
                                                            <td valign="top">
                                                                    &nbsp;
                                                            </td>
                                                            <td valign="top">
                                                            </td>
                                                            <td valign="top">
                                                            </td>
                                                            <td valign="top">
                                                                    &nbsp;</td>
                                                    </tr>
                                                    <tr>
                                                            <td style="font-weight: bold" valign="top">
                                                                    Telefon *
                                                            </td>
                                                            <td style="font-weight: bold" valign="top" colspan="3">
                                                                    <asp:TextBox ID="txtPhone" runat="server" ToolTip="Telefon" Width="330px" TabIndex="8"></asp:TextBox>
                                                                    &nbsp;<asp:RequiredFieldValidator ID="rfvPhone" Display="Dynamic" ControlToValidate="txtPhone"
                                                                            runat="server" ErrorMessage="<img src=Images/validator_requiredInfo.gif class='UCvalidatorSymbol'>" />      
                                                            </td>
                                                    </tr>
                                                    <tr>
                                                            <td valign="top">
                                                                    &nbsp;
                                                            </td>
                                                            <td style="font-weight: bold; color: red" valign="top" colspan="3">
                                                                    <%--<asp:RegularExpressionValidator ID="revPhone" runat="server" ControlToValidate="txtPhone"
                                                                            Display="Dynamic" ErrorMessage="Angiv en gyldig telefon (8 digit)" ToolTip="Angiv en gyldig telefon (8 digit)"
                                                                            ValidationExpression="^[0-9]{8}$" />--%>
                                                            </td>
                                                    </tr>
                                                    <tr>
                                                            <td style="font-weight: bold" valign="top">
                                                                    Mail *
                                                            </td>
                                                            <td style="font-weight: bold" valign="top" colspan="3">
                                                                    <asp:TextBox ID="txtOrdererEmail" runat="server" ToolTip="Mail" Width="330px" TabIndex="9"></asp:TextBox>
                                                                    &nbsp;<asp:RequiredFieldValidator ID="rfvOrdererEmail" Display="Dynamic" ControlToValidate="txtOrdererEmail"
                                                                            runat="server" ErrorMessage="<img src=Images/validator_requiredInfo.gif class='UCvalidatorSymbol'>" />
                                                            </td>

我创建了一些javascript Cookie,但我在addEventListener的控制台中收到错误,它是:
未捕获的TypeError:无法读取属性'addEventListener'为null

这是我的javascript:

`window.onload = function () {
    document.getElementById('btnProceed_Click').addEventListener('click', createCookies);




    // jsfiddle runs code after page loads -- in other environments
    // run this on page load
    document.getElementById("txtOrdererName").value = getCookie("txtOrdererName");
    document.getElementById("txtPhone").value = getCookie("txtPhone");
    document.getElementById("txtOrdererEmail").value = getCookie("txtOrdererEmail");

    // Read values from textboxes and create cookies
    function createCookies(evt) {
        evt.preventDefault();

        setCookie('txtOrdererName', document.getElementById("txtOrdererName").value);
        setCookie('txtPhone', document.getElementById("txtPhone").value);
        setCookie('txtOrdererEmail', document.getElementById("txtOrdererEmail").value);
    }

    // set the cookie lifetime
    function setCookie(cname, cvalue, exdays) {
        var d = new Date();
        exdays = exdays || 365; // Set to 1 year - 365 days
        d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
        var expires = "expires=" + d.toUTCString();
        document.cookie = cname + "=" + cvalue + "; " + expires;
    }

    function getCookie(cname) {
        var name = cname + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ') c = c.substring(1);
            if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
        }
        return "";
    }
}`

任何人都可以知道这是否有效以及问题是什么?

0 个答案:

没有答案