如何创建一个 Asp 按钮以在单击时显示新文本框
所以我想做的是我在网络表单等上经常看到的事情,基本上在我的网络表单中,我想授予用户根据需要添加更多信息的能力。例如,我要求一个单词,但我想通过一个按钮来允许输入多个单词,每次单击该按钮时都会显示一个附加文本框。现在我尝试实现它的方法是创建 3 个文本框(以及与每个文本框对应的按钮),使第一个文本框可见,但隐藏其余文本框。这个想法是,一个全局变量跟踪应该显示哪个文本框,然后我在它上面运行一个 switch 语句并显示相应的框:
<asp:TextBox ID="textBoxNewCanonical" runat="server"></asp:TextBox>
<asp:Button runat="server" ID="buttonFind" Text="Find" Visible="false" OnClick="buttonFind_OnClick" />
<asp:TextBox ID="textBoxNewCanonical1" Visible="false" runat="server"></asp:TextBox>
<asp:Button runat="server" ID="buttonFind1" Text="Find" Visible="false" OnClick="buttonFind_OnClick" />
<asp:TextBox ID="textBoxNewCanonical2" Visible="false" runat="server"></asp:TextBox>
<asp:Button runat="server" ID="buttonFind2" Text="Find" Visible="false" OnClick="buttonFind_OnClick" />
<asp:Button runat="server" ID="btnMultipleCanonical" Text="Choose Another Canoical" OnClick="buttonChooseAnother_Click" />
这里的按钮ChooseAnother_Click
protected void buttonChooseAnother_Click(object sender, EventArgs e)
{
switch(CanonicalNum)
{
case 0:
textBoxNewCanonical1.Visible = true;
buttonFind1.Visible = true;
break;
case 1:
textBoxNewCanonical2.Visible = true;
buttonFind2.Visible = true;
break;
default:
break;
}
CanonicalNum = CanonicalNum+1;
}
CanonicalNum 设置为 0 来启动这似乎应该可以工作,但是最终发生的情况是,当我单击按钮时,它只显示 textBoxNewCanonical1,然后在下次单击时不执行任何其他操作。 所以我的问题有两个 1. 谁能告诉我我的代码可能有什么问题以及如何修复它 2.如果有更好的方法来做到这一点,我会很高兴听到它
您可能想知道的几件事是我当前需要能够访问按钮我还使用 switch 语句在一个事件中处理它们处理程序。此外,我需要访问文本字段,以便我可以在另一个按钮单击事件时填写值。
编辑:它是母版页的子页面,仅供参考。
So what I would like to do is something I see pretty frequently on webforms etc., basically in my webform I want to grant the user the ability to add more information as is necessary. For example I ask for a word but I want to allow the entry of multiple words by having a button to reveal an additional textbox each time it is clicked. Right now how I am trying to implement it is by creating 3 textboxes (and buttons corresponding to each one) leaving the first one visible but hiding the rest. The idea would be then that a global varible which track which textbox should be revealed and then I run a switch statement on that and reveal the appropriate box:
<asp:TextBox ID="textBoxNewCanonical" runat="server"></asp:TextBox>
<asp:Button runat="server" ID="buttonFind" Text="Find" Visible="false" OnClick="buttonFind_OnClick" />
<asp:TextBox ID="textBoxNewCanonical1" Visible="false" runat="server"></asp:TextBox>
<asp:Button runat="server" ID="buttonFind1" Text="Find" Visible="false" OnClick="buttonFind_OnClick" />
<asp:TextBox ID="textBoxNewCanonical2" Visible="false" runat="server"></asp:TextBox>
<asp:Button runat="server" ID="buttonFind2" Text="Find" Visible="false" OnClick="buttonFind_OnClick" />
<asp:Button runat="server" ID="btnMultipleCanonical" Text="Choose Another Canoical" OnClick="buttonChooseAnother_Click" />
and here is the buttonChooseAnother_Click
protected void buttonChooseAnother_Click(object sender, EventArgs e)
{
switch(CanonicalNum)
{
case 0:
textBoxNewCanonical1.Visible = true;
buttonFind1.Visible = true;
break;
case 1:
textBoxNewCanonical2.Visible = true;
buttonFind2.Visible = true;
break;
default:
break;
}
CanonicalNum = CanonicalNum+1;
}
CanonicalNum is set to 0 to start It seems like this should work but what ends up happening is when I click the button it just shows textBoxNewCanonical1 and then doesn't do anything else the next time I click.
So my question is two-fold
1. Can anyone tell me what might be wrong with my code and how to fix it
2. If there is a better way to do it I would be happy to hear it
A couple things you might want to know is that I need to be able to access the buttons currently I also use a switch statement to handle them in one event handler. Also I need access to the text fields so that I can fill in a value upon another button click event.
Edit: Its a subpage of a Masterpage just fyi.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
单击按钮将导致回发,这将在页面重新加载时将 CanonicalNum 重置为 0。我将使用 Session 来存储 CanonicalNum:
在您的 page_load 方法中,添加以下内容:
在按钮单击事件中,在递增 CanonicalNum 后用新值更新 Session 对象:
如果您希望文本框保持可见,则需要更新你的buttonChooseAnother_Click来处理这个问题,否则那些不匹配CanonicalNum的将在postack上设置回Visible =“false”:
我想你可能想重新审视你正在尝试的内容来完成,因为可能有更好的方法来做到这一点。
The button click will cause postbacks, which will reset CanonicalNum to 0 when the page reloads. I would use Session to store CanonicalNum:
In your page_load method, add this:
In your button click event, update the Session object with the new value after you increment CanonicalNum:
If you want the textboxes to stay visible, you'll need to update your buttonChooseAnother_Click to handle this, otherwise the ones that don't match CanonicalNum will be set back to Visible = "false" on postack:
I think you may want to revisit what you're trying to accomplish, as there's probably a better way to do it.
我将使用 Ajax 来完成此操作,并将按钮和文本框放在 UpdatePanel 内。
然后使用代码创建文本框。唯一复杂的部分是将文本框放置在您想要的位置。为此,您需要使用占位符和 CSS 的组合。
I would do this with Ajax and put the button and the text boxes inside the UpdatePanel.
Then create the text boxes using code. The only complex part is placing the text boxes where you want them. For that you will need to use a combination of placeholder and CSS.