2010-07-06 15 views
11

एक पेज मेरे पास है पर है।क्लिक करने के लिए बटन को दो बार asp.net में (autopostback पाठ बॉक्स के बाद)

समस्या यह है कि जब आप ईमेलटेक्स्टबॉक्स में कुछ टाइप करते हैं, और बटन पर क्लिक करते हैं, तो आपको वास्तविक परिणाम प्राप्त करने के लिए दो बार क्लिक करना होगा। ऐसा इसलिए है क्योंकि पहला क्लिक टेक्स्ट बॉक्स से "ऑटोपोस्टबैक" भाग कर रहा है, और फिर आपको वास्तविक क्लिक पोस्टबैक होने के लिए फिर से क्लिक करना होगा।

"ऑटोपोस्टबैक = सत्य" को हटाने के बिना, मैं इन परिस्थितियों में दो क्लिकों की आवश्यकता को कैसे रोक सकता हूं?

+0

-Change एक ग्राहक आधारित बटन करने के लिए एक सर्वर नियंत्रण से बटन के अलावा, इसके लिए जेएस का प्रयोग करें। – JonH

उत्तर

1

यह एक ग्राहक के पक्ष की जांच कर रहा था इस का हल ... वहाँ यह को रोकने के लिए अन्यथा

1

वास्तव में, आपको पहली घटना होने के लिए बटन पर क्लिक करने की आवश्यकता नहीं है। बस टेक्स्टबॉक्स को 'छोड़ दें', यानी ऑटोपोस्टबैक होने के लिए 'टैबबिंग' के साथ।

यदि आप एक ही पोस्टबैक में दोनों करना चाहते हैं तो बस बटन को हटाएं और टेक्स्टबॉक्स_Change ईवेंट में AddButton_Click में जो कुछ भी करते हैं उसे करें।

+0

एचएम हाँ, शायद। मैं कोशिश करना चाहता था और इससे बचने के लिए क्योंकि टेक्स्टबॉक्स केवल एक गिनती करता है, और बटन एक पूर्ण fetch करता है। – Paul

0

आप सर्वर पक्ष और जावास्क्रिप्ट का उपयोग करके इसे से बच सकते हैं। आपने अपना पेज लोड इवेंट भी पोस्ट नहीं किया है। क्या आप जांच कर रहे हैं कि यह वापस पोस्ट करता है या नहीं?

एक और तरीका यह है कि आप ऐसा कर सकते हैं, बटन के क्लिक पर होने वाली घटना को टेक्स्ट चेंजेड ईवेंट से बुलाया जा सकता है और बटन को एक साथ छुटकारा पा सकता है।

+0

एचएम हाँ, मैंने यह माना। वास्तव में अधिक टेक्स्ट बॉक्स हैं, और ऑटोपॉस्टबैक वहां पर है ताकि आप उन्हें अपने फ़िल्टर के रूप में देख सकें, और जब आप तैयार हों, तो आप उन्हें दिखा सकते हैं। शायद एक पूरी तरह से जावास्क्रिप्ट समाधान आगे रास्ता है। – Paul

2

मैं इस मुद्दे के लिए एक उत्तर के लिए भी देख रहा था एक तरह से होना करने के लिए प्रतीत नहीं होता। मैं सभी autopostback = सत्य को हटा रहा हूं और जावास्क्रिप्ट के साथ सभी कार्यों को आपके जैसा ही कर रहा हूं।

हालांकि, जावास्क्रिप्ट से पहले मैंने जो चीजों का प्रयोग किया था, उनमें से एक पोस्टबैक के बाद नियंत्रण फोकस बनाए रखने के लिए कुछ था। मैंने देखा कि छिपे हुए क्षेत्र को मैंने उस नियंत्रण के नाम को संग्रहीत करने के लिए उपयोग किया था जिसमें अंतिम फोकस था DID के पास खोज बटन का नाम है (मेरा एक सेव बटन है)। इसलिए, जबकि मुझे अभी भी यकीन नहीं है कि 'खोज' फ़ंक्शन को स्वचालित रूप से 'स्वचालित रूप से' आग लगाना चाहिए, जो मूल रूप से टेक्स्टबॉक्स और बटन दोनों से पोस्टबैक ईवेंट को एक दूसरे के बाद श्रृंखला में ले जाने के लिए है, मैं जान सकता हूं कि उपयोगकर्ता ने पोस्टबैक होने से पहले सहेजने वाले बटन पर क्लिक किया (या कोशिश की)।

तो, आपके पास पोस्टबैक पर जो है, वह आपके टेक्स्टबॉक्स ईवेंट फायरिंग है, और उसके बाद पेज_लोड विधि, या जो पेज पेज विधि आप उपयोग करना चाहते हैं, जहां आप यह देखने के लिए जांच सकते हैं कि अंतिम नियंत्रण किस पर केंद्रित था। इसके साथ, आप कई कामों को लागू कर सकते हैं।

बंद हाथ, आप फोकस नियंत्रण के नाम की जांच करने के लिए, टेक्स्टबॉक्स और खोज बटन जैसे नियंत्रण ऑटोपॉस्टबैक से आग लगने वाली प्रत्येक घटना में कोड जोड़ सकते हैं। यदि नियंत्रण जो आखिरी बार केंद्रित था, वह नियंत्रण का ऑटोपॉस्टबैक फ़ंक्शन नहीं है जिसे हम चल रहे हैं, हम एक पृष्ठ स्तर बूल सेट कर सकते हैं जिसे 'Run_Controls_Method' कहा जाता है, अन्यथा, इसे गलत पर सेट करें। इस तरह हम जानते हैं कि हमें उस नियंत्रण को चलाया जाना चाहिए जिसमें पिछली फोकस पोस्टबैक विधि थी।

पृष्ठ लोड पर, आप की तरह कुछ कर सकता है:

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" /> 

एचटीएमएल कोड के पीछे:

protected void Page_PreRender(object sender,EventArgs e) 
{ 
    if (IsPostBack != true) 
    { 
     //Add the OnFocus event to all appropriate controls on the panel1 panel.   
     ControlManager.AddOnFocus(this.Controls,hdfHasFocus,true); 
     //other code... 
    } 

    ControlManager.SetFocus(this.Controls,hdfHasFocus.Value,true); 
} 

नियंत्रण Manager.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; 
    } 
0

मैं एक ही समस्या थी, मैं पृष्ठ लोड घटना के लिए क्लिक करें घटना कोड के लिए कदम और पोस्टबैक के मामले में यह निष्पादित करने के लिए फैसला किया। और एक क्लिक घटना का उपयोग न करें।

protected void Page_Load(object sender, System.EventArgs e) 
    { 
     if (IsPostBack) 
     { 
      // put code here 
     } 
    } 
बजाय

:

public void ButtonClick(object sender, EventArgs e) 
    { 
     //... 
    } 
1

Page_Load घटना में कोड को रोकने के लिए नीचे लिखें दो बार क्लिक

BtnSaveAndPrint.Attributes.Add("onclick", "return confirm('Are you sure you Want to Save & Print?');") 
संबंधित मुद्दे