2

长期以来一直使用 PHP,但现在正在使用 ASP.NET 制作网站。我正在尝试创建一个基本的用户注册表单,但是每当我提交表单时,所有数据都显示为空!此外,tagsinput 方法似乎不起作用。

这是一个基本形式,它位于内容占位符中。母版页中的整个正文被包围在一个<form runat="server">

<script type="text/javascript">
    $(document).ready(function () {
        //$("[id$=pick_user_type]").hide();

        $("[id$=UserTypeList]").click(function (e) { //second step of registration

            $('[id$=UserTypeList] input:checked').each(function () {

                //artist
                if ((this.value).toLowerCase().indexOf("artist") >= 0) {
                    $('#whatinstruments').show();
                    $('#instrumentsplayedtags').prop('disabled', false);
                }

                //producer
                if ((this.value).toLowerCase().indexOf("producer") >= 0) {
                    $('#whatrecordingplatforms').show();
                    $('#recordingplatformstags').prop('disabled', false);
                }
            });

            $('[id$=UserTypeList] input:not(:checked)').each(function () { //unchecked
                //artist
                if ((this.value).toLowerCase().indexOf("artist") >= 0) {
                    $('#whatinstruments').hide();
                    $('#instrumentsplayedtags').prop('disabled', true);
                }

                //producer
                if ((this.value).toLowerCase().indexOf("producer") >= 0) {
                    $('#whatrecordingplatforms').hide();
                    $('#recordingplatformstags').prop('disabled', true);
                }
            });
        });

        $('#artistslikedtags').tagsInput({ //tags for listener 
            width: 'auto'
        });

        $('#instrumentsplayedtags').tagsInput({ //tags for artist
            width: 'auto'
        });



        $('#recordingplatformstags').tagsInput({ //tags for producer 
            width: 'auto'
        });



    });

    function onNextClick() {
        //alert(Page_ClientValidate("UserInformation"));
        //if (Page_ClientValidate("UserInformation")) {
            $("[id$=registration_div]").hide('slide', { direction: 'left' }, 1000);
            $("[id$=pick_user_type]").css('position', 'absolute');
            $("[id$=pick_user_type]").show('slide', { direction: 'right' }, 1000);
        //}
    }
    function onLeftClick() {
        //alert(Page_ClientValidate("UserInformation"));
        //if (Page_ClientValidate("UserInformation")) {
        $("[id$=pick_user_type]").hide('slide', { direction: 'right' }, 1000);
        $("[id$=registration_div]").show('slide', { direction: 'left' }, 1000);

        //}
    }
</script>


<div id="registration_div">
    <table id="registration_table">
            <td>
                <div id="registration">

                    <table id="user_information_table">
                        <tr>
                            <%--first name and last name--%>
                            <td>
                                <asp:TextBox ID="FirstName" runat="server" CssClass="roundedcorner"></asp:TextBox>
                                <ajaxToolkit:TextBoxWatermarkExtender ID="WatermarkFirstName" runat="server" TargetControlID="FirstName" WatermarkText="First Name" WatermarkCssClass="watermarked roundedcorner" />
                                <asp:RequiredFieldValidator ID="FirstNameRequired" runat="server" ControlToValidate="FirstName" ErrorMessage="First Name is required." ToolTip="First Name is required." ValidationGroup="UserInformation"><font color="red">*</font></asp:RequiredFieldValidator>

                                <ajaxToolkit:TextBoxWatermarkExtender ID="WatermarkLastName" runat="server" TargetControlID="LastName" WatermarkText="Last Name" WatermarkCssClass="watermarked roundedcorner" />
                                <asp:TextBox ID="LastName" runat="server" CssClass="roundedcorner"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="LastNameRequired" runat="server" ControlToValidate="LastName" ErrorMessage="Last Name is required." ToolTip="Last Name is required." ValidationGroup="UserInformation"><font color="red">*</font></asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <%--username--%>
                            <td>
                                <ajaxToolkit:TextBoxWatermarkExtender ID="WatermarkUsername" runat="server" TargetControlID="Username" WatermarkText="Username" WatermarkCssClass="watermarked roundedcorner" />
                                <asp:TextBox ID="UserName" runat="server" CssClass="roundedcorner"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName" ErrorMessage="User Name is required." ToolTip="User Name is required." ValidationGroup="UserInformation"><font color="red">*</font></asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <%--password and confirm pass--%>
                            <td>
                                <ajaxToolkit:TextBoxWatermarkExtender ID="WatermarkPassword" runat="server" TargetControlID="Password" WatermarkText="Password" WatermarkCssClass="watermarked roundedcorner" />
                                <asp:TextBox ID="Password" runat="server" TextMode="Password" CssClass="roundedcorner"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password" ErrorMessage="Password is required." ToolTip="Password is required." ValidationGroup="UserInformation"><font color="red" >*</font></asp:RequiredFieldValidator>


                                <ajaxToolkit:TextBoxWatermarkExtender ID="WatermarkConfirmPassword" runat="server" TargetControlID="ConfirmPassword" WatermarkText="Confirm Password" WatermarkCssClass="watermarked roundedcorner" />
                                <asp:TextBox ID="ConfirmPassword" runat="server" CssClass="roundedcorner" TextMode="Password"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="ConfirmPasswordRequired" runat="server" ControlToValidate="ConfirmPassword" ErrorMessage="Confirm Password is required." ToolTip="Confirm Password is required." ValidationGroup="UserInformation"><font color="red">*</font></asp:RequiredFieldValidator>

                            </td>
                        </tr>
                        <%--email and confirm email--%>
                        <tr>

                            <td>
                                <ajaxToolkit:TextBoxWatermarkExtender ID="WatermarkEmail" runat="server" TargetControlID="Email" WatermarkText="Email" WatermarkCssClass="watermarked roundedcorner" />
                                <asp:TextBox ID="Email" runat="server" Columns="48" CssClass="roundedcorner"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="EmailRequired" runat="server" ControlToValidate="Email" ErrorMessage="E-mail is required." ToolTip="E-mail is required." ValidationGroup="UserInformation"><font color="red">*</font></asp:RequiredFieldValidator>
                                <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ForeColor="Red" ControlToValidate="Email" ValidationGroup="UserInformation" ErrorMessage="Invalid Email Format"></asp:RegularExpressionValidator>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <ajaxToolkit:TextBoxWatermarkExtender ID="WatermarkConfirmEmail" runat="server" TargetControlID="ConfirmEmail" WatermarkText="Confirm Email" WatermarkCssClass="watermarked roundedcorner" />
                                <asp:TextBox ID="ConfirmEmail" runat="server" Columns="48" CssClass="roundedcorner"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="ConfirmEmail" ErrorMessage="Confirm E-mail is required." ToolTip="Confirm E-mail is required." ValidationGroup="UserInformation"><font color="red">*</font></asp:RequiredFieldValidator>
                                <asp:RegularExpressionValidator ID="regexEmailValid" runat="server" ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ForeColor="red" ControlToValidate="ConfirmEmail" ValidationGroup="UserInformation" ErrorMessage="Invalid Email Format"></asp:RegularExpressionValidator>

                            </td>
                        </tr>
                        <tr>
                            <%-- location --%>
                            <td>
                                <ajaxToolkit:TextBoxWatermarkExtender ID="WatermarkLocation" runat="server" TargetControlID="Location" WatermarkText="Location" WatermarkCssClass="watermarked roundedcorner" />
                                <asp:TextBox ID="Location" runat="server" Columns="48" CssClass="roundedcorner"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="LocationValidator" runat="server" ControlToValidate="Location" ErrorMessage="Location is required." ToolTip="Location is required." ValidationGroup="UserInformation"><font color="red">*</font></asp:RequiredFieldValidator>

                            </td>
                        </tr>
                        <tr>
                            <%-- birthdate --%>
                            <td>
                                <ajaxToolkit:TextBoxWatermarkExtender ID="WatermarkBirthdate" runat="server" TargetControlID="Birthdate" WatermarkText="Birthdate (mm/dd/yyyy)" WatermarkCssClass="watermarked roundedcorner" />
                                <asp:TextBox ID="Birthdate" runat="server" CssClass="roundedcorner"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="BirthdayRequired" runat="server" ControlToValidate="Birthdate" ErrorMessage="Birthdate is required." ToolTip="Security question is required." ValidationGroup="UserInformation"><font color="red">*</font></asp:RequiredFieldValidator>
                                <asp:CompareValidator ErrorMessage="(mm/dd/yyyy)" Display="Dynamic" ID="valcDate" ControlToValidate="Birthdate" Operator="DataTypeCheck" Type="Date" ForeColor="red" runat="server" ValidationGroup="UserInformation"></asp:CompareValidator>
                                <asp:RangeValidator ID="valrDate" runat="server" ControlToValidate="Birthdate" MinimumValue="12/31/1950" MaximumValue="1/1/2100" Type="Date" Text="Invalid Date" ForeColor="Red" Display="Dynamic" ValidationGroup="UserInformation" />
                            </td>
                        </tr>
                        <tr>
                            <%-- gender --%>
                            <td>
                                <%--<ajaxToolkit:TextBoxWatermarkExtender ID="WatermarkGender" runat="server" TargetControlID="Gender" WatermarkText="Birthdate (mm/dd/yyyy)" WatermarkCssClass="watermarked" />--%>
                                <asp:DropDownList ID="Gender" runat="server" CssClass="roundedcorner">
                                    <asp:ListItem Value="Gender"></asp:ListItem>
                                    <asp:ListItem Value="Male"></asp:ListItem>
                                    <asp:ListItem Value="Female"></asp:ListItem>
                                </asp:DropDownList>
                                <asp:RequiredFieldValidator InitialValue="Gender" ID="GenderRequired" runat="server" ControlToValidate="Gender" ErrorMessage="Gender is required." ToolTip="Gender is required." ValidationGroup="UserInformation"><font color="red">*</font></asp:RequiredFieldValidator>
                            </td>
                        </tr>

                        <tr>
                            <td style="color: Red;">
                                <asp:Literal ID="ErrorMessage" runat="server" EnableViewState="False"></asp:Literal>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <asp:CompareValidator ID="comparePasswords" runat="server" ControlToCompare="Password" ControlToValidate="ConfirmPassword" ErrorMessage="Your passwords do not match!" ForeColor="red" Display="Dynamic" ValidationGroup="UserInformation" />
                                <asp:CompareValidator ID="EmailComparitor" runat="server" ControlToCompare="Email" ControlToValidate="ConfirmEmail" ErrorMessage="Your emails do not match!" ForeColor="Red" Display="Dynamic" ValidationGroup="UserInformation" />
                                <br />
                                <%--<asp:RegularExpressionValidator ID="PasswordRegExValidator" runat="server" ValidationExpression="^.*(?=.{8,35})(?=.*\d)(?=.*[a-zA-Z]).*$" ForeColor="Red" ControlToValidate="Password" ValidationGroup="UserInformation" ErrorMessage="Your Password must be at least 8 charaters long and contain at least 1 number."></asp:RegularExpressionValidator>--%>
                            </td>

                        </tr>
                    </table>



                </div>
            </td>
        </tr>
    </table>

    <asp:ImageButton ID="NextButton" ClientIDMode="Static" runat="server" ImageUrl="images/registration/arrowright.png"
        Height="50px" onmouseover="this.src='images/registration/arrowrightgreen.png'" onmouseout="this.src='images/registration/arrowright.png'" ValidationGroup="UserInformation" OnClientClick="onNextClick()"  AutoPostBack="False" OnClick="NextButton_Click"/>

</div>

<%--STEP 2--%>
<div id="pick_user_type">


    <asp:ImageButton ID="LeftButton" runat="server" ImageUrl="images/registration/arrowleft.png"
        Height="50px" onmouseover="this.src='images/registration/arrowleftgreen.png'"
        onmouseout="this.src='images/registration/arrowleft.png'" ValidationGroup="UserSelectGroup"
        OnClientClick="onLeftClick()" AutoPostBack="False"/>
    <h2>Almost Done! Tell us a little more information.</h2>
    <br />
    <div id="pick_user_list">
        <strong>Check Those That Apply to Your User Experience</strong>
        <asp:CheckBoxList ID="UserTypeList" runat="server">
            <asp:ListItem Selected="True" Value="Listener" Enabled="False">  Listener</asp:ListItem>
            <asp:ListItem Value="Artist">  Artist</asp:ListItem>
            <asp:ListItem Value="Producer">  Producer</asp:ListItem>
            <asp:ListItem Value="Promoter">  Promoter</asp:ListItem>
        </asp:CheckBoxList>
        <br />


        <strong>What music artists do you like?</strong>
        <input id="artistslikedtags" type="text" class="tags" value="" runat="server" />

        <br />

        <div id="whatinstruments" style="display: none">
            <strong>What instruments do you play?</strong>
            <input id="instrumentsplayedtags" type="text" class="tags" value="" disabled="disabled" runat="server"/>
        </div>

        <br />

        <div id="whatrecordingplatforms" style="display: none">
            <strong>What recording platforms or equipment do you use?</strong>
            <input id="recordingplatformstags" type="text" class="tags" value="" disabled="disabled" runat="server"/>
        </div>

        <br />
        <strong>What mobile platform do you use?</strong><br />
        <asp:DropDownList ID="MobilePlatforms" runat="server">
            <asp:ListItem></asp:ListItem>
            <asp:ListItem Value="iOS">iOS (Apple)</asp:ListItem>
            <asp:ListItem Value="Android">Android</asp:ListItem>
            <asp:ListItem Value="Windows">Windows</asp:ListItem>
            <asp:ListItem Value="Blackberry">Blackberry</asp:ListItem>
        </asp:DropDownList>
        <asp:RequiredFieldValidator InitialValue="" ID="MobilePlatformsValidator" runat="server" ControlToValidate="MobilePlatforms" ErrorMessage="This field is required." ToolTip="This field is required." ValidationGroup="UserTypeInformation" ClientIDMode="Static"><font color="red">*</font></asp:RequiredFieldValidator>


        <div style="text-align: right">
            <asp:Button ID="Finish" runat="server" Text="Finish" ControlToValidate="MobilePlatforms" OnClick="FinishButton_Click" UseSubmitBehavior="False" ValidationGroup="MobilePlatforms" />
        </div>
    </div>
</div>

然后在幕后只是一个简单的方法调用。

protected void FinishButton_Click(object sender, EventArgs e)
{
            MembershipAdapter ma = new MembershipAdapter();
            Boolean b = ma.CreateUser(UserName.Text, Email.Text, Password.Text, FirstName.Text, LastName.Text, Gender.Text, Location.Text, Birthdate.Text, MobilePlatforms.Text, "0", "0", "0", artistslikedtags.Value);
}

当我在 createUser() 方法上放置断点时,通过该方法传递的对象中的所有文本都是 EMPTY 字符串!有谁知道为什么会这样?

我试过ASP.NET AutoPostBack 正在清除表单数据

4

2 回答 2

2

您需要了解 ASP.NET 页面生命周期的各个阶段,因为如果不了解这些基础知识,就会对 ASP.NET 实际发生的事情感到非常陌生。

观看面向初学者的 ASP.NET 培训:ASP.NET 页面生命周期并特别注意该Page_Load事件,因为您将在其中进行大量工作,尤其是动态内容创建。

在您发布的示例中,了解您可以在页面第一次加载而不是后续页面请求(称为回发)时执行某些操作,然后您在页面的Page_Load事件中执行以下操作:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        // Do things here that you only want to happen when the page first loads

    }
    else
    {
        // Do things here every time the page is requested, except for the first time

    }
}
于 2013-09-09T01:00:54.373 回答
2

您是否在 Page_Load 期间将文本框值设置为空白?如果是这种情况,你需要用

if(!Page.IsPostback)
{
    //put your initialisation code for them here
}

因为否则当您提交表单时,它们每次都会被清除。

另外,不知道我晚年会失明,<form>上面的代码中没有标签吗?

于 2013-09-08T22:53:34.813 回答