How to disable the submit button when partial postback begin ?

Last post 08-02-2006 11:48 AM by ausideveloper. 23 replies.

Sort Posts:

  • How to disable the submit button when partial postback begin ?

    04-19-2006, 11:14 PM
    • Member
      35 point Member
    • fanweixiao
    • Member since 06-02-2004, 5:55 AM
    • Beijing , China
    • Posts 7

    When I use AJAX.net or Anthem, I can make a control enable property while partial postback is working(Anthem's controls has a property to implement this easily). But in Atlas, I can't find a direct way to solve it. AJAX has a feature makes user can check status of xmlhttprequest, but how to do in ATLAS?

    For example:

    I have a textbox, user enter their username here, while they are entering, system will check if username is valid, while in this progress, the enable property of submit button is set to false.

    How to check the status of partial postback and set the button to false and set to true when partial postback is end?

    Thanks in advance.

  • Re: How to disable the submit button when partial postback begin ?

    04-20-2006, 4:34 AM
    • All-Star
      25,662 point All-Star
    • Luis Abreu
    • Member since 02-12-2005, 6:22 AM
    • Madeira [Portugal]
    • Posts 5,368

    hello.

    well, i think that currently tha can't be done since the postback process is managed by the _pagerequestmanager class (which is sealed) and i'm not seeing any events that let's us know about the begining and ending of a postback action.

    i think this woul be a good request for the next CTP ...

    --
    Regards,
    Luis Abreu
    email: labreu_at_gmail.com
    EN blog:http://msmvps.com/blogs/luisabreu
  • Re: How to disable the submit button when partial postback begin ?

    04-20-2006, 6:35 AM
    • Contributor
      7,416 point Contributor
    • Garbin
    • Member since 09-17-2004, 12:35 PM
    • Sassari, Italy
    • Posts 1,506
    • ASPInsiders
      TrustedFriends-MVPs
    Hi,

    check this example (code is in C#):

    <%@ Page Language="C#" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server">
        protected void Page_PreRender(object sender, EventArgs e)
        {
            ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(btnSubmit);
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            System.Threading.Thread.Sleep(3000);  
        }
    </script>

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <atlas:ScriptManager id="sm" runat="server"
                             EnablePartialRendering="true"></atlas:ScriptManager>
        <div>
            <input type="text" id="txtUsername" />
            <asp:Button ID="btnSubmit" runat="server"
                        Text="Submit"
                        OnClick="btnSubmit_Click"
                        />
        </div>
        </form>
        <script type="text/xml-script">
            <page>
                <components>
                    <button id="btnSubmit">
                        <bindings>
                            <binding dataContext="_PageRequestManager"
                                     dataPath="inPostBack"
                                     property="associatedElement"
                                     propertyKey="disabled"
                                     direction="In"
                                     />
                        </bindings>
                    </button>
                </components>
            </page>
        </script>
    </body>
    </html>

    Alessandro Gallo | Blog | My book: ASP.NET AJAX In Action
  • Re: How to disable the submit button when partial postback begin ?

    04-20-2006, 6:45 AM
    • All-Star
      25,662 point All-Star
    • Luis Abreu
    • Member since 02-12-2005, 6:22 AM
    • Madeira [Portugal]
    • Posts 5,368

    hello Garbin.

    cool!

    sometimes one just forgets about what can be done by using bindings...

    --
    Regards,
    Luis Abreu
    email: labreu_at_gmail.com
    EN blog:http://msmvps.com/blogs/luisabreu
  • Re: How to disable the submit button when partial postback begin ?

    04-20-2006, 12:33 PM
    • Member
      585 point Member
    • JRumerman
    • Member since 03-01-2006, 6:26 PM
    • San Diego, CA
    • Posts 140
    That really is a badass way to use bindings.
  • Re: How to disable the submit button when partial postback begin ?

    04-20-2006, 1:19 PM
    • Member
      35 point Member
    • fanweixiao
    • Member since 06-02-2004, 5:55 AM
    • Beijing , China
    • Posts 7

    I found a bug(maybe) here:

    If I write like this, use triggers, it works.

    <atlas:ScriptManager id="sm" runat="server" EnablePartialRendering="true"/>
    <atlas:UpdateProgress ID="upg" runat="server">
            <ProgressTemplate>Updating</ProgressTemplate>
    </atlas:UpdateProgress>
    <atlas:UpdatePanel ID="testUP" runat="server">
            <Triggers>
                <atlas:ControlEventTrigger ControlID="btnSubmit" EventName="Click" />
          </Triggers>
          <ContentTemplate>
                <asp:TextBox runat="server" ID="tb" Text="Lorem Ipsum" /><br />
          </ContentTemplate>
    </atlas:UpdatePanel>
    <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />

     but if I put the button to the ContentTemplate, it just worked at the first time of clicking, after the first time, the button is always enabled.

    Are there some differences about these two ways of writing?

    Thank you very much for telling me the solution:-)

  • Re: How to disable the submit button when partial postback begin ?

    04-20-2006, 5:23 PM
    • Contributor
      7,416 point Contributor
    • Garbin
    • Member since 09-17-2004, 12:35 PM
    • Sassari, Italy
    • Posts 1,506
    • ASPInsiders
      TrustedFriends-MVPs

    Hi,

    yes, you have to use a Trigger to make it work correctly. If you put the button inside the ContentTemplate, after the first postback its client counterpart (i.e. the Atlas Button control) will be disposed, breaking the functionality.

    Alessandro Gallo | Blog | My book: ASP.NET AJAX In Action
  • Re: How to disable the submit button when partial postback begin ?

    04-20-2006, 5:32 PM
    • All-Star
      25,662 point All-Star
    • Luis Abreu
    • Member since 02-12-2005, 6:22 AM
    • Madeira [Portugal]
    • Posts 5,368

    hello guys.

    Garbin, one question: currently, is there any way to inject xml-script from the server side during a partial postback? i'm asking this because currently my focus has been only on the client side of the platform...

    btw, great posts on the drag-n-drop behaviors.

    thanks.

    --
    Regards,
    Luis Abreu
    email: labreu_at_gmail.com
    EN blog:http://msmvps.com/blogs/luisabreu
  • Re: How to disable the submit button when partial postback begin ?

    04-20-2006, 6:33 PM
    • Contributor
      7,416 point Contributor
    • Garbin
    • Member since 09-17-2004, 12:35 PM
    • Sassari, Italy
    • Posts 1,506
    • ASPInsiders
      TrustedFriends-MVPs
    Hi,

    yes it can be done by implementing the IScriptControl interface, overriding the RenderScript() method and registering the control within the ScriptManager by calling its RegisterControl() method.

    For example, here is an Atlas-enabled Button that is disabled when an asynch postback occurs:

    using System;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Microsoft.Web.Script;
    using Microsoft.Web.UI;

    namespace AtlasNotes.Controls
    {
        /// <summary>
        /// An Atlas-enabled Button that is disabled whenever an asynchronous
        /// postback occurs.
        /// </summary>
        public class InPostBackButton : System.Web.UI.WebControls.Button, IScriptControl
        {
            protected override void OnPreRender(EventArgs e)
            {
                base.OnPreRender(e);

                ScriptManager.GetCurrent(this.Page).RegisterControl(this);
            }

            #region IScriptControl Members

            public void RegisterBehavior(IScriptBehavior behavior)
            {
                throw new Exception("The method or operation is not implemented.");
            }
            public bool SupportsBehaviors
            {
                get { throw new Exception("The method or operation is not implemented."); }
            }

            #endregion

            #region IScriptObject Members

            public void RenderScript(ScriptTextWriter writer)
            {
                writer.WriteStartElement("button");
                writer.WriteAttributeString("id", this.ClientID);

                writer.WriteStartElement("bindings");

                writer.WriteStartElement("binding");
                writer.WriteAttributeString("dataContext", "_PageRequestManager");
                writer.WriteAttributeString("dataPath", "inPostBack");
                writer.WriteAttributeString("property", "associatedElement");
                writer.WriteAttributeString("propertyKey", "disabled");
                writer.WriteAttributeString("direction", "In");
                writer.WriteEndElement();

                writer.WriteEndElement();   // bindings
                writer.WriteEndElement();   // button
            }

            #endregion
        }
    }


    It's jsut the same functionality defined for the client button in the previous example, but this time we are dealing with an Atlas-enabled server control, thus we could use it even inside an UpdatePanel:

    <%@ Page Language="C#" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <%@ Register TagPrefix="myatlas" Namespace="AtlasNotes.Controls" %>

    <script runat="server">
        protected void myButton_Click(object sender, EventArgs e)
        {
            System.Threading.Thread.Sleep(3000);
        }
    </script>

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <atlas:ScriptManager ID="sm" runat="server"
                             EnablePartialRendering="true"></atlas:ScriptManager>
        <div>
            <atlas:UpdatePanel ID="upd1" runat="server">
                <ContentTemplate>
                    <span>Username:</span>
                    <input type="text" id="txtUsername" />
                    <myatlas:InPostBackButton id="myButton" runat="server"
                        Text="Submit"
                        OnClick="myButton_Click"
                        />           
                </ContentTemplate>
            </atlas:UpdatePanel>
        </div>
        </form>
    </body>
    </html>


    Hope this helps.
    Note: the Atlas-enabled Button defined above lacks error checking: for example, it's better to check if the ScriptManager has the EnablePartialRendering property set to true, otherwise the framework will raise an error.
    Alessandro Gallo | Blog | My book: ASP.NET AJAX In Action
  • Re: How to disable the submit button when partial postback begin ?

    04-20-2006, 8:53 PM
    • Member
      35 point Member
    • fanweixiao
    • Member since 06-02-2004, 5:55 AM
    • Beijing , China
    • Posts 7

    oic, it seems that I should learn page life circle deeply :-)

    but I have another question, why when I type <script type="text/..... there is no tellisense shows up about "xml-script"?

  • Re: How to disable the submit button when partial postback begin ?

    04-21-2006, 4:14 AM
    • All-Star
      25,662 point All-Star
    • Luis Abreu
    • Member since 02-12-2005, 6:22 AM
    • Madeira [Portugal]
    • Posts 5,368

    hello.

    Garbin, that's cool. and i think with this i can even inject xml-script at the page level by implementing the interface and registering with script manager...thanks again.

    --
    Regards,
    Luis Abreu
    email: labreu_at_gmail.com
    EN blog:http://msmvps.com/blogs/luisabreu
  • Re: How to disable the submit button when partial postback begin ?

    04-23-2006, 5:32 AM
    • Contributor
      7,416 point Contributor
    • Garbin
    • Member since 09-17-2004, 12:35 PM
    • Sassari, Italy
    • Posts 1,506
    • ASPInsiders
      TrustedFriends-MVPs
    Hi,

    fanweixiao:

    why when I type <script type="text/..... there is no tellisense shows up about "xml-script"?

    because it's a custom type, defined by the framework to handle the markup parsing. You can read more about it in this post from Nikhil Kotari's blog.
    Alessandro Gallo | Blog | My book: ASP.NET AJAX In Action
  • Re: How to disable the submit button when partial postback begin ?

    04-23-2006, 7:29 PM
    • Member
      585 point Member
    • JRumerman
    • Member since 03-01-2006, 6:26 PM
    • San Diego, CA
    • Posts 140
    I see that this is the way to render XML-Script, is there a choice to render this as JavaScript instead using the same method (implementing IScriptControl).

    Thx, Joel
  • Re: How to disable the submit button when partial postback begin ?

    04-24-2006, 4:50 AM
    • All-Star
      25,662 point All-Star
    • Luis Abreu
    • Member since 02-12-2005, 6:22 AM
    • Madeira [Portugal]
    • Posts 5,368

    hello.

    well, you can get the same results by using javascript. to garantee that it's injected on the page, you'll have to use the clientscripmanager and its registerXXX methods.

    --
    Regards,
    Luis Abreu
    email: labreu_at_gmail.com
    EN blog:http://msmvps.com/blogs/luisabreu
  • Re: How to disable the submit button when partial postback begin ?

    04-24-2006, 10:54 AM
    • Member
      585 point Member
    • JRumerman
    • Member since 03-01-2006, 6:26 PM
    • San Diego, CA
    • Posts 140
    Ok ... I was just wondering if there was a switch or something to have the method spit out JavaScript code instead of the XML-Script code. I guess not, no big deal.

    Thx, Joel
Page 1 of 2 (24 items) 1 2 Next >