How to create an Asp button to reveal new textboxes when clicked
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 visib开发者_StackOverflowle 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.
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:
if (!Page.IsPostBack)
{
CanonicalNum = 0;
Session["CanonicalNum"] = CanonicalNum;
}
else
{
CanonicalNum = (int)Session["CanonicalNum"];
}
In your button click event, update the Session object with the new value after you increment CanonicalNum:
Session["CanonicalNum"] = 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:
protected void buttonChooseAnother_Click(object sender, EventArgs e)
{
switch(CanonicalNum)
{
case 0:
textBoxNewCanonical1.Visible = true;
buttonFind1.Visible = true;
break;
case 1:
textBoxNewCanonical1.Visible = true;
buttonFind1.Visible = true;
textBoxNewCanonical2.Visible = true;
buttonFind2.Visible = true;
break;
default:
break;
}
CanonicalNum = CanonicalNum+1;
Session["CanonicalNum"] = CanonicalNum;
}
I think you may want to revisit what you're trying to accomplish, as there's probably a better way to do it.
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.
精彩评论