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

New2This、

次のコードを試してみてください。

-- OnClientClick ではなく OnClick にする必要があります。

<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>

-- コード ビハインドの場合、最初の電話番号テキスト ボックスに値があることを確認し、値がある場合は 2 番目を表示します。

    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 に答える