ASP.NET ::在page_load期间,如何获取提交回发的控件的ID?

在Page_Load期间,我想捕获执行回发的控件.

protected void Page_Load(object sender,EventArgs e)
{
    if (!Page.IsPostBack)
    {

    }

    // Capture the control ID here.
}

像往常一样,任何想法将不胜感激!

解决方法

对于任何可能对此感兴趣的人(至少对我有用的东西). Cen提供了答案.

在您的Page_Load事件中添加

Control c= GetPostBackControl(this.Page); 

if(c != null) 
{ 
    if (c.Id == "btnSearch") 
    { 
        SetFocus(txtSearch); 
    } 
}

然后在你的基页代码添加

public static Control GetPostBackControl(Page page)
{
    Control control = null;
    string ctrlname = page.Request.Params.Get("__EVENTTARGET");
    if (ctrlname != null && ctrlname != String.Empty)
    {
        control = page.FindControl(ctrlname);

    }
    else
    {
        foreach (string ctl in page.Request.Form)
        {
            Control c = page.FindControl(ctl);
            if (c is System.Web.UI.WebControls.Button)
            {
                control = c;
                break;
            }
        }

    }
    return control;
}

你可以看到原始的帖子here

希望这可以帮助.

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....