0

我有一个表格,我可以在其中输入电话号码。我使用了一个图像按钮,当按下该按钮时,如果在它之前的文本框中输入了一个电话号码,它应该显示一个新行来输入电话号码。但是,它并没有按照我想要的方式工作。相反,它给出了错误“未定义 TelNum2”

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);
        }
    }      
4

1 回答 1

1

新2这个,

请尝试以下代码,因为它应该可以帮助您:

-- 应该是 OnClick 而不是 OnClientClick。

<asp:TextBox ID="PhoneNumber" runat="server" width="120px"></asp:TextBox>
 <asp:ImageButton ID="ImageButtonAdd1" runat="server" ImageUrl="~/Images/bullet.png"  
  Height="16px" Width="23px" AlternateText="Add another Phone Number"  
  CausesValidation="False" OnClick="TelNum2_Click" />
<asp:TextBox ID="PhoneNumber2" runat="server" width="120px" Visible="false"></asp:TextBox>

--对于代码隐藏,确保第一个电话号码文本框有一个值,如果有,则显示第二个。

    protected void TelNum2_Click(object sender, ImageClickEventArgs e)
    {
        if (PhoneNumber.Text.Trim().Length > 0)
        {
            PhoneNumber2.Visible = true;
        }
        else
        {
            PhoneNumber2.Visible = false;
        }
    }
于 2014-01-24T20:37:38.780 回答