__EVENTTARGET为空? [英] __EVENTTARGET empty?

查看:112
本文介绍了__EVENTTARGET为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我阅读的某些网站,我应该能够知道哪个控件导致了页面的回发.我将2个按钮转储到页面上(接受所有默认属性和行为),并在Page Load上测试此变量给我的内容,但返回null.窗体和两个Button的runat ="server".感谢帮助.

According to some sites I read, I should be able to know which control caused the postback of a page. I dumped 2 buttons on a page (accepted all default properties and behaviours) and test on Page Load what this variable gives me but null is returned. The Form and both Button''s runat="server". Help appreciated.

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack)
    {
        var target = Request.Form["__EVENTTARGET"];
        var arg = Request.Form["__EVENTARGUMENT"];
    }
}

推荐答案

只是对Brij答案的一种改进.
Just an improvement to Brij answer.
public 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 mycontrol = page.FindControl(ctl);
            if (mycontrol is System.Web.UI.WebControls.Button)
            {
                control =mycontrol ;
               // This gives you ID of which button caused postback
                Response.Write(mycontrol.ID);
                break;
            }
        }
    }
    return control;
}


对于回发的不同控件有不同的机制.
上面的一个适用于CheckBoxes,DropDownLists,LinkBut​​tons等,不适用于Button控件.


对于按钮控件,如果看一下服务器控件如何呈现为HTML,您会发现按钮没有调用__doPostBack Javascript函数,因此从未设置__EVENTTARGET.而是将Button呈现为简单的输入type ="submit"标签.

由于按钮(或输入)是导致表单提交的原因,因此将其与提交的表单中的所有其他值一起添加到Form集合中的项目中.

因此,您可以通过以下方法找到回发控件

There are different mechanism for different controls for postback.
Above one will work for CheckBoxes, DropDownLists, LinkButtons, etc, this does not work for Button controls.


For button controls, If you take a look at how the server controls render as HTML, you''ll see that the buttons do not call the __doPostBack Javascript function so the __EVENTTARGET is never set. Instead, the Buttons render as simple input type="submit" tags.

Since the button (or input) is what causes the form to submit, it is added to the items in the Form collection, along with all the other values from the submitted form.

So you can find the postback control by the following method

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 mycontrol = page.FindControl(ctl);
            if (c is System.Web.UI.WebControls.Button)
            {
                control =mycontrol ;
                break;
            }
        }
    }
    return control;
}





希望这能解决您的问题.





Hope this will solve your problem.


谢谢.了解您的解释,但不了解粘贴的解决方案. For循环遍历页面上的所有控件,并检查控件是否为按钮.如果按钮不止一个会发生什么?
Thank you. Understand your explanation but don''t understand the pasted solution. The For loop iterates over all the controls on the page and checks if the control is a button. What happens if there are more than one button?


这篇关于__EVENTTARGET为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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