嗨我需要为学校制作一个计算器,但是当我点击按钮1时,数字1不会进入文本框。
这是我的aspx代码:
protected void btn1_Click(object sender, EventArgs e)
{
// controleer of er minder dan 7 tekens staan
if (txtScherm.Text.Length < 7)
{
txtScherm.Text += "1";
}
}
<asp:Table ID="Table2" runat="server">
<asp:TableRow>
<asp:TableCell><asp:TextBox runat="server" ID="txtScherm"></asp:TextBox></asp:TableCell>
</asp:TableRow>
</asp:Table>
<asp:Table ID="Table1" runat="server">
<asp:TableRow>
<asp:TableCell><asp:Button ID="btn1" runat="server" Text="7" Height="50" Width="50" /></asp:TableCell>
<asp:TableCell><asp:Button ID="btn2" runat="server" Text="8" Height="50" Width="50" /></asp:TableCell>
<asp:TableCell><asp:Button ID="btn3" runat="server" Text="9" Height="50" Width="50" /></asp:TableCell>
<asp:TableCell><asp:Button ID="btn4" runat="server" Text="C" Height="50" Width="50" /></asp:TableCell>
<asp:TableCell><asp:Button ID="btn5" runat="server" Text="Back" Height="50" Width="50" /></asp:TableCell>
</asp:TableRow>
答案 0 :(得分:2)
你必须像这样修改你的代码:
<asp:Button ID="btn1" runat="server" Text="7" Height="50" Width="50" OnClick="btn1_Click"/>
并使用正确的事件对所有按钮执行相同的操作。
答案 1 :(得分:1)
您没有为btn1设置点击事件:
变化:
<asp:TableCell><asp:Button ID="btn1" runat="server" Text="7" Height="50" Width="50" /></asp:TableCell>
为:
<asp:TableCell><asp:Button ID="btn1" runat="server" Text="7" Height="50" Width="50" Click="btn1_Click"/></asp:TableCell>
答案 2 :(得分:0)
添加OnClick =&#34; btn1_Click&#34;如下图所示
<asp:TableCell><asp:Button ID="btn1" runat="server" Text="7" Height="50" Width="50" OnClick="btn1_Click"/></asp:TableCell>