Reputation: 75
Problem: the event handler quantity_TextChanged
does not change the session variable "list" to reflect the current state. The event handler is attached to all the textboxes generated by addItemRowToTable
, and they're told apart by their assigned ids.
I'm definitely sure that something's missing, but I'm not sure.
Additional Info: I need this in a way that the changes made to multiple Textboxes are all saved into 1 Session variable for later use further in the session, in the order as they are presented in the table. The values from each of the Textboxes are then used in their Label that's beside it (1 Label per Textbox).
Desired result:
Header Header2
[TextBox with value of 42] [Label displaying 42]
[TextBox with value of 76] [Label displaying 76]
[TextBox with value of 105] [Label displaying 105]
WebForm1.aspx.cs
protected void Page_LoadComplete(object sender, EventArgs e)
{
if (Session["list"] != null)
{
List<int> numbers = Session["list"] as List<int>;
int index = 0;
foreach (int item in numbers)
{
tblItems.Rows.AddAt(index + 1, addItemRowToTable(item, index));
// For debugging purposes
Response.Write("<script>console.log(\"" + item.ToString() + "\");</script>");
index++;
}
}
}
private TableRow addItemRowToTable(int item, int index)
{
TableCell[] cells = { new TableCell(), new TableCell() };
TableRow row = new TableRow();
TextBox box = new TextBox();
box.ID = String.Format("{0}_q", index);
box.Attributes["type"] = "number";
box.Attributes["min"] = "1";
box.Attributes["width"] = "60px";
box.TextChanged += new EventHandler(quantity_TextChanged);
box.AutoPostBack = true;
cells[0].Controls.Add(box);
Label lbl = new Label();
lbl.ID = String.Format("{0}_l", index);
lbl.Text = item.ToString();
cells[1].Controls.Add(lbl);
foreach (TableCell cell in cells)
row.Cells.Add(cell);
return row;
}
protected void Button1_Click(object sender, EventArgs e)
{
if (Session["list"] != null)
{
List<int> numbers = Session["list"] as List<int>;
numbers.Add(0);
Session["list"] = numbers;
}
else
{
List<int> numbers = new List<int>();
numbers.Add(0);
Session["list"] = numbers;
}
}
private void quantity_TextChanged(object sender, EventArgs e)
{
if (Session["list"] != null)
{
TextBox item = sender as TextBox;
int position = Convert.ToInt32(item.ID.Split('_')[0]);
List<int> numbers = Session["list"] as List<int>;
numbers[position] = Convert.ToInt32(item.Text);
Session["list"] = numbers;
}
}
WebForm1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Assignment2.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<br />
<br />
<asp:Table ID="tblItems" runat="server">
<asp:TableHeaderRow>
<asp:TableHeaderCell>Header</asp:TableHeaderCell>
<asp:TableHeaderCell>Header2</asp:TableHeaderCell>
</asp:TableHeaderRow>
</asp:Table>
</div>
</form>
</body>
</html>
Upvotes: 0
Views: 886
Reputation: 35524
First of all, you don't need to be in Page_LoadComplete
. It is probably your attempt to add the correct amount of textboxes after a button click I think. But the button click should just add a new TextBox, nothing more. In Page_Load you need to add the TextBoxes that are stored in the Session.
See the below sample for a working solution with an TextChanged event that works because all the correct controls are recreated on postback.
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack == false)
{
//do not add or manipulate dynamic controls inside an ispostback check
}
//check if the session exists
if (Session["list"] != null)
{
//add the correct number of textboxes from session
for (int i = 1; i <= Convert.ToInt32(Session["list"]); i++)
{
addTextBox(i);
}
}
}
private void addTextBox(int index)
{
//create a new textbox
TextBox tb = new TextBox();
tb.ID = "DynamicTextBox" + index;
tb.AutoPostBack = true;
//add an event to the textbox
tb.TextChanged += new EventHandler(quantity_TextChanged);
//add the textbox to the page
PlaceHolder1.Controls.Add(tb);
}
protected void Button1_Click(object sender, EventArgs e)
{
int index = 1;
//get the current textbox count if it exists
if (Session["list"] != null)
{
index = Convert.ToInt32(Session["list"]) + 1;
}
//add a new textbox
addTextBox(index);
//upate the session with the new value
Session["list"] = index;
}
private void quantity_TextChanged(object sender, EventArgs e)
{
//display the textbox value in a label for testing
Label1.Text = ((TextBox)sender).Text;
}
Upvotes: 1