c# - DataGrid ButtonColumns 的空元素 ID

标签 c# asp.net javascript

我的一个页面中有一些 JavaScript,可以避免用户双击表单上的按钮并在异步回发期间导致双重提交的问题:

var prm = Sys.WebForms.PageRequestManager.getInstance(); prm.add_initializeRequest(InitializeRequest); prm.add_endRequest(EndRequest);

var btn;

函数 InitializeRequest(发件人,args){
  document.body.style.cursor = "等待";
  var btnId = args._postBackElement.id;
  btn = document.getElementById(btnId);
  if (btn != null && (btn.type == "button"|| btn.type == "submit"))
    btn.disabled = true;
}

函数 EndRequest(发件人,参数){
  document.body.style.cursor = "默认";
  如果(btn!= null)btn.disabled = false;
}

这不适用于 DataGrid ButtonColumns。 document.getElementById(btnId) 为这些返回 null。

如果我使用 TemplateColumn 并在其中放置一个 Button 控件,则效果很好。

查看为 DataGrid 呈现的 HTML,我可以看到 ASP.NET 没有为 ButtonColumn 输出 onclick,而它为 TemplateColumn 内的按钮输出:

按钮列:

<input type="submit"
  name="ctl00$placeholderContent$dgCommittees$ctl03$ctl00"
  value="Edit" />

TemplateColumn 中的按钮:

<input type="submit"
  name="ctl00$placeholderContent$dgCommittees$ctl03$cmdDelete"
  value="Delete"
  onclick="return confirm('Are you sure you wish to delete this committee?');
    WebForm_DoPostBackWithOptions(
     new WebForm_PostBackOptions("ctl00$placeholderContent$dgCommittees$ctl03$cmdDelete", "", true, "", "", false, false))"
  id="ctl00_placeholderContent_dgCommittees_ctl03_cmdDelete" />

显然是“我如何使这项工作成功?”的答案。就是用 TemplateColumns 中的按钮简单地替换我所有的 ButtonColumns。

我很好奇是否有人知道 为什么 ASP.NET 以这种方式呈现 ButtonColumns,以及是否有某种方法可以让它像按钮一样呈现它们。

最佳答案

MS 有绑定(bind)字段的标准实现,它不会为 ButtonColumn 创建的按钮设置 ID 或属性。

更具体一点,每当网格创建单元格和后续按钮时,它都会调用 InitializeCell:

public override void InitializeCell(TableCell cell, int columnIndex, ListItemType itemType)
{
    base.InitializeCell(cell, columnIndex, itemType);
    if ((itemType != ListItemType.Header) && (itemType != ListItemType.Footer))
    {
        WebControl child = null;
        if (this.ButtonType == ButtonColumnType.LinkButton)
            ...
        else
        {
            child = new Button {
                Text = this.Text,
                CommandName = this.CommandName,
                CausesValidation = this.CausesValidation,
                ValidationGroup = this.ValidationGroup
            };
        }
        ...
        cell.Controls.Add(child);
    }
}

如您所见,这就是一个按钮的全部内容。

如果你想让它有 ID 和“onclick”属性,你可以使用这样的东西:

protected void grid_RowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //assuming that a first cell is a ButtonField
        //note that in edit mode a button may be at some other index that 0
        Button btn = e.Row.Cells[0].Controls[0] as Button;
        if (btn != null)
        {
            btn.OnClientClick = "return confirm('are you sure?')";
            btn.ID = "myButtonX";
        }
    }
}

关于c# - DataGrid ButtonColumns 的空元素 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/768738/

相关文章:

c# - 我应该为流对象调用 Close() 还是 Dispose()?

c# - 从父对象获取派生属性的数据

c# - 仅比较 ASP.NET 代码中 DATETIME 字段中的 DATE

javascript - 如何将扩展导入 ES6/Typescript 模块

javascript - Vue图像src文件路径不起作用

javascript - 单击带有codemirror的按钮时如何撤消选定/突出显示的文本

c# - 在 Visual Studio 2017 中“找不到类型或命名空间”

c# - 通过 P/Ivoke 在 C# 中传递 Struct 指针(主体中带有 char 指针)

asp.net - 使用 Visual Studio 发布 Web 应用程序项目

asp.net - MVC4 - ContextDependentView - 是什么意思?