有一个aspx页面。当用户单击(+)添加电话号码时,应检查并查看是否输入了先前的号码。如果有的话,只有这样才能动态添加另一个文本框来添加另一个电话号码等。但是,它不会在按下(+)时触发验证触发器,因为它允许页面进行回发,尽管没有输入任何内容,如以及当页面执行回发时它会触发我对其他控件的验证触发器以及相应的错误消息显示。怎么避免这个?
ASPX
<tr><td class="labels">Tel. No. (XXX-XXXX) </td>
<td class="tb">
<asp:TextBox ID="PN1" runat="server" width="120px"></asp:TextBox>
<asp:ImageButton ID="ImageButtonAdd1" runat="server" ImageUrl="~/Styles/Images/add.jpg"
Height="16px" Width="23px" AlternateText="Add another Phone Number"
CausesValidation="False" Onclick="TelNum2_Click" />
<asp:RegularExpressionValidator ID="RegularExpressionValidatorPN" runat="server"
ErrorMessage="Please enter a VALID Phone Number in the format XXX-XXXX" ControlToValidate="PN1" Display="Dynamic" Font-Italic="True" ForeColor="#FF3300" Font-Bold="True" ValidationExpression="\d{3}-\d{4}"></asp:RegularExpressionValidator></tr>
<tr id="phoneNum2" runat="server"><td class="labels"> Tel. No 2. (XXX-XXXX)</td>
<td class="tb"><asp:TextBox ID="PN2" runat="server" Width="120px"></asp:TextBox>
<asp:ImageButton ID="ImageButtonAdd2" runat="server" ImageUrl="~/Styles/Images/add.jpg"
Height="16px" Width="23px" AlternateText="Add another Phone Number" CausesValidation="False"
Onclick="TelNum3" />
<asp:RegularExpressionValidator ID="RegularExpressionValidatorPN2" runat="server"
ErrorMessage="Please enter a VALID Phone Number in the format XXX-XXXX"
ControlToValidate="PN2" Display="Dynamic" Font-Italic="True" ForeColor="#FF3300" Font-
Bold="True" ValidationExpression="\d{3}-\d{4}"></asp:RegularExpressionValidator></td></tr>
<tr id="phoneNum3" runat="server"><td class="labels"> Tel. No 3. (XXX-XXXX)</td>
<td class="tb"><asp:TextBox ID="PN3" runat="server" Width="120px"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidatorPN3" runat="server"
ErrorMessage="Please enter a VALID Phone Number in the format XXX-XXXX"
ControlToValidate="PN3" Display="Dynamic" Font-Italic="True" ForeColor="#FF3300" Font-
Bold="True" ValidationExpression="\d{3}-\d{4}"></asp:RegularExpressionValidator></td></tr>
CS
protected void Page_Load(object sender, EventArgs e)
{
UpdatePanel1.Visible = true;
BtnNew.Visible = true;
BtnDelete.Visible = false;
BtnUpdate.Visible = false;
BtnSave.Visible = false;
BtnCancel.Visible = false;
pubvar.DisableAllControls(Page);
if (!Page.IsPostBack)
{
processAgentData.Visible = false; //area in which textboxes are displayed
phoneNum2.Visible = false;
phoneNum3.Visible = false;
}
else
{
processAgentData.Visible = true;
}
}
protected void TelNum2_Click(object sender, ImageClickEventArgs e)
{
try
{
if (PN1.Text.Trim().Length > 0)
{
phoneNum2.Visible = true;
}
else
{
phoneNum2.Visible = false;
}
}
catch (Exception ex)
{
Console.WriteLine("{0} Exception caught.", ex);
}
}
protected void TelNum3(object sender, EventArgs e)
{
try
{
if (PN2.Text.Trim().Length > 0)
{
phoneNum3.Visible = true;
}
else
{
phoneNum3.Visible = false;
}
}
catch (Exception ex)
{
Console.WriteLine("{0} Exception caught.", ex);
}
}
答案 0 :(得分:1)
您需要在图片按钮上设置CauseValidation = true
。