Hi I am trying to dynamically create Buttons and TextBoxes from a List in my page load. The Buttons have a click event attached which add one to the TextBox. At the same time this happens I want to update the according List item to the new number.
My issue is that I am getting an error:
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
I don't seem to understand why I am getting the error and thus how I could fix my issue. Below I prepared a code example that recreates the issue:
public partial class _Default : Page
{
List<ProductRates> ProductRateList = new List<ProductRates>() {
new ProductRates(1,"Test",0),
new ProductRates(2,"Test2",0),
new ProductRates(3,"Test3",0)
};
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i < ProductRateList.Count; i++)
{
double testing = ProductRateList[i].ProductOrderAmount;
TextBox textBox = new TextBox();
textBox.Text = "0";
textBox.ID = ProductRateList[i].ProductName + "TextBox";
Button plusButton = new Button();
plusButton.Text = "+";
plusButton.ID = ProductRateList[i].ProductName + "PinusButton";
plusButton.Click += (se, ev) => PlusButton_Click(se, ev, ProductRateList,i, textBox);
System.Web.UI.HtmlControls.HtmlGenericControl createButtonDiv =
new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
createButtonDiv.ID = ProductRateList[i].ProductName + "createButtonDiv";
createButtonDiv.Attributes.Add("class", "col-md-6");
createButtonDiv.Controls.Add(plusButton);
createButtonDiv.Controls.Add(textBox);
ProductPlaceholderDiv.Controls.Add(createButtonDiv);
}
}
void PlusButton_Click(Object sender, EventArgs e, List<ProductRates> ProductRateList, int i, TextBox textBox)
{
string test = textBox.Text;
ProductRateList[i].ProductOrderAmount = 1 + double.Parse(test);
textBox.Text = (ProductRateList[i].ProductOrderAmount).ToString();
}
public class ProductRates
{
public ProductRates(int prodductId, string productName , double productOrderAmount )
{
ProdductId = prodductId;
ProductName = productName;
ProductOrderAmount = productOrderAmount;
}
//Product
public int ProdductId { get; set; }
public string ProductName { get; set; }
public double ProductOrderAmount { get; set; }
}
}
1) Use below new event handler =>
private void PlusButton_Click1(object sender, EventArgs e)
{
Button btn = (Button)sender;
System.Web.UI.HtmlControls.HtmlGenericControl div =(System.Web.UI.HtmlControls.HtmlGenericControl)btn.Parent;
string ProductName = btn.ID.Substring(0, btn.ID.Length - "PinusButton".Length);
TextBox txt = (TextBox)div.FindControl(ProductName + "TextBox");
txt.Text = (Convert.ToInt32(string.IsNullOrEmpty(txt.Text)? "0":txt.Text) + 1).ToString();
}
2) subscribe as =>
plusButton.Click += PlusButton_Click1;
Upvote if the solution works for you..thanks..enjoy coding
I have solved my problem through help of @RiteshKulkarni answer. Even though there is probably a better solution for this and if someone knows the answer please let me know.
Then on the click event that @RiteshKulkarni posted I added a loop that checks if the name is the corresponding one and use this to store the value in the list.
If someone knows a better way to achieve this please let me know.
public partial class _Default : Page
{
List<ProductRates> ProductRateList = new List<ProductRates>() {
new ProductRates(1,"Test",0),
new ProductRates(2,"Test2",0),
new ProductRates(3,"Test3",0)
};
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i < ProductRateList.Count; i++)
{
double testing = ProductRateList[i].ProductOrderAmount;
TextBox textBox = new TextBox();
textBox.Text = ProductRateList[i].ProductOrderAmount.ToString();
textBox.ID = ProductRateList[i].ProductName + "TextBox";
Button plusButton = new Button();
plusButton.Text = "+";
plusButton.ID = ProductRateList[i].ProductName + "PinusButton";
plusButton.Click += PlusButton_Click1;
System.Web.UI.HtmlControls.HtmlGenericControl createButtonDiv =
new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
createButtonDiv.ID = ProductRateList[i].ProductName + "createButtonDiv";
createButtonDiv.Attributes.Add("class", "col-md-6");
createButtonDiv.Controls.Add(plusButton);
createButtonDiv.Controls.Add(textBox);
ProductPlaceholderDiv.Controls.Add(createButtonDiv);
}
}
private void PlusButton_Click1(object sender, EventArgs e)
{
Button btn = (Button)sender;
System.Web.UI.HtmlControls.HtmlGenericControl div = (System.Web.UI.HtmlControls.HtmlGenericControl)btn.Parent;
string ProductName = btn.ID.Substring(0, btn.ID.Length - "PinusButton".Length);
TextBox txt = (TextBox)div.FindControl(ProductName + "TextBox");
for (int i = 0; i < ProductRateList.Count; i++)
{
if (ProductRateList[i].TextBoxName == ProductName + "TextBox")
{
ProductRateList[i].ProductOrderAmount = double.Parse(txt.Text) + 1;
txt.Text = ProductRateList[i].ProductOrderAmount.ToString();
}
}
}
public class ProductRates
{
public ProductRates(int prodductId, string productName , double productOrderAmount )
{
ProdductId = prodductId;
ProductName = productName;
ProductOrderAmount = productOrderAmount;
}
//Product
public int ProdductId { get; set; }
public string ProductName { get; set; }
public double ProductOrderAmount { get; set; }
public string TextBoxName { get; set; }
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.