Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Wizard and viewstate

Status
Not open for further replies.

bhaizlett123

Programmer
Joined
Jul 17, 2013
Messages
1
Location
US
I have a form with 3 wizard steps, and when i click the button to dynamically add text boxes, that works fine, but when i go to the next step and i click on add to add more text boxes, it automatically adds all the text boxes from the previous steps and then continues to add if i keep click on it.

How do i prevent that from happening.


private List<string> ControlsList
{
get
{
if (ViewState["controls"] == null)
{
ViewState["controls"] = new List<string>();
}
return (List<string>)ViewState["controls"];
}
}

private int NextID
{
get
{
return ControlsList.Count + 1;
}
}


protected override void LoadViewState(object savedState)
{

string section = Wizard1.ActiveStep.ID;
int sectionNum = Wizard1.ActiveStepIndex;

var control = Wizard1.ActiveStep.FindControl("Place" + sectionNum) as PlaceHolder;

base.LoadViewState(savedState);

int count = 0;
foreach (string txtID in ControlsList)
{
if (count == 0)
{
control.Controls.Add(new LiteralControl("<tr>"));
}
TextBox txt = new TextBox();
control.Controls.Add(new LiteralControl("<td>"));
txt.ID = txtID;
control.Controls.Add(txt);
control.Controls.Add(new LiteralControl("</td>"));

count = count + 1;
if (count == 3)
{
control.Controls.Add(new LiteralControl("</tr>"));
count = 0;
}

}
}


protected void AddControlButton_Click(object sender, EventArgs e)
{


string section = Wizard1.ActiveStep.ID;
int sectionNum = Wizard1.ActiveStepIndex;

var control = Wizard1.ActiveStep.FindControl("Place" + sectionNum) as PlaceHolder;

TextBox txt1 = new TextBox();
TextBox txt2 = new TextBox();
TextBox txt3 = new TextBox();


txt1.ID = section.ToString() + "Size" + NextID.ToString();
control.Controls.Add(new LiteralControl("<td>"));
control.Controls.Add(txt1);
control.Controls.Add(new LiteralControl("</td>"));
ControlsList.Add(txt1.ID);

txt2.ID = section.ToString() + "Description" + NextID.ToString();
control.Controls.Add(new LiteralControl("<td>"));
control.Controls.Add(txt2);
control.Controls.Add(new LiteralControl("</td>"));
ControlsList.Add(txt2.ID);

txt3.ID = section.ToString() + "Quantity" + NextID.ToString();
control.Controls.Add(new LiteralControl("<td>"));
control.Controls.Add(txt3);
control.Controls.Add(new LiteralControl("</td></tr>"));
ControlsList.Add(txt3.ID);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top