• __EVENTTARGET is empty on postback of button click


    __EVENTTARGET is empty on postback of button click

    问题

    I have a button on aspx page

    <asp:Button runat="server" CssClass="sc-ButtonHeightWidth" ID="btnFirstSave" Text="Save" OnClick="btnSave_Click" />

    I am trying to get the event target and event source in code behind to do some validation based on it. I tried with below code.

    string ctrlname = page.Request.Params.Get("__EVENTTARGET");
    string ctrlname = Request.Form["__EVENTTARGET"];
    string ctrlname = Request.Params["__EVENTTARGET"];

    But all the above are giving me empty values. How to get the control which caused postback everytime. Am i doing anyting wrong above?

    FYI : I already tried the solution mentioned in this LINK. But its only returning button text for me. I want the buttonID.

    评论

    try with <asp:Button runat="server" usesubmitbehavior="false" ...
    – Damith
    Dec 30, 2013 at 10:56

    回答1

    Asp button renders as input with type submit this method will not fill_EVENTTARGET controls using "__doPostBack" method to cause postback will add values to _EVENTTARGET so your button id is missing from _EVENTTARGET you can iterate through all the controls in page to check which control caused postback.

    Try this to capture Your control -Here

    private string getPostBackControlName()
            {
                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;
    
            }

    回答2

    I have set usesubmitbehavior to false as of now. A good solution given by Damith above in the comment. As of now its working fine for me without any problem. To know about the property Read this LINK

    Hope this will help someone.

  • 相关阅读:
    [转] STM32 FSMC学习笔记
    【转】嵌入式系统 Boot Loader 技术内幕
    mini2440 使用 JLink V8 直接烧写 Nor flash
    S3C6410移植uboot(一)
    2440的RTC时钟
    关闭2440 屏幕背光
    基于十级流水线的开立方根算法
    Visual Studio 2008配置SystemC开发环境
    Linux C 中字符串化操作符#
    linux 中 timeval结构体
  • 原文地址:https://www.cnblogs.com/chucklu/p/16563021.html
Copyright © 2020-2023  润新知