方法没有在C#中触发 [英] Method not getting triggered in C#

查看:78
本文介绍了方法没有在C#中触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好!我正在创建一个包含动态添加属性的列表,包括ASP.NET Web应用程序中每行的删除按钮。

Hello! I'm making a list with dynamically added attributes, including a delete button for each row in ASP.NET web application.

private void GenerateRow()
    {
        ctr += 1;
        TableRow row = new TableRow();

        TableCell cell = new TableCell();
        TableCell cell1 = new TableCell();

        Button removeButton = createDeleteButton();

        TextBox tb = new TextBox();

        tb.ID = "TextBoxRow_" + ctr;
        tb.Width = 80;

        removeButton.ID = "RemoveButton" + ctr;

        cell.Controls.Add(tb);
        cell1.Controls.Add(removeButton);

        row.Cells.Add(cell);
        row.Cells.Add(cell1);

        table.Rows.Add(row);
        Session["table"] = table;
        ViewState["ctr"] = ctr;
    }





此方法调用createDeleteButton()方法:





This method calls the createDeleteButton() method:

private Button createDeleteButton()
    {
        int rowToDelete = ctr;
        Button deleteBtn = new Button();
        deleteBtn.Text = "x";
        deleteBtn.Click += (s, e) => RemoveRow(rowToDelete);
        return deleteBtn;
    }





那个方法**应该**调用RemoveRow(int specificRow),它不会:



And that method **should** call RemoveRow(int specificRow), which it doesn't:

protected void RemoveRow(int specificRow)
    {
        TableRow rw = table.Rows[specificRow];
        table.Rows.Remove(rw);

        ctr--;

        Session["table"] = table;
        ViewState["ctr"] = ctr;

    }





有什么想法吗?



我尝试了什么:



我试图在page_load函数中解决这个问题,这让我多喝咖啡休息时间。



Any thoughts?

What I have tried:

I tried to fix this problem within the page_load function, which made me take multiple coffee breaks.

推荐答案

不,不调用方法 - 它将它添加到删除按钮的事件处理程序列表中,按下按钮时将调用该按钮。

如果没有按下按钮,将永远不会调用事件处理程序。



但是......如果你的页面加载没有检查这是否是回发,单击按钮时将获得一组全新的按钮 - 并且在处理单击事件处理程序之前调用load。
No, that doesn't call the method - it adds it to a list of event handlers for the delete button, which will be called when the button is pressed.
If the button isn't pressed, the event handler will never be called.

But ... if your page load doesn't check if this is a postback, you will get a whole new set of buttons when the button is clicked - and load is called before the click event handler can be processed.


这篇关于方法没有在C#中触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆