简体   繁体   中英

Can't set button Click event hendler from inside C# literal - ASP.NET, WebForms

I'm using C# and asp web forms. I'm trying to build a table dynamically. Some cells of my table need to have buttons. Everything was working fine using a more verbose c#. When I try to use literal to simplify my code I find a weird thing. I can't access the Click event inside the literal to setup the event handler. First, code that was working!

var row = new TableRow();
var cell = new TableCell();
var btn = new Button();
btn.CausesValidation = false;
btn.UseSubmitBehavior = true;
btn.Text = "Editar";
btn.ID = item.Id.ToString();
btn.CommandName = "excluir";
btn.CommandArgument = item.Id.ToString();
btn.CssClass = "btn btn-primary btn-xs";

//Click event is accessible this way!!!
btn.Click += new EventHandler(btnEditarArquivo_OnClick);

cell.Controls.Add(btn);
row.Cells.Add(cell);
this.table.Rows.Add(row);

Now using literals, the click event don't exist inside the literal. What can I do?

this.table.Rows.Add(new TableRow(){
    Cells ={
        new TableCell() { 
            Controls = {
                new Button() {
                    CausesValidation = false,
                    UseSubmitBehavior = true,
                    Text = "Editar",
                    ID = item.Id.ToString(),
                    CommandName = "excluir",
                    CommandArgument = item.Id.ToString(),
                    CssClass = "btn btn-primary btn-xs",


                    //No click event accessible, this does not work
                    Click += new EventHandler(btnEditarArquivo_OnClick)
                }
            }
        }
    }
});

What can I do? Is there any way to make a self reference to access the click event?

I don't believe there is syntax sugar for events similar to properties.

I'd recommend refactor initialization of the button into separate function to make code more compact.

Note that

 var btn = new Button();
 btn.CausesValidation = false;

is exactly the same as

 var btn = new Button() { CausesValidation = false };

it is just different syntax of setting property right after calling constructor. So unless the reason to switch syntax "I like it that way more" than you should not change it (ie there is no performance impact of any kind).

This is how i did. Using literals I don't need to reassign variables for each cell. But because of the click event I have to create a variable just for the buttons.

this.table.Rows.Clear();//clear table

//Re add headers
this.table.Rows.Add (
    new TableHeaderRow(){ Cells = {
        new TableHeaderCell() {Text = "Id"},
        new TableHeaderCell() {Text = "Descrição"},
        new TableHeaderCell() {Text = "Arquivo"},
        new TableHeaderCell() {Text = "Tamanho"},
        new TableHeaderCell() {Text = "Ed"},
        new TableHeaderCell() {Text = "Ex"},
    }
});

foreach (var item in _listaArquivos) {
    var btn = new Dictionary<string,Button>();
    btn["Editar"] = new Button() {
        CausesValidation = false,
        UseSubmitBehavior = true,
        Text = "Edit",
        ID = item.Id.ToString(),
        CommandName = "edit",
        CommandArgument = item.Id.ToString(),
        CssClass = "btn btn-primary btn-xs",
    };
    btn["Excluir"] = new Button() {
        CausesValidation = false,
        UseSubmitBehavior = true,
        Text = "Excluir",
        ID = item.Id.ToString(),
        CommandName = "excluir",
        CommandArgument = item.Id.ToString(),
            CssClass = "btn btn-primary btn-xs",
    };
    //add events
    btn["Edit"].Click += new EventHandler(btnEditarArquivo_OnClick);
    btn["Exclude"].Click += new EventHandler(btnEditarArquivo_OnClick);
    this.tabelaArquivo.Rows.Add(
        new TableRow() { Cells = {
            new TableCell() {Text = item.Id.ToString()},
            new TableCell() {Text = item.Descricao},
            new TableCell() {Text = item.Arquivo},
            new TableCell() {Text = item.Tamanho},
            new TableCell() {Controls = {btn["Edit"]}},
            new TableCell() {Controls = {btn["Exclude"]}},
        },
    });
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM