ASP.NET确定在页面加载事件中在updatepanel内单击哪个按钮 [英] ASP.NET determine which button was clicked inside an updatepanel in page load event

查看:159
本文介绍了ASP.NET确定在页面加载事件中在updatepanel内单击哪个按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让我的头脑围绕ASP.NET UserControl的页面生命周期问题。我有一个带有两个按钮的更新面板。现在,在Page_Load事件中,我需要检查以查看两个按钮中哪一个被点击了。

I'm trying to get my head around a page lifecycle issue of an ASP.NET UserControl. What I have is an updatepanel with two buttons in it. Now, in the Page_Load event I need to make a check to see which of the two buttons was clicked.

我知道我应该使用这个click事件,但是这是一个相当复杂的页面循环的动态添加控件等等,所以这不是一个选项,不幸的是: - (

I do know that I should use the click event for this, but this is a case of quite a complex page cycle with dynamically added controls and so on, so forth, so that is not an option, unfortunately :-(

ve尝试检查 Request.Form [__ EVENTTARGET] 值,但是由于按钮在UpdatePanel内,该值是一个空字符串(至少我猜这就是为什么它是空的)

I've tried to check on the Request.Form["__EVENTTARGET"] value, but since the buttons are inside an UpdatePanel the value is an empty string (at least I guess that's why it is empty)

所以基本上,有没有办法检查在UpdatePanel中,在Page_Load事件中点击哪个按钮?

So bascially, is there any way to check which button was clicked in an UpdatePanel, in the Page_Load event?

提前谢谢

所有最好的

Bo

推荐答案

您可以通过此方法获取在Page_Load事件中造成回发的ID。

You can get ID of control which caused postback in the Page_Load event by this method.

    protected void Page_Load(object sender, EventArgs e)
    {
           Textbox1.Text = getPostBackControlID();    
    }   

    private string getPostBackControlID()
    {
        Control control = null;
        //first we will check the "__EVENTTARGET" because if post back made by       the controls
        //which used "_doPostBack" function also available in Request.Form collection.
        string ctrlname = Page.Request.Params["__EVENTTARGET"];
        if (ctrlname != null && ctrlname != String.Empty)
        {
            control = Page.FindControl(ctrlname);
        }
        // if __EVENTTARGET is null, the control is a button type and we need to
        // iterate over the form collection to find it
        else
        {
            string ctrlStr = String.Empty;
            Control c = null;
            foreach (string ctl in Page.Request.Form)
            {
                //handle ImageButton they having an additional "quasi-property" in their Id which identifies
                //mouse x and y coordinates
                if (ctl.EndsWith(".x") || ctl.EndsWith(".y"))
                {
                    ctrlStr = ctl.Substring(0, ctl.Length - 2);
                    c = Page.FindControl(ctrlStr);
                }
                else
                {
                    c = Page.FindControl(ctl);
                }
                if (c is System.Web.UI.WebControls.Button ||
                         c is System.Web.UI.WebControls.ImageButton)
                {
                    control = c;
                    break;
                }
            }
        }
        return control.ID; 
    }
}

这篇关于ASP.NET确定在页面加载事件中在updatepanel内单击哪个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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