Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
必须在asp.net中单击两次按钮(在自动回邮文本框之后)_Asp.net_Autopostback - Fatal编程技术网

必须在asp.net中单击两次按钮(在自动回邮文本框之后)

必须在asp.net中单击两次按钮(在自动回邮文本框之后),asp.net,autopostback,Asp.net,Autopostback,在我的一页上: <asp:TextBox runat="server" ID="EmailTextBox" AutoPostBack="true" OnTextChanged="EmailTextBox_Changed" /> <asp:Button runat="server" ID="SearchButton" OnClick="AddButton_Click" Text="add" /> 在EmailTextBox\u Changed中,它统计在运行搜索之前可

在我的一页上:

<asp:TextBox runat="server" ID="EmailTextBox" AutoPostBack="true" OnTextChanged="EmailTextBox_Changed" />
<asp:Button runat="server" ID="SearchButton" OnClick="AddButton_Click" Text="add" />

在EmailTextBox\u Changed中,它统计在运行搜索之前可以找到多少封电子邮件

问题是,当您在EmailTextBox中键入内容并单击按钮时,您必须单击两次才能获得实际结果。这是因为第一次单击是在文本框中执行“自动回发”部分,然后必须再次单击才能执行实际的单击回发


如果不删除“AutoPostBack=true”,在这种情况下,我如何才能停止需要两次单击的操作?

事实上,您不必单击按钮就可以使第一个事件发生。 只需“离开”文本框,即退出“选项卡”即可实现自动回邮


如果您想在一次回发中同时执行这两项操作,只需删除按钮并执行AddButton中的操作,同时单击文本框中的更改事件。

您可以通过不在服务器端执行此操作并使用Javascript来避免此问题。您也没有发布页面加载事件。你在检查它是否发回了吗


另一种方法是,点击按钮时发生的事件可以从TextChanged事件中调用,并一起删除按钮。

将其作为客户端检查是解决此问题的方法…似乎没有办法防止它,否则我也在寻找此问题的答案。我最终删除了所有autopostback=true,并使用JavaScript执行了所有操作,与您一样

然而,我在使用JavaScript之前所做的实验之一是在回发后保持控件焦点。我注意到我用来存储最后一个焦点的控件名称的隐藏字段确实有搜索按钮的名称(我的是一个保存按钮)。因此,虽然我仍然不确定如何让“搜索”功能像它应该的那样“自动”启动,这基本上是将文本框和按钮中的回发事件一个接一个地链接在一起,但我可以知道用户在回发发生(或尝试)之前单击了保存按钮

因此,回发的内容是触发textbox事件,然后是Page_Load方法,或者您想要使用的任何页面循环方法,您可以在其中检查最后一个具有焦点的控件是什么。有了这一点,有几种方法可以实现变通方法

立即,您可以在从控件自动回发触发的每个事件中添加代码,如文本框和搜索按钮,以检查焦点控件的名称。如果最后有焦点的控件不是我们正在运行的控件的自动回发函数,我们可以将名为“Run_Controls_Method”的页面级bool设置为TRUE,否则将其设置为false。这样我们就知道应该运行具有上次焦点回发方法的控件

在页面加载时,您可以执行以下操作:

if (Run_Controls_Method && hdfFocusControl.Value != "")
{
    switch(hdfFocusControl.Value)
    {
        case "btnSearch":
           btnSearch_OnClick(null, null);
           break;
        case etc.
    }
}
我实现hdfHasFocus的方式是:

HTML:

<input id="hdfHasFocus" runat="server" type="hidden" />
ControlManager.cs相关代码:

        /// <summary>
    /// Adds the onfocus event to the UI controls on the controls in the passed in control list.
    /// </summary>
    /// <param name="controls">The list of controls to apply this event.</param>
    /// <param name="saveControl">The control whose .value will be set to the control.ID of the control which had focus before postback.</param>
    /// <param name="Recurse">Should this method apply onfocus recursively to all child controls?</param>
    public static void AddOnFocus(ControlCollection controls, Control saveControl, bool Recurse)
    {
        foreach (Control control in controls)
        {
            //To make the .Add a bit easier to see/read.
            string action = "";

            //Only apply this change to valid control types. 
            if ((control is Button) ||
                (control is DropDownList) ||
                (control is ListBox) ||
                (control is TextBox) ||
                (control is RadDateInput) ||
                (control is RadDatePicker) ||
                (control is RadNumericTextBox))
            {
                //This version ignores errors.  This results in a 'worse case' scenario of having the hdfHasFocus field not getting a 
                //   value but also avoids bothering the user with an error.  So the user would call with a tweak request instead of 
                //   and error complaint.
                action = "try{document.getElementById(\"" + saveControl.ClientID + "\").value=\"" + control.ClientID + "\"} catch(e) {}";

                //Now, add the 'onfocus' attribute and the built action string.
                (control as WebControl).Attributes.Add("onfocus", action);
            }

            //The 'onfocus' event doesn't seem to work for checkbox...use below.
            if (control is CheckBox)
            {
                //This version ignores errors.  This results in a 'worse case' scenario of having the hdfHasFocus field not getting a 
                //   value but also avoids bothering the user with an error.  So the user would call with a tweak request instead of 
                //   and error complaint.
                action = "try{document.getElementById(\"" + saveControl.ClientID + "\").value=\"" + control.ClientID + "\"} catch(e) {}";
                //In case there is already an attribute here for 'onclick' then we will simply try to add to it.
                action = action + (control as WebControl).Attributes["onclick"];

                //Now, add the event attribute and the built action string.                 
                (control as WebControl).Attributes.Add("onclick", action);
            }

            //You don't seem to be able to easily work the calendar button wiht the keyboard, and it seems made for
            //  mouse interaction, so lets set the tab index to -1 to avoid focus with tab.
            if (control is CalendarPopupButton)
            {
                (control as WebControl).Attributes.Add("tabindex", "-1");
            }

            //We also want to avoid user tab to the up and down spinner buttons on any RadNumericTextBox controls.
            if (control is RadNumericTextBox)
            {
                (control as RadNumericTextBox).ButtonDownContainer.Attributes.Add("tabindex", "-1");
                (control as RadNumericTextBox).ButtonUpContainer.Attributes.Add("tabindex", "-1");
            }

            //Recursively call this method if the control in question has children controls and we are told to recurse.
            if ((Recurse) && (control.HasControls()))
            {
                AddOnFocus(control.Controls, saveControl, Recurse);
            }
        }
    }

    /// <summary>
    /// Searches the ControlCollection passed in for a match on the ID name string passed in and sets focus on that control if it is found.
    /// </summary>
    /// <param name="controls">The collection of controls to search.</param>
    /// <param name="FocusToID">The ID of the control to set focus on.</param>
    /// <param name="recurse">Recursively search sub-controls in the passed in control collection?</param>        
    /// <returns>True means keep processing the control list.  False means stop processing the control list.</returns>
    public static bool SetFocus(ControlCollection controls, string FocusToID, bool recurse)
    {
        //Return if no control ID to work with.
        if (string.IsNullOrEmpty(FocusToID) == true)
        { return false; }

        //If we get here and don't have controls, return and continue the other controls if applicable.
        if (controls.Count <= 0)
        { return true; }

        foreach (Control control in controls)
        {
            //If this is the control we need AND it is Enabled, set focus on it.
            if (((control is GridTableRow) != true) &&  //GridTableRow.ClientID throws an error. We don't set focus on a 'Row' anyway.
                (control.ClientID == FocusToID) && 
                ((control as WebControl).Enabled))
            {
                control.Focus();
                //return to caller.  If we were recursing then we can stop now.
                return false;
            }
            else
            {
                //Otherwise, see if this control has children controls to process, if we are told to recurse.
                if ((recurse) && (control.HasControls()))
                {
                    bool _continue = SetFocus(control.Controls, FocusToID, recurse);
                    //If the recursive call sends back false, that means stop.
                    if (_continue != true)
                    { return _continue; }
                }
            }
        }

        //We are done processing all the controls in the list we were given...
        //  If we get here, then return True to the caller.  If this was a recursive call, then
        //  the SetFocus in the call stack above will be told to continue looking since we 
        //  didn't find the control in question in the list we were given.
        return true;
    }
//
///将onfocus事件添加到传入控件列表中控件上的UI控件。
/// 
///要应用此事件的控件列表。
///其.value将设置为回发前具有焦点的控件的control.ID的控件。
///此方法是否应递归地将onfocus应用于所有子控件?
公共静态void AddOnFocus(ControlCollection控件、Control saveControl、bool Recurse)
{
foreach(控件中的控件)
{
//使.Add更易于查看/阅读。
字符串动作=”;
//仅将此更改应用于有效的控件类型。
如果((控件为按钮)||
(控件为下拉列表)||
(控件为列表框)||
(控件是文本框)||
(控件为RADDATE输入)||
(控件为RadDatePicker)||
(控件为RadNumericTextBox))
{
//此版本忽略错误。这会导致HDFHASPOCURE字段未获取错误的“更糟情况”场景
//值,但也避免了因错误而困扰用户。因此,用户将使用调整请求而不是
//和错误投诉。
action=“try{document.getElementById(\”“+saveControl.ClientID+”\”)。value=\”“+control.ClientID+“\”}catch(e){}”;
//现在,添加“onfocus”属性和构建的操作字符串。
(控件为WebControl).Attributes.Add(“onfocus”,action);
}
//“onfocus”事件似乎不适用于复选框…请使用下面的选项。
如果(控件为复选框)
{
//此版本忽略错误。这会导致HDFHASPOCURE字段未获取错误的“更糟情况”场景
//值,但也避免了因错误而困扰用户。因此,用户将使用调整请求而不是
//和错误投诉。
action=“try{document.getElementById(\”“+saveControl.ClientID+”\”)。value=\”“+control.ClientID+“\”}catch(e){}”;
//如果“onclick”在这里已经有一个属性,那么我们将简单地尝试添加到它。
action=action+(控件为WebControl)。属性[“onclick”];
//现在,添加事件属性和生成的操作字符串。
(控件为WebControl).Attributes.Add(“onclick”,action);
}
//您似乎无法轻松使用键盘上的日历按钮,而且它似乎是专为您设计的
//鼠标交互,因此让我们将选项卡索引设置为-1,以避免使用选项卡进行聚焦。
如果(控件为CalendarPopupButton)
{
(控件为WebControl).Attributes.Add(“tabindex”,“-1”);
}
//我们还希望避免用户标签被删除
        /// <summary>
    /// Adds the onfocus event to the UI controls on the controls in the passed in control list.
    /// </summary>
    /// <param name="controls">The list of controls to apply this event.</param>
    /// <param name="saveControl">The control whose .value will be set to the control.ID of the control which had focus before postback.</param>
    /// <param name="Recurse">Should this method apply onfocus recursively to all child controls?</param>
    public static void AddOnFocus(ControlCollection controls, Control saveControl, bool Recurse)
    {
        foreach (Control control in controls)
        {
            //To make the .Add a bit easier to see/read.
            string action = "";

            //Only apply this change to valid control types. 
            if ((control is Button) ||
                (control is DropDownList) ||
                (control is ListBox) ||
                (control is TextBox) ||
                (control is RadDateInput) ||
                (control is RadDatePicker) ||
                (control is RadNumericTextBox))
            {
                //This version ignores errors.  This results in a 'worse case' scenario of having the hdfHasFocus field not getting a 
                //   value but also avoids bothering the user with an error.  So the user would call with a tweak request instead of 
                //   and error complaint.
                action = "try{document.getElementById(\"" + saveControl.ClientID + "\").value=\"" + control.ClientID + "\"} catch(e) {}";

                //Now, add the 'onfocus' attribute and the built action string.
                (control as WebControl).Attributes.Add("onfocus", action);
            }

            //The 'onfocus' event doesn't seem to work for checkbox...use below.
            if (control is CheckBox)
            {
                //This version ignores errors.  This results in a 'worse case' scenario of having the hdfHasFocus field not getting a 
                //   value but also avoids bothering the user with an error.  So the user would call with a tweak request instead of 
                //   and error complaint.
                action = "try{document.getElementById(\"" + saveControl.ClientID + "\").value=\"" + control.ClientID + "\"} catch(e) {}";
                //In case there is already an attribute here for 'onclick' then we will simply try to add to it.
                action = action + (control as WebControl).Attributes["onclick"];

                //Now, add the event attribute and the built action string.                 
                (control as WebControl).Attributes.Add("onclick", action);
            }

            //You don't seem to be able to easily work the calendar button wiht the keyboard, and it seems made for
            //  mouse interaction, so lets set the tab index to -1 to avoid focus with tab.
            if (control is CalendarPopupButton)
            {
                (control as WebControl).Attributes.Add("tabindex", "-1");
            }

            //We also want to avoid user tab to the up and down spinner buttons on any RadNumericTextBox controls.
            if (control is RadNumericTextBox)
            {
                (control as RadNumericTextBox).ButtonDownContainer.Attributes.Add("tabindex", "-1");
                (control as RadNumericTextBox).ButtonUpContainer.Attributes.Add("tabindex", "-1");
            }

            //Recursively call this method if the control in question has children controls and we are told to recurse.
            if ((Recurse) && (control.HasControls()))
            {
                AddOnFocus(control.Controls, saveControl, Recurse);
            }
        }
    }

    /// <summary>
    /// Searches the ControlCollection passed in for a match on the ID name string passed in and sets focus on that control if it is found.
    /// </summary>
    /// <param name="controls">The collection of controls to search.</param>
    /// <param name="FocusToID">The ID of the control to set focus on.</param>
    /// <param name="recurse">Recursively search sub-controls in the passed in control collection?</param>        
    /// <returns>True means keep processing the control list.  False means stop processing the control list.</returns>
    public static bool SetFocus(ControlCollection controls, string FocusToID, bool recurse)
    {
        //Return if no control ID to work with.
        if (string.IsNullOrEmpty(FocusToID) == true)
        { return false; }

        //If we get here and don't have controls, return and continue the other controls if applicable.
        if (controls.Count <= 0)
        { return true; }

        foreach (Control control in controls)
        {
            //If this is the control we need AND it is Enabled, set focus on it.
            if (((control is GridTableRow) != true) &&  //GridTableRow.ClientID throws an error. We don't set focus on a 'Row' anyway.
                (control.ClientID == FocusToID) && 
                ((control as WebControl).Enabled))
            {
                control.Focus();
                //return to caller.  If we were recursing then we can stop now.
                return false;
            }
            else
            {
                //Otherwise, see if this control has children controls to process, if we are told to recurse.
                if ((recurse) && (control.HasControls()))
                {
                    bool _continue = SetFocus(control.Controls, FocusToID, recurse);
                    //If the recursive call sends back false, that means stop.
                    if (_continue != true)
                    { return _continue; }
                }
            }
        }

        //We are done processing all the controls in the list we were given...
        //  If we get here, then return True to the caller.  If this was a recursive call, then
        //  the SetFocus in the call stack above will be told to continue looking since we 
        //  didn't find the control in question in the list we were given.
        return true;
    }
protected void Page_Load(object sender, System.EventArgs e)
    {
        if (IsPostBack)
        {
             // put code here
        }
    }
public void ButtonClick(object sender, EventArgs e)
    {
      //...
    }
BtnSaveAndPrint.Attributes.Add("onclick", "return confirm('Are you sure you Want to Save & Print?');")