【问题标题】:RequiredFieldValidator error in my asp.net我的 asp.net 中的 RequiredFieldValidator 错误
【发布时间】:2015-08-20 13:54:18
【问题描述】:

我有两页。在第一页我有一个button1,当我点击那个帖子时,一些sessions 到下一页和redirect 到第二页。在第二页我有一些RequiredFieldValidators 但是当我点击button1 时会显示这样的错误!!

Server Error in '/' Application.

The ControlToValidate property of 'FirstnameRequiredFieldValidator' cannot be blank.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Web.HttpException: The ControlToValidate property of 'FirstnameRequiredFieldValidator' cannot be blank.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 


[HttpException (0x80004005): The ControlToValidate property of 'FirstnameRequiredFieldValidator' cannot be blank.]
   System.Web.UI.WebControls.BaseValidator.ControlPropertiesValid() +9831920
   System.Web.UI.WebControls.BaseValidator.OnPreRender(EventArgs e) +36
   System.Web.UI.Control.PreRenderRecursiveInternal() +83
   System.Web.UI.Control.PreRenderRecursiveInternal() +155
   System.Web.UI.Control.PreRenderRecursiveInternal() +155
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +974

我的第二页代码:

  <table style="width:100%;">

            <tr>
                <td class="style1">
                    &nbsp;</td>
                <td class="style2">
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style1">
                    <asp:Label ID="Label1" runat="server" Text="نام" Font-Size="Larger"></asp:Label>
                </td>
                <td class="auto-style2">
                    <asp:TextBox ID="firstnametxt" runat="server" Height="22px" MaxLength="20" 
                        Width="158px"></asp:TextBox>
                </td>
              <td>
                  <asp:RequiredFieldValidator ID="FirstnameRequiredFieldValidator" runat="server" ForeColor="Red" ErrorMessage="نام خود را وارد کنید"></asp:RequiredFieldValidator>
              </td>


            </tr>
            <tr>
                <td class="style1">
                    <asp:Label ID="Label2" runat="server" Text="نام خانوادگی" Font-Size="Larger"></asp:Label>
                </td>
                <td class="style2">
                    <asp:TextBox ID="lastnametxt" runat="server" Height="22px" MaxLength="10" 
                        Width="158px"></asp:TextBox>
                </td>
                 <td>
                  <asp:RequiredFieldValidator ID="LastbaneRequiredFieldValidator" runat="server" ForeColor="Red" ErrorMessage="نام خانوادگی خود را وارد کنید."></asp:RequiredFieldValidator>
              </td>

            </tr>

            <tr>
                <td class="style1">
                    <asp:Label ID="Label4" runat="server" Text="شماره تلفن" Font-Size="Larger"></asp:Label>
                </td>
                <td class="style2">
                    <asp:TextBox ID="phonetxt" runat="server" Height="22px" MaxLength="10" 
                        Width="158px"></asp:TextBox>
                </td>
                 <td>
                  <asp:RequiredFieldValidator ID="PhoneRequiredFieldValidator" runat="server" ForeColor="Red" ErrorMessage="شماره تلفن خود را وارد کنید."></asp:RequiredFieldValidator>
                <asp:RegularExpressionValidator ID="Phonevalidator" 
                    runat="server" ForeColor="Red" ControlToValidate="phonetxt"
                        ErrorMessage="شماره وارد شده نامعتبر است ." 
                        ValidationExpression="\d+">
                    </asp:RegularExpressionValidator>
              </td>


            </tr>

            <tr>
                <td class="style1">
                    &nbsp;</td>
                <td class="style2">
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style1">
                    &nbsp;</td>
                <td class="style2">
                    <asp:Button ID="buy" runat="server" onclick="buybtn_Click" Text="Submit" />
                </td>
                <td>
                    &nbsp;</td>
            </tr>
                           <tr>
                <td class="style1">
                    &nbsp;</td>
                <td class="style2">
                    <td><asp:Label ID="msglabel" runat="server" ></asp:Label></td>

                </td>
                <td>
                    &nbsp;</td>
            </tr>
        </table>  

我的按钮代码:

protected void button1_Click(object sender, EventArgs e)
{

    Session["Numofclass"] = Numofclass.SelectedItem.Value;
    Session["totalprice"] = totalprice;
    Session["Numofstud"] = Numofstud.SelectedItem.Value;
    Response.Redirect("account.aspx");

}  

我的第一页代码:

.
.
.
 <section class =" container">

        <!-- start Pages -->


        <div id="pages">
            <div class="top_shadow"></div>
                        <!-- Start Price -->
            <div id="price" class="page">
            <h1>برنامه ریزی کلاس مجازی و قیمت</h1>
            <div class="tabs">

                <div id="tab_1" class="pane" style="width:85%;text-align:center">                               
                <p style="text-align:right;border-bottom:1px solid #aaa">برخی اساتید و معلمانی هستند که می‌خواهند آموزش خود را گسترش دهند اما نیاز به فضا و تجهیزاتی دارند که آنها را از این کار باز می‌دارد. با ارتقا عضویت خود در طرح دبیرانه به همه امکانات لازم برای گسترش بدون مرز آموزش خود با همه امکانات و تجهیزات لازم دست پیدا می‌کنید.</p>
                <form id="price_biz_calculator"><input type="hidden" name="biz"/>
                    <p>تعداد نفرات دانشجویان شما:

                    <asp:DropDownList ID="Numofstud" runat="server" AutoPostBack="True" Height="40px" OnSelectedIndexChanged="Numofstud_SelectedIndexChanged" Width="150px">

                    <asp:ListItem>10</asp:ListItem>
                    <asp:ListItem>25</asp:ListItem> 
                    <asp:ListItem>50</asp:ListItem>
                    <asp:ListItem>100</asp:ListItem>
                    <asp:ListItem>200</asp:ListItem>
                    <asp:ListItem>250</asp:ListItem>
                    </asp:DropDownList>
                    </p>
                    <p>تعداد کلاس&zwnj;های همزمان:&nbsp&nbsp&nbsp&nbsp&nbsp

                    <asp:DropDownList ID="Numofclass" runat="server" AutoPostBack="True" Height="40px" style="margin-left: 0px" Width="150px" OnSelectedIndexChanged="Numofclass_SelectedIndexChanged">

                    <asp:ListItem>1</asp:ListItem>
                    <asp:ListItem>2</asp:ListItem>
                    <asp:ListItem>3</asp:ListItem>
                    <asp:ListItem>4</asp:ListItem>
                    <asp:ListItem>5</asp:ListItem>
                    <asp:ListItem>6</asp:ListItem>
                    <asp:ListItem>7</asp:ListItem>
                    <asp:ListItem>8</asp:ListItem>
                    <asp:ListItem>9</asp:ListItem>
                </asp:DropDownList>

            </p>
                </form>
                    &nbsp
                    &nbsp
                    <p id="price_final_biz" style="color:#6b6b6b;font-size:100px;line-height:90px"></p>
                    <span style="font-size:20px;vertical-align:22%">ماهیانه</span>

                        <asp:ScriptManager EnablePartialRendering="true" ID="ScriptManager1" runat="server" ViewStateMode="Inherit" ValidateRequestMode="Inherit"></asp:ScriptManager>

                    <asp:UpdatePanel runat="server" id="UpdatePanel" updatemode="Conditional">
                  <ContentTemplate>
              <asp:Label ID="lblTotalprice"   runat="server"/>
               </ContentTemplate>

               <Triggers>
                    <asp:AsyncPostBackTrigger controlid="Numofclass" EventName="SelectedIndexChanged" />
                   <asp:AsyncPostBackTrigger controlid="Numofstud" EventName="SelectedIndexChanged" />
                </Triggers>
                    </asp:UpdatePanel>

                    <span style="font-size:20px;vertical-align:22%">هزار تومان</span>
                    &nbsp
                    &nbsp


                    <div>
                    <td>  
                        <asp:Button ID="button1" runat="server" onclick="button1_Click"   
Text="ثبت" Height="34px" Width="81px" /> 

                    </td>
                        </div>
            </div>
            </div>
            <!-- End Price -->
                        <div class="bottom_shadow"></div>
            </div>
         </div>
        <!-- End Pages -->


        <div class="clear"></div>


        <!-- End Pages -->  

            </section>

【问题讨论】:

    标签: asp.net requiredfieldvalidator


    【解决方案1】:

    原因是您没有告诉验证器它必须验证哪个控件。在您的标记中添加ControlToValidate

    我猜你想验证lastnametxt,因此,

    替换

    中的 validaot 标记
    <asp:RequiredFieldValidator ID="FirstnameRequiredFieldValidator" runat="server" ForeColor="Red" ErrorMessage="نام خود را وارد کنید"></asp:RequiredFieldValidator>
    

    <asp:RequiredFieldValidator ID="FirstnameRequiredFieldValidator" runat="server" ForeColor="Red" ControlToValidate="lastnametxt"  ErrorMessage="نام خود را وارد کنید"></asp:RequiredFieldValidator>
    

    【讨论】:

      【解决方案2】:

      您尚未设置任何RequiredFieldValidator 的ControlToValidate 属性。 将 ControlToValidate 属性值设置为要验证的输入控件。 例如

         <asp:RequiredFieldValidator ControlToValidate="firstnametxt" ID="FirstnameRequiredFieldValidator" runat="server" ForeColor="Red" ErrorMessage="Show Error Msg here"></asp:RequiredFieldValidator>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-08
        • 1970-01-01
        • 2010-10-13
        • 2023-04-04
        相关资源
        最近更新 更多