我如何制作" link_click"在不影响必填字段验证器的情况下工作?

时间:2015-07-06 03:26:40

标签: c# asp.net linkbutton

详细说明我的问题,我有购物车网站。我也有像用户名,地址,联系人等文本框。所有这些都有必要的字段验证器。但是我也有"查看个人资料"并且"退出"页面上的链接按钮。当我试图阻止这些链接时,它不会让我因为所需的字段没有填满。有什么技巧可以用吗?谢谢你的任何答案:)

这是我的代码。

<asp:TextBox ID="txtCustomerName" runat="server" Width="231px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
    ErrorMessage="Customer Name is Required." ForeColor="Red" ControlToValidate="txtCustomerName"
    ></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
    <td align="left">
        Phone No:
    </td>
</tr>
<tr>
    <td>
        <asp:TextBox ID="txtCustomerPhoneNo" runat="server" Width="231px" 
            MaxLength="11"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
            ErrorMessage="Phone Number is required." ForeColor="Red" ControlToValidate="txtCustomerPhoneNo"
            ></asp:RequiredFieldValidator>
    </td>
</tr>
<tr>
    <td align="left">
        EmailID:
    </td>
</tr>
<tr>
    <td>
        <asp:TextBox ID="txtCustomerEmailID" runat="server" Width="231px"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" 
            ErrorMessage=" Email Address is Required." ForeColor="Red" ControlToValidate="txtCustomerEmailID"
            ></asp:RequiredFieldValidator>
    </td>

这是我的链接代码。

 protected void link_ViewProfile_Click(object sender, EventArgs e)
    {
        Response.Redirect("viewprofile.aspx");
    }

    protected void link_Logout_Click(object sender, EventArgs e)
    {
        Session.Clear();
        Response.Redirect("Home.aspx");
    }

2 个答案:

答案 0 :(得分:1)

对于要运行验证的链接按钮,添加validationgroup属性。例如

<asp:button id="Button2" 
  text="Validate" 
  causesvalidation="true"
  validationgroup="LocationInfoGroup"
  runat="Server" />

还在RequiredFieldValidator

中添加validationgroup属性
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
            ErrorMessage="Phone Number is required." ForeColor="Red" ControlToValidate="txtCustomerPhoneNo"
validationgroup="LocationInfoGroup"
            ></asp:RequiredFieldValidator>

答案 1 :(得分:0)

这是我如何设法解决它的链接。 https://msdn.microsoft.com/en-us/library/ms227424(v=vs.140).aspx。归功于@Tonny