可点击的Webcontrol,ASP.NET

Ask*_* B. 2 c# asp.net button web-controls

如果您想了解更多背景信息,这个问题与我的上一个问题有关.

我的问题是:是否有可能在asp.net表格中制作单元格?

或者至少可以在ASP.NET中创建一个可点击的WebControl(应该可以放在ControlCollection中),而不是Button或LinkBut​​ton?如果没有,是否可以将多行信息输入按钮文本?

我已经尝试将其他组件添加到按钮的ControlCollection(我已经看到在Windows窗体版本的Button中工作),看看我是否可以将子组件渲染到按钮,但没有成功:

private void ModifyTableCell(TableCell cell)
{
    //Create new button
    Button btnCell = new Button();
    btnCell.Click += (sender, args) =>
    {
        //Event for the button
    };

    //Create new Label
    Label lblCell = new Label();
    lblCell.Font.Bold = true;
    lblCell.Text = "This text won't appear";

    btnCell.Controls.Add(lblCell); //Attempt to add label to Button
    cell.Controls.Add(btnCell);
}
Run Code Online (Sandbox Code Playgroud)

编辑:我最后只为整个单元格创建了一个多行的LinkBut​​ton.

Jam*_*son 6

通过分配onclick属性和利用该__doPostBack功能,您应该能够使任何控件可点击.

ctrl.Attributes["onclick"] = string.Format("__doPostBack('{0}', '{1}');", ctrl.ClientID, "SomeArgument");
Run Code Online (Sandbox Code Playgroud)

您也可以使用该GetPostBackEventReference方法.此选项实际上更安全,因为它将注册__doPostBack它尚不存在的功能:

ctrl.Attributes["onclick"] = Page.ClientScript.GetPostBackEventReference(ctrl, string.Empty);
Run Code Online (Sandbox Code Playgroud)

然后,在代码隐藏中,您可以简单地覆盖该RaisePostBackEvent方法:

protected override void RaisePostBackEvent(IPostBackEventHandler source, string eventArgument)
{
    base.RaisePostBackEvent(source, eventArgument);

    if (eventArgument == "SomeArgument") //using the argument
    {
        //do whatever
    }
}
Run Code Online (Sandbox Code Playgroud)