通过页面加载时动态创建的按钮单击事件更新列表项。 (索引超出范围)

Updating list items through dynamically created button click event on page load. (Index was out of range)

您好,我正在尝试从页面加载中的列表动态创建按钮和文本框。 Buttons 有一个附加的点击事件,将一个添加到 TextBox。 同时发生这种情况我想将相应的列表项更新为新编号。

我的问题是出现错误:

Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

我似乎不明白为什么我会收到错误信息,因此我不明白如何解决我的问题。 下面我准备了一个重现问题的代码示例:

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) 使用下面的新事件处理程序 =>

    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) 订阅为 =>

plusButton.Click += PlusButton_Click1;

如果解决方案适合您,请点赞..谢谢..享受编码

我已经通过@RiteshKulkarni 回答的帮助解决了我的问题。 尽管可能有更好的解决方案,如果有人知道答案,请告诉我。

然后在@RiteshKulkarni 发布的点击事件上,我添加了一个循环来检查名称是否对应,并使用它来将值存储在列表中。

如果有人知道实现此目的的更好方法,请告诉我。

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; }
    }
}